2005年 6月 24日 金曜日
第2話 - libauto_ef の使い方
libauto_ef の使い方です。libauto_ef は、文字エンコーディングを 判別するための library で、auto_ef (第1話参照) も、libauto_ef を 使って、エンコーディングを判別しています。 libauto_ef が提供している関数の詳細については、libauto_ef(3LIB) を 参照してください。ここでは簡単な sample (auto_ef_file()) とその説明を載せます。
/* */
/* test_file.c : auto_ef_file sample */
/* */
1 #include
2 #include
3
4 main (int argc, char **argv)
5 {
6 auto_ef_t *root1 = NULL;
7 int i=0;
8 size_t test;
9 test = auto_ef_file(&root1, argv[1], AE_LEVEL_0);
10 for (i=0; i < (int)test; i++){
11 printf("encoding = %s\n", auto_ef_get_encoding(root1[i]));
12 printf("score = %f\n", auto_ef_get_score(root1[i]));
13 printf("\n");
14 }
15 auto_ef_free(&root1[0]);
16 exit(0);
17 }
2 #include
ヘッダーファイル auto_ef.h を取り込んでいます。
auto_ef.h は、/usr/include/auto_ef.h にあります。
6 auto_ef_t *root1 = NULL;
エンコーディング判別の結果を格納する root1 を定義しています。
root1 に格納された値は、auto_ef_get_encoding (3LIB)、auto_ef_get_score(3LIB) で
取得することができます。11 行目、12 行目で実際に使っているので参照してください。
9 test = auto_ef_file(&root1, argv[1], AE_LEVEL_0);
エンコーディングの判別を行います。2番目の引数 argv[1] には、判別を行う
対象のファイル名が入ります。3番目の引数 AE_LEVEL_0 は、判別の精度を
指定しています。レベルは 0 から 3 まで存在し、AE_LEVEL_0 が最も精度が低く、
レベルが上がるに従って、精度は上がりますが、処理速度は遅くなります。
auto_ef_file は可能性のあるエンコーディングの数を戻り値 (test) として
返します。
10 for (i=0; i < (int)test; i++){
11 printf("encoding = %s\n", auto_ef_get_encoding(root1[i]));
12 printf("score = %f\n", auto_ef_get_score(root1[i]));
13 printf("\n");
14 }
判別したエンコーディングを出力しています。
auto_ef_get_encoding は、判別されたエンコーディング名を取得します。
エンコーディング名は、Solaris にバンドルされている iconv (1) で
使用できるエンコーディング名になっています。
auto_ef_get_score は、判別された エンコーディングにマッチする
可能性 (パーセンテージ) を取得します。
15 auto_ef_free(&root1[0]);
auto_ef_file によって割り当てられたメモリーを解放します。
% cc test_file.c -lauto_efで、コンパイルできるはずです。
./a.out FILENAMEfilename にエンコーディングを判別したいファイル名を入力します。
% ./a.out ~/memo/command.doc encoding = eucJP score = 94.000000 encoding = ko_KR.euc score = 5.000000このように、使い方は非常に簡単ですので、 是非試してみてください。
Posted at 07:35午後 6 24, 2005 by kenji in ローテクな話 | 投稿されたコメント[0]