japanese-highlighter.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. # A japanese highlighter of different japanese letter formats
  3. import tkinter as tk
  4. JAPANINPUT = "失せろ、くそオーストラリア人め。誰が貴様を払うかよ (⁠-⁠_⁠-⁠メ⁠)"
  5. hiragana = set([
  6. "あ", "い", "う", "え", "お",
  7. "か", "き", "く", "け", "こ",
  8. "さ", "し", "す", "せ", "そ",
  9. "た", "ち", "つ", "て", "と",
  10. "な", "に", "ぬ", "ね", "の",
  11. "は", "ひ", "ふ", "へ", "ほ",
  12. "ま", "み", "む", "め", "も",
  13. "や", "ゆ", "よ",
  14. "ら", "り", "る", "れ", "ろ",
  15. "わ", "を", "ん"
  16. ])
  17. katakana = set([
  18. "ア", "イ", "ウ", "エ", "オ",
  19. "カ", "キ", "ク", "ケ", "コ",
  20. "サ", "シ", "ス", "セ", "ソ",
  21. "タ", "チ", "ツ", "テ", "ト",
  22. "ナ", "ニ", "ヌ", "ネ", "ノ",
  23. "ハ", "ヒ", "フ", "ヘ", "ホ",
  24. "マ", "ミ", "ム", "メ", "モ",
  25. "ヤ", "ユ", "ヨ",
  26. "ラ", "リ", "ル", "レ", "ロ",
  27. "ワ", "ヲ", "ン"
  28. ])
  29. numberOfData = {
  30. "kanji": 0,
  31. "hiragana": 0,
  32. "katakana": 0,
  33. "other": 0
  34. }
  35. def Rounder(number, total):
  36. return f"{round((number / total) * 100)}%"
  37. def main():
  38. root = tk.Tk()
  39. root.title("Japanese Text Highlighter")
  40. #root.geometry('650x650')
  41. # Japanese Text Input
  42. textInput = tk.Text(root, wrap=tk.WORD, font=("Arial", 12), height = 5, width = 52)
  43. textInput.insert(tk.END, JAPANINPUT)
  44. textInput.pack()
  45. # Japanese Text Highlight
  46. textHighlight = tk.Text(root, wrap=tk.WORD, font=("Arial", 14), height=10, width = 52)
  47. textHighlight.tag_configure("hiragana", foreground="blue")
  48. textHighlight.tag_configure("katakana", foreground="green")
  49. textHighlight.tag_configure("kanji", foreground="red")
  50. #buttonInput = tk.Button(root, text="Enter", command=highlight_text(textHighlight, textInput.get(1.0, "end-1c")))
  51. def highlight_text():
  52. numberOfData = {
  53. "kanji": 0,
  54. "hiragana": 0,
  55. "katakana": 0,
  56. "other": 0,
  57. "total": 0,
  58. }
  59. text = textInput.get(1.0, "end-1c")
  60. textHighlight.delete(1.0, tk.END)
  61. textHighlight.insert(tk.END, text)
  62. print("Highlighting...")
  63. start_index = 1.0
  64. for char in text:
  65. if char in hiragana:
  66. tag = "hiragana"
  67. numberOfData["hiragana"] = numberOfData["hiragana"] + 1
  68. numberOfData["total"] = numberOfData["total"] + 1
  69. elif char in katakana:
  70. tag = "katakana"
  71. numberOfData["katakana"] = numberOfData["katakana"] + 1
  72. numberOfData["total"] = numberOfData["total"] + 1
  73. elif char.isascii() or char.isspace():
  74. start_index = textHighlight.index(f"{start_index} + 1 chars")
  75. numberOfData["other"] = numberOfData["other"] + 1
  76. numberOfData["total"] = numberOfData["total"] + 1
  77. continue
  78. else:
  79. tag = "kanji"
  80. numberOfData["kanji"] = numberOfData["kanji"] + 1
  81. numberOfData["total"] = numberOfData["total"] + 1
  82. end_index = textHighlight.index(f"{start_index} + 1 chars")
  83. textHighlight.tag_add(tag, start_index, end_index)
  84. start_index = end_index
  85. print(numberOfData)
  86. total = numberOfData["total"]
  87. kanjiLabel.config(text=f"Kanji: {Rounder(numberOfData['kanji'], total)}")
  88. hiraganaLabel.config(text=f"Hiragana: {Rounder(numberOfData['hiragana'], total)}")
  89. katakanaLabel.config(text=f"Katakana: {Rounder(numberOfData['katakana'], total)}")
  90. otherLabel.config(text=f"Other: {Rounder(numberOfData['other'], total)}")
  91. buttonInput = tk.Button(root, text="Highlight!", command=highlight_text)
  92. buttonInput.pack()
  93. textHighlight.pack(expand=1, fill=tk.BOTH)
  94. kanjiLabel = tk.Label(root, text = f"Kanji: {numberOfData['kanji']}")
  95. kanjiLabel.pack()
  96. hiraganaLabel = tk.Label(root, text = f"Hiragana: {numberOfData['hiragana']}")
  97. hiraganaLabel.pack()
  98. katakanaLabel = tk.Label(root, text = f"Katakana: {numberOfData['katakana']}")
  99. katakanaLabel.pack()
  100. otherLabel = tk.Label(root, text = f"Other: {numberOfData['other']}")
  101. otherLabel.pack()
  102. # Instructions
  103. Instructions = tk.Text(root, height = 5, width = 52)
  104. l = tk.Label(root, text = "Instructions")
  105. l.config(font =("Courier", 14), )
  106. instructionsText = """Red is for kanji
  107. Blue is for hiragana
  108. Green is for Katakana"""
  109. l.pack()
  110. Instructions.pack()
  111. Instructions.insert(tk.END, instructionsText)
  112. Instructions.config(state=tk.DISABLED)
  113. root.mainloop()
  114. if __name__ == "__main__":
  115. main()