diff options
| author | Peng Wu <alexepico@gmail.com> | 2022-03-18 14:46:43 +0800 |
|---|---|---|
| committer | Peng Wu <alexepico@gmail.com> | 2022-03-18 14:46:43 +0800 |
| commit | 2b07bef9cf7cb35ca81a15fa347edc6de6d6d122 (patch) | |
| tree | 94b13e46f3231ab3c152d7c2ce0d418e9eded347 /src | |
| parent | c18c197c67e694e52424c37d8f1da04f29728511 (diff) | |
Write class TableDatabase
Diffstat (limited to 'src')
| -rw-r--r-- | src/PYTableDatabase.cc | 59 | ||||
| -rw-r--r-- | src/PYTableEditor.cc | 11 |
2 files changed, 55 insertions, 15 deletions
diff --git a/src/PYTableDatabase.cc b/src/PYTableDatabase.cc index 1580ec5..98d6c1d 100644 --- a/src/PYTableDatabase.cc +++ b/src/PYTableDatabase.cc @@ -50,11 +50,11 @@ TableDatabase::init () gchar *path = g_build_filename (g_get_user_cache_dir (), "ibus", "libpinyin", "table-user.db", NULL); - if (m_user_instance->isDatabaseExisted (path)) - result = m_user_instance->openDatabase (path, TRUE); - else + if (!m_user_instance->isDatabaseExisted (path)) result = m_user_instance->createDatabase (path); + result = m_user_instance->openDatabase (path, TRUE); + if (!result) g_warning ("can't open user table database.\n"); } @@ -146,17 +146,19 @@ TableDatabase::createDatabase(const char *filename) { m_sql = "CREATE TABLE IF NOT EXISTS phrases ( " "id INTEGER PRIMARY KEY NOT NULL," "tabkeys TEXT NOT NULL," - "phrase TEXT NOT NULL UNIQUE," + "phrase TEXT NOT NULL," "freq INTEGER NOT NULL DEFAULT (10)" ");"; if (!executeSQL (tmp_db)) { sqlite3_close (tmp_db); return FALSE; } + + sqlite3_close (tmp_db); return TRUE; } -/* No self-learning here, and no user database file. */ +/* Self-learning is only in user table database file. */ gboolean TableDatabase::openDatabase(const char *filename, gboolean writable) { int flags = SQLITE_OPEN_READONLY; @@ -185,7 +187,8 @@ TableDatabase::listPhrases(const char *prefix, /* list phrases */ const char *SQL_DB_LIST = "SELECT phrase FROM phrases " - "WHERE tabkeys LIKE \"%s%\" ORDER BY freq DESC, id ASC;"; + "WHERE tabkeys LIKE \"%s%\" " + "ORDER BY LENGTH(phrase) ASC, freq DESC, id ASC;"; m_sql.printf (SQL_DB_LIST, prefix); int result = sqlite3_prepare_v2 (m_sqlite, m_sql.c_str(), -1, &stmt, &tail); if (result != SQLITE_OK) @@ -273,19 +276,27 @@ TableDatabase::importTable (const char *filename){ if (result != SQLITE_ROW) return FALSE; + int id = 0; result = sqlite3_column_type (stmt, 0); - if (result != SQLITE_INTEGER) - return FALSE; - int id = sqlite3_column_int (stmt, 0); + if (result == SQLITE_INTEGER) + id = sqlite3_column_int (stmt, 0); + else if (result == SQLITE_NULL) + id = 0; + else + g_warning ("Can't find id for user table database."); + + m_sql = "BEGIN TRANSACTION;"; + result = executeSQL (m_sqlite); + /* Open the table file with format: "tabkeys phrase freq". */ - while (!feof (input)) { ++id; char tabkeys[256], phrase[256]; int freq = 10; fscanf (input, "%255s %255s %d\n", tabkeys, phrase, &freq); - if (feof(input)) + + if (tabkeys[0] == '\0' || phrase[0] == '\0') break; const char *SQL_DB_REPLACE = @@ -298,6 +309,9 @@ TableDatabase::importTable (const char *filename){ break; } + m_sql = "COMMIT;"; + result = executeSQL (m_sqlite); + fclose (input); return TRUE; } @@ -385,13 +399,28 @@ public: TableDatabase *db = new TableDatabase (); bool retval = db->isDatabaseExisted ("../data/table.db"); g_assert (retval); - retval = db->openDatabase ("../data/table.db"); + retval = db->openDatabase ("../data/table.db", FALSE); g_assert (retval); - std::vector<std::string> chars; + std::vector<std::string> phrases; std::vector<std::string>::iterator iter; - db->listCharacters("hshshhh", chars); + db->listPhrases("hshshhh", phrases); + printf ("characters:\t"); + for (iter = phrases.begin(); iter != phrases.end(); ++iter) + printf ("%s ", iter->c_str()); + printf ("\n"); + phrases.clear (); + delete db; + + db = new TableDatabase (); + if (!db->isDatabaseExisted ("table.db")) + db->createDatabase ("table.db"); + db->openDatabase ("table.db", TRUE); + + db->importTable ("table.txt"); + db->exportTable ("export.txt"); + db->listPhrases("a", phrases); printf ("characters:\t"); - for (iter = chars.begin(); iter != chars.end(); ++iter) + for (iter = phrases.begin(); iter != phrases.end(); ++iter) printf ("%s ", iter->c_str()); printf ("\n"); printf ("table database test ok.\n"); diff --git a/src/PYTableEditor.cc b/src/PYTableEditor.cc index 1a37afd..2b62101 100644 --- a/src/PYTableEditor.cc +++ b/src/PYTableEditor.cc @@ -31,6 +31,8 @@ #define _(text) (gettext (text)) +#define TABLE_DATABASE_ADD_FREQUENCY 10 + namespace PY { TableEditor::TableEditor (PinyinProperties &props, Config &config) @@ -236,6 +238,15 @@ TableEditor::selectCandidate (guint index) IBusText *candidate = m_lookup_table.getCandidate (index); Text text (candidate); + + if (m_config.useCustomTable ()) { + TableDatabase *table_database = &TableDatabase::userInstance (); + int freq = 0; + table_database->getPhraseInfo (text.text (), freq); + freq += TABLE_DATABASE_ADD_FREQUENCY; + table_database->updatePhrase (text.text (), freq); + } + commitText (text); reset (); return TRUE; |
