Repository navigation
Expand file tree
/
Copy pathlearn_jp_pyconus.py
More file actions
109 lines (77 loc) · 2.92 KB
/
Copy pathlearn_jp_pyconus.py
File metadata and controls
109 lines (77 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import jaconv
import streamlit as st
from sudachipy import Dictionary
from kanji_reading_with_level import add_reading as add_reading_with_level
tokenizer = Dictionary().create()
def make_ruby(text: str, rt: str) -> str:
"""Generater Ruby tagged text"""
return f"<ruby>{text}<rt>{rt}</rt></ruby>"
def kana_with_romaji_ruby(text: str) -> str:
hiragana = jaconv.kata2hira(text)
romaji = jaconv.kana2alphabet(hiragana)
return make_ruby(text, romaji)
def kana_to_romaji():
st.write("""## Hiragana and Katakana to Romaji
パイコン あめりか
""")
text = st.text_input("Hiragana and Katakana texts:")
if text:
result = kana_with_romaji_ruby(text)
st.write(f"#### {result}", unsafe_allow_html=True)
def word_segmentation():
st.write("""## Word segmentation
すもももももももものうち
""")
text = st.text_input("Text for Word segmentation:")
if text:
words = []
for token in tokenizer.tokenize(text):
words.append(kana_with_romaji_ruby(str(token)))
result = " / ".join(words)
st.write(f"#### {result}", unsafe_allow_html=True)
def add_reading(text: str) -> tuple[str, str]:
"""Add Hiranaga ruby to text"""
hiragana = ""
romaji = ""
for token in tokenizer.tokenize(text):
ruby_kana = jaconv.kata2hira(token.reading_form())
hiragana += make_ruby(token, ruby_kana) + " "
ruby_romaji = jaconv.kata2alphabet(token.reading_form())
romaji += make_ruby(token, ruby_romaji) + " "
return hiragana, romaji
def kanji_reading():
st.write("""## Kanji Reading
一人の日本人の大人が一人前のラーメンを食べる
""")
text = st.text_input("Text with Kanji:")
if text:
hiragana, romaji = add_reading(text)
st.write(f"#### {hiragana}", unsafe_allow_html=True)
st.write(f"#### {romaji}", unsafe_allow_html=True)
def kanji_reading_with_level():
st.write("""## Kanji Reading with Kanji level
日本語を勉強する
""")
st.markdown("[The Python Tutorial](https://docs.python.org/3.13/tutorial/index.html)")
levels = {"N1": "1", "N2": "2", "N3": "3", "N4": "4", "N5": "5"}
text = st.text_input("Text:")
alphabet = st.toggle("Alphabet(romaji) annotation(default: Hiragana)")
level = st.pills("Kanji level", levels, selection_mode="single")
if text:
text = text.replace(" ", "")
level_value = levels.get(level)
result = add_reading_with_level(text, level_value, alphabet)
result = result.replace("\n", " ")
st.write(f"#### {result}", unsafe_allow_html=True)
def main():
st.title("Learn Japanese🇯🇵 with Python🐍")
st.write("PyCon US 2025 ver.")
kana_to_romaji()
st.write("------")
word_segmentation()
st.write("------")
kanji_reading()
st.write("------")
kanji_reading_with_level()
if __name__ == "__main__":
main()