OSDN Git Service

modified: src/recognize_kanji.c
[kp123/kp123.git] / src / recognize_kanji.c
index 5bde148..8579f1d 100644 (file)
@@ -139,7 +139,7 @@ static gunichar2* find_next_entry(gchar *allkanji, gunichar2 *entry, gint allkan
 
 static gunichar2* pick_kanji(GList *strokes, gchar **sdata, gchar *allkanji, gint allkanjilen)
 {
-    const gint MAX_DISTANCE = 5;
+    const gint MAX_DISTANCE = g_list_length(strokes) - 1;
     gint datalen = g_strv_length(sdata), i;
     gunichar2 key = 'A' + datalen - 1;
     gunichar2 *entry = (gunichar2*)allkanji;
@@ -178,6 +178,14 @@ static gunichar2* pick_kanji(GList *strokes, gchar **sdata, gchar *allkanji, gin
     return ret;
 }
 
+static GMappedFile* load_strokes_txt(gchar *dir)
+{
+    gchar *fname = g_build_filename(dir, "strokes.txt", NULL);
+    GMappedFile *file = g_mapped_file_new(fname, FALSE, NULL);
+    g_free(fname);
+    return file;
+}
+
 gunichar2* recognize_kanji(GList *strokes)
 {
     static gchar **sdata = NULL;
@@ -209,18 +217,16 @@ gunichar2* recognize_kanji(GList *strokes)
     }
     sdata[strokes_len] = 0;
     sdata_len = strokes_len;
-#ifdef KP_LIBDIR
-    gchar *dir = g_strdup(KP_LIBDIR);
-#else
-    gchar *dir = "";
+    GMappedFile *file = load_strokes_txt("");
+    if(!file)
+    {
+#ifdef KP123_DATADIR
+       file = load_strokes_txt(KP123_DATADIR);
 #endif
-    gchar *fname = g_build_filename(dir, "strokes.txt", NULL);
-    GMappedFile *file = g_mapped_file_new(fname, FALSE, NULL);
+    }
     if(!file)
     {
-       g_free(fname);
-       g_free(dir);
-       g_printf("failed to open strokes.txt\n");
+       g_printf("failed to load strokes.txt\n");
        return 0;
     }
     gint allkanjilen = g_mapped_file_get_length(file);