jp_text_parsing/get_readings2.py

68 lines
2.2 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from janome.tokenizer import Tokenizer
from kotobase import Kotobase
kb = Kotobase()
tokenizer = Tokenizer()
with open("words.json", "r", encoding="utf-16le") as f:
words = json.load(f)
# Generate Hiragana and Katakana characters
hiragana = tuple(chr(i) for i in range(12353, 12436)) # \u3041 - \u3094
katakana = tuple(chr(i) for i in range(12450, 12532)) # \u30A2 - \u30F4
punctuation = tuple(chr(i) for i in range(65281, 65382))
# Combine both into one tuple
all_kana = hiragana + katakana + punctuation + (' ', '', '')
with open("dump.json", "r", encoding="utf-16le") as f:
dump = json.load(f)
readings = {}
## Iterates through all the keys not values
for cd in dump:
word = ''
for char in dump[cd]['nm'] + '': ## hacky char add to prevent dupe logic
if char in all_kana:
if len(word) > 0:
#print(word)
if readings.get(word) is None:
kana = None
try:
result = kb.lookup(word)
if result:
if len(result.entries) > 0:
kana = result.entries[0].kana[0]
print (result.word, ": ", kana)
readings[word] = {"kana":kana}
else:
print("no entries")
for k in result.kanji:
if len(k.kunyomi) > 0:
print(k.kunyomi[0])
except TypeError as e:
kana = None
#words[word] = (words[word] + 1)
word = ''
else:
word += char
## Might need catch for end of string still
print()
# for word in words:
# result = kb.lookup(word)
# if result:
# if len(result.entries) > 0:
# kana = result.entries[0].kana[0]
# print (result.word, ": ", kana)
# words[word] = {"kana":kana,"count":words[word]}
with open("words-readings.json", "w", encoding="utf-16le") as outfile:
json.dump(readings, outfile, ensure_ascii=False, indent=2)
print()