diff --git a/src/data/dictionary_manual/BUILD.bazel b/src/data/dictionary_manual/BUILD.bazel index 071ae812b..4c451d4c7 100644 --- a/src/data/dictionary_manual/BUILD.bazel +++ b/src/data/dictionary_manual/BUILD.bazel @@ -30,4 +30,5 @@ exports_files([ "domain.txt", "era.txt", + "words.tsv", ]) diff --git a/src/data/dictionary_manual/words.tsv b/src/data/dictionary_manual/words.tsv new file mode 100755 index 000000000..98ac76501 --- /dev/null +++ b/src/data/dictionary_manual/words.tsv @@ -0,0 +1,101 @@ +# key value pos +あいしんかくら 愛新覚羅 姓 +あさごはん 朝ごはん 名詞 +あさごはん 朝ご飯 名詞 +あそうわん 浅海湾 固有名詞 +いっきょうたじゃく 一強他弱 名詞 +いとしの 愛し野 地名 +いりなか 杁中 地名 +うっぷんばらし 鬱憤晴らし 名詞サ変 +うとろ 宇登呂 地名 +うらしべつ 浦士別 地名 +うらめん 裏面 名詞 +えいえいじてん 英英辞典 名詞 +えとんびやま 江鳶山 地名 +おくぶたえ 奥二重 名詞 +おんねない 音根内 地名 +おんねべつ 遠音別 地名 +かいかい 回回 固有名詞 +かいごかんせい 下位互換性 名詞 +かいていこう 改訂稿 名詞 +かくしゃ 覚者 名詞 +かっくみ 活汲 地名 +かねのなるき 金のなる木 名詞 +からだき 空焚き 名詞サ変 +かんじょうば 勘定場 名詞 +かんりめいがら 監理銘柄 名詞 +ききん 木禽 地名 +きそう 寄贈 名詞サ変 +きんとれ 筋トレ 名詞サ変 +くうしゅうごう 空集合 名詞 +くわたけいすけ 桑田佳祐 人名 +けっていこう 決定稿 名詞 +こういしつ 更衣室 名詞 +こうじょ 扣除 名詞サ変 +こうばいりょくへいか 購買力平価 名詞 +こきゅう 枯朽 名詞 +こんせ 今世 名詞 +こんぜ 今世 名詞 +こんせい 今世 名詞 +さいしゅうこう 最終稿 名詞 +さかもとりゅういち 坂本龍一 人名 +さきむい 崎無異 地名 +さぶすく サブスク 名詞サ変 +さんこうにんしょうち 参考人招致 名詞サ変 +じきしょうそう 時期尚早 名詞形動 +ししゅう 歯周 名詞 +しっちゃく 失着 名詞サ変 +じってんぽ 実店舗 名詞 +しゅうせいこう 修正稿 名詞 +じょういごかんせい 上位互換性 名詞 +しんしょかいふうざい 信書開封罪 名詞 +しんせん 深圳 地名 +せいりめいがら 整理銘柄 名詞 +ぜんざいさん 全財産 名詞 +そうけつ 倉頡 人名 +そうけつ 蒼頡 人名 +そっかん 速乾 名詞サ変 +たいしょうがい 対象外 名詞 +たいしょうない 対象内 名詞 +たいせつ 堆雪 名詞サ変 +たごうら 田子浦 固有名詞 +たちあらい 太刀洗 姓 +たちあらい 太刀洗 地名 +たようそにんしょう 多要素認証 名詞サ変 +たんろくでんち 単6電池 名詞 +たんろくでんち 単六電池 名詞 +ちえんべつ 知円別 地名 +でびあん デビアン 固有名詞 +とうよ 党与 名詞サ変 +とっくつ 突厥 固有名詞 +とっけつ 突厥 固有名詞 +とっぷし 富武士 地名 +にだんかいうせつ 二段階右折 名詞サ変 +にだんかいにんしょう 二段階認証 名詞サ変 +にっぽんじゅう 日本中 名詞 +にほんじゅう 日本中 名詞 +にようそにんしょう 二要素認証 名詞サ変 +はいたしょり 排他処理 名詞サ変 +ばうんしゃ 馬運車 名詞 +ひかりの ひかり野 地名 +ひともうけ 一儲け 名詞サ変 +ひゃっきん 百均 名詞 +ひらぶん 平文 名詞 +ふいふい 回回 固有名詞 +ふたつ 不達 名詞サ変 +ふもうご 不妄語 名詞 +ほじょう 圃場 名詞 +ほっけ 𩸽 名詞 +ほんじ 翻字 名詞サ変 +みどり 美禽 地名 +むえき 無益 名詞形動 +むびゅう 無謬 名詞 +むびゅうせい 無謬性 名詞 +むやく 無役 名詞形動 +もどりち 戻り値 名詞 +やくたたず 役立たず 名詞形動 +らのべ ラノベ 名詞 +りねーむ リネーム 名詞サ変 +りまいんど リマインド 名詞サ変 +りゅうこうごたいしょう 流行語大賞 名詞 +れいわ 令和 固有名詞 diff --git a/src/data/dictionary_oss/BUILD.bazel b/src/data/dictionary_oss/BUILD.bazel index 45a006345..f281c3da7 100644 --- a/src/data/dictionary_oss/BUILD.bazel +++ b/src/data/dictionary_oss/BUILD.bazel @@ -99,13 +99,17 @@ filegroup( genrule( name = "aux_dictionary", srcs = [ + ":aux_dictionary.tsv", ":base_dictionary_data", - "//data/oss:aux_dictionary.tsv", + ":id.def", + "//data/dictionary_manual:words.tsv", ], outs = ["aux_dictionary.txt"], cmd = ("$(location //dictionary:gen_aux_dictionary) " + "--strict --output $@ " + - "--aux_tsv $(location //data/oss:aux_dictionary.tsv) " + + "--aux_tsv $(location :aux_dictionary.tsv) " + + "--id_def $(location :id.def) " + + "--words_tsv $(location //data/dictionary_manual:words.tsv) " + "--dictionary_txts $(locations :base_dictionary_data)"), tools = ["//dictionary:gen_aux_dictionary"], ) @@ -114,11 +118,11 @@ genrule( name = "filtered_dictionary", srcs = [ ":base_dictionary_data", - "//data/oss:dictionary_filter.tsv", + ":dictionary_filter.tsv", ], outs = ["dictionary.txt"], cmd = ("$(location //dictionary:gen_filtered_dictionary) --output $@ " + - "--filter_tsv $(location //data/oss:dictionary_filter.tsv) " + + "--filter_tsv $(location :dictionary_filter.tsv) " + "--dictionary_txts $(locations :base_dictionary_data)"), tools = ["//dictionary:gen_filtered_dictionary"], ) diff --git a/src/data/oss/README.md b/src/data/dictionary_oss/README.md similarity index 98% rename from src/data/oss/README.md rename to src/data/dictionary_oss/README.md index c688bb290..f7e699d83 100644 --- a/src/data/oss/README.md +++ b/src/data/dictionary_oss/README.md @@ -1,4 +1,4 @@ -# data/oss +# data/dictionary_oss ## aux_dictionary.tsv diff --git a/src/data/dictionary_oss/aux_dictionary.tsv b/src/data/dictionary_oss/aux_dictionary.tsv new file mode 100644 index 000000000..0834ebc92 --- /dev/null +++ b/src/data/dictionary_oss/aux_dictionary.tsv @@ -0,0 +1,8 @@ +# key value base_key base_value cost_offset +みにおぼえ 身に覚え みにおぼえ 見に覚え -1 +たちあらい 太刀洗 たちあらい 大刀洗 -1 +よろしくおねがいします よろしくお願いします よろしくおねがいします よろしくおねがいします -1 +あさごはん 朝ごはん あさごはん 朝ゴハン -2 +こんせ 今世 こんき 今期 0 +いっきょう 一強 いちきょう 一強 0 +いりなか 杁中 しおがまぐち 塩釜口 0 diff --git a/src/data/oss/dictionary_filter.tsv b/src/data/dictionary_oss/dictionary_filter.tsv similarity index 100% rename from src/data/oss/dictionary_filter.tsv rename to src/data/dictionary_oss/dictionary_filter.tsv diff --git a/src/data/dictionary_oss/evaluation.tsv b/src/data/dictionary_oss/evaluation.tsv index 4d6cab163..4a45b7ef3 100644 --- a/src/data/dictionary_oss/evaluation.tsv +++ b/src/data/dictionary_oss/evaluation.tsv @@ -185,7 +185,7 @@ FAILED: あらいながらもこんごにきたい 洗いながらも今後に OK: かうかちがあるとおもうなら 買う価値があると思うなら Conversion Expected 買う価値があると思うなら 2.26.4596 OK: みにおぼえのない 身に覚えのない Conversion Expected 身に覚えのない 2.26.4596 OK: まめまめしい まめまめしい Conversion Expected 2 忠実忠実しい 2.26.4596 -FAILED: さかもとりゅういち 坂本竜一 Conversion Expected 坂本龍一 2.30.5432 +OK: さかもとりゅういち 坂本龍一 Conversion Expected 坂本龍一 2.30.5490 OK: あさごはん 朝ごはん Conversion Expected 2 朝ごはん 2.26.4610 OK: ひるごはん 昼ご飯 Conversion Expected 2 昼ごはん 2.30.5432 OK: とっけつ 突厥 Conversion Expected 突厥 2.26.4596 @@ -202,7 +202,7 @@ OK: しんせん 新鮮 Conversion Expected 9 深圳 2.28.4702 OK: ふたつ 2つ Conversion Expected 9 不達 2.28.4730 OK: からだき 空焚き Conversion Expected 2 空焚き 2.28.4730 OK: ししゅう 刺繍 Conversion Expected 9 歯周 2.28.4750 -OK: そうけつ 倉頡 Conversion Expected 9 倉頡 2.30.5432 +OK: そうけつ 総穴 Conversion Expected 9 倉頡 2.30.5490 OK: あかつきにふねをいだす 暁に船を出だす Conversion Expected 暁に船を出だす 2.28.4770 OK: さいのうをみいだす 才能を見出す Conversion Expected 2 才能を見出だす 2.30.5432 FAILED: いっぴょう 一瓢 Conversion Expected 2 一票 2.28.4880 @@ -249,7 +249,7 @@ OK: らのべ ラノベ Conversion Expected ラノベ 2.28.5000 FAILED: なごやとばし 名古屋鳥羽市 Conversion Expected 名古屋飛ばし 2.30.5490 OK: こんせ 今世 Conversion Expected 9 今世 2.28.5000 OK: こんぜ 今世 Conversion Expected 9 今世 2.28.5000 -OK: こんせい 今世 Conversion Expected 9 今世 2.28.5000 +OK: こんせい 混成 Conversion Expected 9 今世 2.30.5490 OK: ふくじょし 副助詞 Conversion Expected 2 副助詞 2.28.5050 FAILED: らっぷ ラップ Conversion Expected 9 乱舞 2.28.4990 OK: こきゅう 呼吸 Conversion Expected 9 枯朽 2.28.5000 diff --git a/src/data/oss/BUILD.bazel b/src/data/oss/BUILD.bazel deleted file mode 100644 index 253aa7cb3..000000000 --- a/src/data/oss/BUILD.bazel +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright 2010-2021, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) - -exports_files(glob([ - "*.tsv", -])) diff --git a/src/data/oss/aux_dictionary.tsv b/src/data/oss/aux_dictionary.tsv deleted file mode 100644 index 41f199a73..000000000 --- a/src/data/oss/aux_dictionary.tsv +++ /dev/null @@ -1,109 +0,0 @@ -# key value base_key base_value cost_offset -でびあん デビアン うぶんつ ウブンツ 0 -かねのなるき 金のなる木 きんのたまご 金の卵 0 -かいごかんせい 下位互換性 ごかんせい 互換性 0 -じょういごかんせい 上位互換性 ごかんせい 互換性 0 -とっけつ 突厥 ひがしとっけつ 東突厥 0 -とっくつ 突厥 ひがしとっけつ 東突厥 0 -かいかい 回回 かいぞく 回族 0 -ふいふい 回回 かいぞく 回族 0 -れいわ 令和 へいせい 平成 0 -みにおぼえ 身に覚え みにおぼえ 見に覚え -1 -たごうら 田子浦 たごのうら 田子の浦 0 -あそうわん 浅海湾 あそうわん 浅茅湾 1 -たちあらい 太刀洗 たちあらい 大刀洗 -1 -よろしくおねがいします よろしくお願いします よろしくおねがいします よろしくおねがいします -1 -えいえいじてん 英英辞典 こくごじてん 国語辞典 0 -まめまめしい 忠実忠実しい まめまめしい まめまめしい 1 -ほんじ 翻字 ほんやくさぎょう 翻訳作業 0 -あさごはん 朝ごはん あさごはん 朝ゴハン -2 -あさごはん 朝ご飯 あさごはん 朝ゴハン -1 -さかもとりゅういち 坂本龍一 さかもとりゅういち 坂本竜一 -1 -やくたたず 役立たず ふよう 不要 0 -むえき 無益 ふよう 不要 0 -むやく 無役 ふよう 不要 0 -むびゅう 無謬 ごびゅう 誤謬 0 -むびゅうせい 無謬性 ごびゅう 誤謬 1 -しんせん 深圳 しんせん 深セン -1 -しんせんし 深圳市 しんせんし 深セン市 -1 -ふたつ 不達 ふざい 不在 0 -からだき 空焚き からやき 空焼き 0 -ししゅう 歯周 しこう 歯垢 0 -そうけつ 倉頡 りし 李斯 0 -そうけつ 蒼頡 りし 李斯 0 -いだす 出だす みだす 見出す 0 -みいだす 見出だす みいだす 見出す 0 -さんこうにんしょうち 参考人招致 しょうにんかんもん 証人喚問 0 -きそう 寄贈 きぞう 寄贈 0 -ひらぶん 平文 あんごうぶん 暗号文 0 -うっぷんばらし 鬱憤晴らし うさばらし 憂さ晴らし 0 -こういしつ 更衣室 しちゃくしつ 試着室 0 -にようそにんしょう 二要素認証 めーるにんしょう メール認証 0 -たようそにんしょう 多要素認証 めーるにんしょう メール認証 0 -きんとれ 筋トレ ふっきん 腹筋 0 -おくぶたえ 奥二重 ふたえまぶた 二重瞼 0 -にほんじゅう 日本中 くにじゅう 国中 0 -にっぽんじゅう 日本中 くにじゅう 国中 0 -じってんぽ 実店舗 あきてんぽ 空き店舗 0 -うらめん 裏面 りめん 裏面 0 -にだんかいうせつ 二段階右折 うせつ 右折 0 -にだんかいにんしょう 二段階認証 めーるにんしょう メール認証 0 -ひともうけ 一儲け かねもうけ 金儲け 0 -ひゃっきん 百均 ざっか 雑貨 0 -こうじょ 扣除 こうじょ 控除 5000 -りゅうこうごたいしょう 流行語大賞 ほんやたいしょう 本屋大賞 0 -たんろくでんち 単6電池 たんごでんち 単5電池 0 -たんろくでんち 単六電池 たんごでんち 単五電池 0 -ふもうご 不妄語 もうご 妄語 0 -ぜんざいさん 全財産 ざいさん 財産 0 -さぶすく サブスク さぶすくりぷしょん サブスクリプション 0 -ばうんしゃ 馬運車 ゆそうしゃ 輸送車 0 -しんしょかいふうざい 信書開封罪 ぎしょうざい 偽証罪 0 -あいしんかくら 愛新覚羅 いおきべ 五百旗頭 0 -らのべ ラノベ しょうせつ 小説 0 -こんせ 今世 こんき 今期 0 -こんぜ 今世 こんき 今期 0 -こんせい 今世 こんき 今期 0 -こきゅう 枯朽 ふきゅう 不朽 0 -こうばいりょくへいか 購買力平価 こうばいりょく 購買力 500 -たいせつ 堆雪 はいせつ 排雪 0 -りねーむ リネーム りむーぶ リムーブ 0 -はいたしょり 排他処理 はいたせいぎょ 排他制御 0 -じきしょうそう 時期尚早 せっそく 拙速 0 -そっかん 速乾 かんそう 乾燥 500 -かんりめいがら 監理銘柄 めいがら 銘柄 500 -せいりめいがら 整理銘柄 めいがら 銘柄 500 -いっぽん 一本 いっこ 一個 0 -ほじょう 圃場 ほじょう ほ場 0 -うとろ 宇登呂 うとろ ウトロ 1 -ひかりの ひかり野 うとろ ウトロ 0 -ちえんべつ 知円別 うとろ ウトロ 0 -おんねべつ 遠音別 うとろ ウトロ 0 -さきむい 崎無異 うとろ ウトロ 0 -うらしべつ 浦士別 うとろ ウトロ 0 -えとんびやま 江鳶山 うとろ ウトロ 0 -とっぷし 富武士 うとろ ウトロ 0 -かっくみ 活汲 うとろ ウトロ 0 -みどり 美禽 うとろ ウトロ 0 -ききん 木禽 うとろ ウトロ 0 -いとしの 愛し野 うとろ ウトロ 0 -おんねない 音根内 うとろ ウトロ 0 -りまいんど リマインド りむーぶ リムーブ 0 -くわたけいすけ 桑田佳祐 かものちょうめい 鴨長明 0 -しっちゃく 失着 せいちゃく 正着 0 -さいひんち 最頻値 ちゅうおうち 中央値 0 -くうしゅうごう 空集合 ちゅうおうち 中央値 0 -たいしょうがい 対象外 ようといがい 用途以外 0 -たいしょうない 対象内 ようといがい 用途以外 0 -かいていこう 改訂稿 しょこう 初稿 0 -けっていこう 決定稿 しょこう 初稿 0 -さいしゅうこう 最終稿 しょこう 初稿 0 -しゅうせいこう 修正稿 しょこう 初稿 0 -いっきょう 一強 いちきょう 一強 0 -いっきょうたじゃく 一強他弱 いっきとうせん 一騎当千 0 -もどりち 戻り値 かえりち 返り値 0 -ほっけ 𩸽 かれい 鰈 0 -かんじょうば 勘定場 てっかば 鉄火場 0 -かくしゃ 覚者 いんじゃ 隠者 0 -とうよ 党与 さんよ 参与 0 -いりなか 杁中 しおがまぐち 塩釜口 0 diff --git a/src/data/rules/boundary.def b/src/data/rules/boundary.def index 4247a4762..6dfb728bf 100644 --- a/src/data/rules/boundary.def +++ b/src/data/rules/boundary.def @@ -69,6 +69,8 @@ PREFIX 助詞,(格助詞|連体化) 3000 # Suppress し|なし PREFIX 動詞,自立,*,*,サ変・スル,連用形 3000 PREFIX 動詞,自立,*,*,サ変・スル,* 1000 +# Suppress あるXX pattern (ラ変動詞) +PREFIX 動詞,自立,*,*,ラ変, 5000 # general noun might be buried in personal names # especially we have a lot of first name variants diff --git a/src/dictionary/gen_aux_dictionary.py b/src/dictionary/gen_aux_dictionary.py index 30bea5363..be2463635 100644 --- a/src/dictionary/gen_aux_dictionary.py +++ b/src/dictionary/gen_aux_dictionary.py @@ -37,7 +37,7 @@ is -1, the new entry is ranked higher than the base entry. gen_aux_dictionary.py --output aux_dictionary.txt ---aux_tsv data/oss/aux_dictionary.tsv +--aux_tsv data/dictionary_oss/aux_dictionary.tsv --dictionary_txts data/dictionary_oss/dictionary0*.txt """ @@ -45,12 +45,96 @@ import sys +class Entry(): + """Class for an entry in the dictionary.""" + + def __init__(self, key: str, value: str, lid: str, rid: str, cost: int): + self.key = key + self.value = value + self.lid = lid + self.rid = rid + self.cost = cost + + def EntryKey(self) -> str: + return '\t'.join([self.lid, self.rid, self.key, self.value]) + + @classmethod + def Parse(cls, line: str) -> 'Entry': + key, lid, rid, cost, value, *_ = line.rstrip().split('\t') + return Entry(key, value, lid, rid, int(cost)) + + +class EntryList(): + """Class for a list of entries.""" + + def __init__(self): + self.entries = [] + self.sorted = False + + def Add(self, entry: Entry) -> None: + self.entries.append(entry) + self.sorted = False + + def Sort(self) -> None: + if self.sorted: + return + self.entries.sort(key=lambda entry: entry.cost) + self.sorted = True + + def AtRatio(self, ratio: float) -> Entry: + self.Sort() + ratio = max(0, min(ratio, 1.0)) + index = int(max(len(self.entries) - 1, 0) * ratio) + return self.entries[index] + + +class PosIds(): + """Class for pos ids.""" + + def __init__(self): + self.ids = {} # "pos_name" -> "pos_id" + # Keys are subset of data/rules/user_pos.def + self.pos_alias = { + '名詞': '名詞,一般,*,*,*,*,*', + '固有名詞': '名詞,固有名詞,一般,*,*,*,*', + '人名': '名詞,固有名詞,人名,一般,*,*,*', + '姓': '名詞,固有名詞,人名,姓,*,*,*', + '名': '名詞,固有名詞,人名,名,*,*,*', + '組織': '名詞,固有名詞,組織,*,*,*,*', + '地名': '名詞,固有名詞,地域,一般,*,*,*', + '名詞サ変': '名詞,サ変接続,*,*,*,*,*', + '名詞形動': '名詞,形容動詞語幹,*,*,*,*,*', + '副詞': '副詞,一般,*,*,*,*,*', + '連体詞': '連体詞,*,*,*,*,*,*', + '接続詞': '接続詞,*,*,*,*,*,*', + '感動詞': '感動詞,*,*,*,*,*,*', + '接頭語': '接頭詞,名詞接続,*,*,*,*,*', + '助数詞': '名詞,接尾,助数詞,*,*,*,*', + '接尾一般': '名詞,接尾,一般,*,*,*,*', + '接尾人名': '名詞,接尾,人名,*,*,*,*', + '接尾地名': '名詞,接尾,地域,*,*,*,*', + } + + def ParseFile(self, id_def: str): + for line in open(id_def, encoding='utf-8'): + pos_id, name = line.rstrip().split(' ') + self.ids[name] = pos_id + + for alias, name in self.pos_alias.items(): + pos_id = self.ids[name] + self.ids[alias] = pos_id + + def GetId(self, pos_name: str) -> str: + return self.ids.get(pos_name, '') + + class Dictionary(): """Class for dictionary0*.txt.""" def __init__(self): self.entry_set = set() - self.data = {} + self.data = {} # "\t" -> [entry1, entry2, ...] + self.pos_entry_map = {} # "\t" -> EntryList def Parse(self, dictionary_txts): for file in dictionary_txts: @@ -59,18 +143,20 @@ def Parse(self, dictionary_txts): def _ParseFile(self, file): """Parses the file and set values to data and entry_set.""" for line in open(file, encoding='utf-8'): - key, lid, rid, cost, value, *_ = line.rstrip().split('\t') + entry = Entry.Parse(line) # Update data - data_key = '\t'.join([key, value]) - data_value = [lid, rid, cost] - self.data.setdefault(data_key, []).append(data_value) - - cost = int(cost) + data_key = '\t'.join([entry.key, entry.value]) + self.data.setdefault(data_key, []).append(entry) # Update entry_set - entry_key = '\t'.join([lid, rid, key, value]) - self.entry_set.add(entry_key) + self.entry_set.add(entry.EntryKey()) + + # Update pos_map + pos_key = '\t'.join([entry.lid, entry.rid]) + if pos_key not in self.pos_entry_map: + self.pos_entry_map[pos_key] = EntryList() + self.pos_entry_map[pos_key].Add(entry) def Exists(self, key, value, lid, rid): entry_key = '\t'.join([lid, rid, key, value]) @@ -80,6 +166,10 @@ def GetDataList(self, key, value): data_key = '\t'.join([key, value]) return self.data.get(data_key) + def GetCost(self, pos_id: str, ratio: float) -> int: + pos_key = '\t'.join([pos_id, pos_id]) + return self.pos_entry_map[pos_key].AtRatio(ratio).cost + class AuxDictionary(): """Class for aux_dictionary.tsv.""" @@ -87,6 +177,7 @@ class AuxDictionary(): def __init__(self, dictionary): self.dictionary = dictionary self.aux_list = [] + self.aux_set = set() def Parse(self, aux_tsv, strict=False): """Parses the file and update aux_list.""" @@ -104,12 +195,17 @@ def Parse(self, aux_tsv, strict=False): print('Warning: ' + message, file=sys.stderr) continue - for base_lid, base_rid, base_cost in data_list: - if self.dictionary.Exists(key, value, base_lid, base_rid): + for entry in data_list: + if self.dictionary.Exists(key, value, entry.lid, entry.rid): # Not overwrite the existing entry. continue - cost = int(base_cost) + int(cost_offset) - self.aux_list.append([key, base_lid, base_rid, str(cost), value]) + cost = int(entry.cost) + int(cost_offset) + self.aux_list.append([key, entry.lid, entry.rid, str(cost), value]) + self.aux_set.add('\t'.join([entry.lid, entry.rid, key, value])) + + def Exists(self, key, value, lid, rid): + entry_key = '\t'.join([lid, rid, key, value]) + return entry_key in self.aux_set def WriteFile(self, output): with open(output, 'w', encoding='utf-8') as file: @@ -117,10 +213,54 @@ def WriteFile(self, output): file.write('\t'.join(aux_entry) + '\n') +class WordsDictionary(): + """Class for aux_dictionary.tsv.""" + + def __init__(self, dictionary, aux): + self.dictionary = dictionary + self.aux = aux + self.words = [] + + def Parse(self, words_tsv, id_def, ratio=0.5): + """Parses the file and update aux_list.""" + pos_ids = PosIds() + pos_ids.ParseFile(id_def) + + for line in open(words_tsv, encoding='utf-8'): + if line.startswith('#') or not line.rstrip(): + continue + + key, value, pos = line.rstrip().split('\t') + pos_id = pos_ids.GetId(pos) + if not pos_id: + print('Error: ' + pos + ' is an invalid pos', file=sys.stderr) + print(line, file=sys.stderr) + sys.exit(1) + + # Do not overwrite the existing entry. + if self.dictionary.Exists(key, value, pos_id, pos_id) or self.aux.Exists( + key, value, pos_id, pos_id + ): + continue + + # Use the same cost as the entry of pos_id and ratio. + # ratio indicates the position in the words sorted by cost. + # 0.5 means the middle of the list. 0.0 means the top of the list. + cost = self.dictionary.GetCost(pos_id, ratio) + self.words.append([key, pos_id, pos_id, str(cost), value]) + + def WriteFile(self, output): + with open(output, 'a', encoding='utf-8') as file: + for word in self.words: + file.write('\t'.join(word) + '\n') + + def main(): parser = argparse.ArgumentParser() parser.add_argument('--aux_tsv') + parser.add_argument('--words_tsv') parser.add_argument('--dictionary_txts', nargs='+') + parser.add_argument('--id_def') parser.add_argument('--output') parser.add_argument('--strict', action='store_true') args = parser.parse_args() @@ -132,6 +272,10 @@ def main(): aux.Parse(args.aux_tsv, strict=args.strict) aux.WriteFile(args.output) + if args.words_tsv and args.id_def: + words = WordsDictionary(dictionary, aux) + words.Parse(args.words_tsv, args.id_def) + words.WriteFile(args.output) if __name__ == '__main__': main() diff --git a/src/dictionary/gen_filtered_dictionary.py b/src/dictionary/gen_filtered_dictionary.py index f702e2876..753ad3f2b 100644 --- a/src/dictionary/gen_filtered_dictionary.py +++ b/src/dictionary/gen_filtered_dictionary.py @@ -31,7 +31,7 @@ r"""A tool to remove specified words from dictionary files. gen_filtered_dictionary.py --output filtered_dictionary.txt ---filter_tsv data/oss/dictionary_filter.tsv +--filter_tsv data/dictionary_oss/dictionary_filter.tsv --dictionary_txts data/dictionary_oss/dictionary0*.txt ### Format of dictionary_filter.tsv