自然言語処理について勉強しています。
Wikipediaのdumpデータから本文を抜き出して処理をしようと思ってます。
xmlの処理について試しているのですが、出力結果が想定とは全然違って困っています。
欲しい出力は,"タイトル(str),id(int),<textのシークポイント(*int)(<textの二つ目のtのポイント),<textから</text>の間のバイト数(*int)の四つがセットになったものです.
おそらくたくさんのif文処理で目的のところにひっかけられてないのだと思います.
以下にソースコード
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 256 * 1024 * 1024 int main() { int p, pb[6]; char *line = malloc(SIZE); char *title; char *id; FILE *fp = fopen("jawiki-20211220-pages-articles-multistream2.xml", "r"); FILE *fw = fopen("getX.txt", "w"); //記入先 if ((NULL == fp) || (NULL == fw)) abort(); while (p = ftell(fp), fgets(line, SIZE, fp)) { if (strstr(line, "<title>")) { pb[0] = p; if ((strstr(line, "</title>"))) { pb[1] = p - pb[0]; pb[0] += strlen("<title>"); fseek(fp, pb[0], SEEK_SET); fgets(title, pb[1], fp); printf("%s\n", title); if (strstr(line, "<title>")) { pb[2] = p; if (strstr(line, "<id>")) { pb[2] = p; if (strstr(line, "</id>")) { pb[3] = p - pb[2]; pb[2] += strlen("<id>"); fseek(fp, pb[2], SEEK_SET); fgets(id, pb[3], fp); printf("title=\t%s\nid=\t%s\n", title, id); if (strstr(line, "<text")) { pb[4] = p; if (strstr(line, "</text>")) { pb[5] = p - pb[4]; printf("%s\t%s\t%d\t%d\n", title, id, pb[4], pb[5]); } } } } } } } } printf("process ok"); free(line); fclose(fw); fclose(fp); }
出力結果は何も表示されてないようです.急ぎです.お願いします.
追記:6/2618:00
修正依頼にあったように処理の前にprintfを追加してみたバージョンのソースと実行結果を載せておきます.
おそらく/titleの処理を忘れているのではないかなと思います.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 256 * 1024 * 1024 int main() { int p, pb[6]; char *line = malloc(SIZE); char *title; char *id; FILE *fp = fopen("jawiki-20211220-pages-articles-multistream2.xml", "r"); FILE *fw = fopen("getX.txt", "w"); //記入先 if ((NULL == fp) || (NULL == fw)) abort(); while (p = ftell(fp), fgets(line, SIZE, fp)) { /* // textやpageなどの複数行にわたるとき if (strstr(line, "<text")) {//属性があるため<text pb[0] = p; } if (strstr(line, "</text>")) { pb[1] = p - pb[0]; printf("%d\t%d\n", pb[0], pb[1]); fprintf(fw, "%d\t%d\n", pb[0], pb[1]); } */ if (strstr(line, "<title>")) { printf("title in\t 1\n"); pb[0] = p; if ((strstr(line, "</title>"))) { printf("/title in\t1\n"); pb[1] = p - pb[0]; pb[0] += strlen("<title>"); fseek(fp, pb[0], SEEK_SET); fgets(title, pb[1], fp); printf("%s\n", title); if (strstr(line, "<title>")) { printf("title\tin\t22222222222\n"); pb[2] = p; if (strstr(line, "<id>")) { printf("id in\t\n"); pb[2] = p; if (strstr(line, "</id>")) { printf("/id in \n"); pb[3] = p - pb[2]; pb[2] += strlen("<id>"); fseek(fp, pb[2], SEEK_SET); fgets(id, pb[3], fp); printf("title=\t%s\nid=\t%s\n", title, id); if (strstr(line, "<text")) { printf("text in "); pb[4] = p; if (strstr(line, "</text>")) { printf("/text in "); pb[5] = p - pb[4]; printf("%s\t%s\t%d\t%d\n", title, id, pb[4], pb[5]); } } } } } } } } printf("process ok"); free(line); fclose(fw); fclose(fp); }
以下実行結果の一部です.
title in 22222222222 title in 1/title in 2(null) title in 22222222222 title in 1/title in 2(null) title in 22222222222 title in 1/title in 2(null) title in 22222222222 title in 1/title in 2(null) title in 22222222222 title in 1/title in 2(null) title in 22222222222 process ok
処理のバランスおかしいですね.

回答2件
あなたの回答
tips
プレビュー