Advertisement
us3

My basic IME

us3
Jul 15th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. --[[
  2. Japanese IME by me
  3. Disclaimer: I don't know that much Japanese, so if there are any errors please tell me. Also, I wouldn't want to do the Kanji because of the horrifying mess that are known as readings. I mean, I am used to really scary kanji (I use stuff like 嘅, 啲 on a very regular basis) but the readings just kill me since in my language there is usually one reading per character.
  4.  
  5. Also, I kind of need to relearn my lua since I am used to using 5.1 not 5.2.
  6.  
  7. This is mostly a proof of concept on how IMEs can be done. this is still a one-to-one mapping, but multiple mappings is probably out of my abilities.
  8.  
  9.  
  10. Usage guide:
  11.  
  12. In default, Japanese mode is enabled. If you want to switch to English mode, simply press tab.
  13.  
  14. Just type the Japanese you want. However, id you want to type katakana, you need to add q in front. In order to type a smaller character, please type l in front. so yes, qltsu will produce ッ. Kya, kyu, kyo and all these two character combos will not work yet (although that is in theory addable), and neither does any kanji. Also, please keep in mind that in order to type ー you should type xx.
  15.  
  16. Please note that if you don't press space after each kana, it will not work. For example, if you want to type にほんごでタイプしましょう, you would have to type "ni ho nn go de qta qi qpu shi ma shi lyo u". If you blend them together (like "nihonngo de taipu shi masho"), they will not work since my IME is too dumb.
  17.  
  18. Also, please note that deleting fullwidth characters do not exactly work right now. If you want to do so, please move the cursor one square so that it disappears but you know it's in the second half of the kana.
  19. ]]--
  20.  
  21. -- Required APIs
  22. local event = require("event")
  23. local table = require("table")
  24. local term = require("term")
  25. local keyboard = require("keyboard")
  26. local unicode = require("unicode")
  27.  
  28. local japanese = true
  29. -- The Unicodes for our characters
  30. local unitable={ la=0x3041, a=0x3042,
  31. li=0x3043, i=0x3044,
  32. lu=0x3045, u=0x3046,
  33. le=0x3047, e=0x3048,
  34. lo=0x3049, o=0x304A,
  35. ka=0x304B, ga=0x304C,
  36. ki=0x304D, gi=0x304E,
  37. ku=0x304F, gu=0x3050,
  38. ke=0x3051, ge=0x3052,
  39. ko=0x3053, go=0x3054,
  40. sa=0x3055, za=0x3056,
  41. si=0x3057, shi=0x3057, zi=0x3058, ji=0x3058,
  42. su=0x3059, zu=0x305A,
  43. se=0x305B, ze=0x305C,
  44. so=0x305D, zo=0x305E,
  45. ta=0x305F, da=0x3060,
  46. ti=0x3061, chi=0x3061, di=0x3062, dzi=0x3062,
  47. ltu=0x3063, tu=0x3064, ltsu=0x3063, tsu=0x3064, du=0x3065, dzu=0x3065,
  48. te=0x3066, de=0x3067,
  49. to=0x3068, xdo=0x3069,
  50. na=0x306A,
  51. ni=0x306B,
  52. nu=0x306C,
  53. ne=0x306D,
  54. no=0x306E,
  55. ha=0x306F, ba=0x3070, pa=0x3071,
  56. hi=0x3072, bi=0x3073, pi=0x3074,
  57. hu=0x3075, fu=0x3075, bu=0x3076, pu=0x3077,
  58. he=0x3078, be=0x3079, pe=0x307A,
  59. ho=0x307B, bo=0x307C, po=0x307D,
  60. ma=0x307E,
  61. mi=0x307F,
  62. mu=0x3080,
  63. me=0x3081,
  64. mo=0x3082,
  65. lya=0x3083, ya=0x3084,
  66. lyu=0x3085, yu=0x3086,
  67. lyo=0x3087, yo=0x3088,
  68. ra=0x3089,
  69. ri=0x308A,
  70. ru=0x308B,
  71. re=0x308C,
  72. ro=0x308D,
  73. lwa=0x308E,
  74. wa=0x308F,
  75. wi=0x3090,
  76. we=0x3091,
  77. wo=0x3092,
  78. nn=0x3093,
  79. vu=0x3094,
  80. lka=0x3095,
  81. lke=0x3096,
  82. qla=0x30A1, qa=0x30A2,
  83. qli=0x30A3, qi=0x30A4,
  84. qlu=0x30A5, qu=0x30A6,
  85. qle=0x30A7, qe=0x30A8,
  86. qlo=0x30A9, qo=0x30AA,
  87. qka=0x30AB, qga=0x30AC,
  88. qki=0x30AD, qgi=0x30AE,
  89. qku=0x30AF, qgu=0x30B0,
  90. qke=0x30B1, qge=0x30B2,
  91. qko=0x30B3, qgo=0x30B4,
  92. qsa=0x30B5, qza=0x30B6,
  93. qsi=0x30B7, qshi=0x30B7, qzi=0x30B8, qji=0x30B8,
  94. qsu=0x30B9, qzu=0x30BA,
  95. qse=0x30BB, qze=0x30BC,
  96. qso=0x30BD, qzo=0x30BE,
  97. qta=0x30BF, qda=0x30C0,
  98. qti=0x30C1, qchi=0x30C1, qdi=0x30C2, qdzi=0x30C2,
  99. qltu=0x30C3, qtu=0x30C4, qltsu=0x30C3, qtsu=0x30C4, qdu=0x30C5, qdzu=0x30C5,
  100. qte=0x30C6, qde=0x30C7,
  101. qto=0x30C8, qdo=0x30C9,
  102. qna=0x30CA,
  103. qni=0x30CB,
  104. qnu=0x30CC,
  105. qne=0x30CD,
  106. qno=0x30CE,
  107. qha=0x30CF, qba=0x30D0, qpa=0x30D1,
  108. qhi=0x30D2, qbi=0x30D3, qpi=0x30D4,
  109. qhu=0x30D5, qfu=0x30D5, qbu=0x30D6, qpu=0x30D7,
  110. qhe=0x30D8, qbe=0x30D9, qpe=0x30DA,
  111. qho=0x30DB, qbo=0x30DC, qpo=0x30DD,
  112. qma=0x30DE,
  113. qmi=0x30DF,
  114. qmu=0x30E0,
  115. qme=0x30E1,
  116. qmo=0x30E2,
  117. qlya=0x30E3, qya=0x30E4,
  118. qlyu=0x30E5, qyu=0x30E6,
  119. qlyo=0x30E7, qyo=0x30E8,
  120. qra=0x30E9,
  121. qri=0x30EA,
  122. qru=0x30EB,
  123. qre=0x30EC,
  124. qro=0x30ED,
  125. qlwa=0x30EE,
  126. qwa=0x30EF,
  127. qwi=0x30F0,
  128. qwe=0x30F1,
  129. qwo=0x30F2,
  130. qnn=0x30F3,
  131. qvu=0x30F4,
  132. qlka=0x30F5,
  133. qlke=0x30F6,
  134. xx=0x30FC
  135.  
  136. }
  137.  
  138. -- Defines temporary keystrokes
  139. temporarykeystrokes=""
  140.  
  141. function handleoutput()
  142. if temporarykeystrokes == "do" then
  143. temporarykeystrokes = "xdo"
  144. -- debug print("do changed to xdo")
  145. end
  146. for k,l in pairs(unitable) do
  147. if temporarykeystrokes == k then
  148. term.write(unicode.char(l))
  149. temporarykeystrokes=""
  150. return true
  151. end
  152. end
  153. temporarykeystrokes=""
  154. end
  155.  
  156. -- puts event listener
  157.  
  158. keydowntrigger = function (keyboardAddress, character, charcode, playerName)
  159. -- debug print("key down")
  160. if charcode == 203 then
  161. local x, y = term.getCursor()
  162. term.setCursor(x-1, y)
  163. end
  164. if charcode == 205 then
  165. local x, y = term.getCursor()
  166. term.setCursor(x+1, y)
  167. end
  168. if charcode == 14 then
  169. local x, y = term.getCursor()
  170. term.setCursor(x-1, y)
  171. term.write(" ")
  172. term.setCursor (x-1, y)
  173. return true
  174. end
  175. if charcode == 28 then
  176. term.write("\n")
  177. return true
  178. end
  179. if charcode == 15 then
  180. if japanese then
  181. japanese = false
  182. return true
  183. else
  184. japanese = true
  185. return true
  186. end
  187. end
  188. if charcode == 1 or charcode == 58 or charcode == 29 or charcode == 42 or charcode == 54 or charcode == 56 or (charcode > 58 and charcode < 71) or (charcode > 88 and charcode < 144) or (charcode > 148) then
  189. return false
  190. end
  191. if charcode == 57 and japanese then
  192. -- debug print("handle output")
  193. handleoutput()
  194. elseif (not japanese) or ((character > 31 and character < 65) or (character > 122 and character > 126)) then
  195. -- debug print("write char "..charcode.." "..character)
  196. term.write(unicode.char(character))
  197. else
  198. -- debug print("append keystrokes")
  199. temporarykeystrokes = temporarykeystrokes .. unicode.char(character)
  200. -- debug print(temporarykeystrokes)
  201. end
  202. end
  203.  
  204. while true do
  205. local event,kbadr,ch,co,pn = term.pull("key_down")
  206. keydowntrigger(kbadr,ch,co,pn)
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement