Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

pyconjp2015_talk_Translation of Python Program__


Published on

Translation of Python Program into non-English Languages for Learners without English Proficiency,
a talk at Pycon Japan 2015, by Renyuan Lyu from Taiwan

Published in: Education
  • Be the first to comment

pyconjp2015_talk_Translation of Python Program__

  1. 1. 1 PyCon JP 2015 Renyuan Lyu 呂仁園 Yunghsin Kuo 郭詠欣 Translation of Python Programs into non-English Languages for Learners without English Proficiency Oct/11/日曜日 3:35 p.m.–4:05 p.m. in メディアホール/Media Hall Chang Gung Univ. Taiwan Visit my Blog for more Info
  2. 2. 2 Computer Programming for everybody! No matter Old or Young No matter Boys or Girls Picture from Pycon JP 2015 By Guido van Rossum
  3. 3. 3 Computer Programming for everybody? As long as she/he knows the English language!
  4. 4. 4 Is it possible that the English language is the first barrier for people to learn programming? In what language, you teach your kids the fundamental math or science?
  5. 5. Python Turtle Graphics in Traditional Chinese (Kanji) 傳統漢字の Python 龜作圖 5 Also appeared on Pycon APAC 2015 without technical detail This slide is here
  6. 6. 2 Main Points • Python Programming in non-English Language improves readability for non-native English speakers • A set of 18 Turtle Demo Programs were Translated into Traditional Chinese as an Example 6 The term “Traditional Chinese” in this presentation may sometimes be used interchangeably with “Kanji”.
  7. 7. Abstract • A set of 18 turtle demo programs has been translated into traditional Chinese (tc) • as an example to show the possibility to write Python code conveniently in non-English language. • To improve code clarity and readability • for non-native English speakers, according to Python PEP 3131. • To attract more people to learn programming • who are considered as less English proficiency. • Providing a full list of tc alias ( • for the official python turtle module. • Github Availability 7
  8. 8. 8 vblll63j/PyConApac2015_Booth11_en_ tc_jp.mp4?dl=0 A quick Glance of Demonstration
  9. 9. The motivation was partially from PEP 3131: "Supporting Non-ASCII Identifiers" • many people in the world not familiar with the English language – They’d like to define variables, functions and classes with names in their native languages • code clarity and maintainability of the code among speakers of that language improves. • Original from PEP 3131 : [] 9
  10. 10. One Glance at Python Code in English v.s. in Kanji from turtle import * print("Hello, this is turtle graphics.") for i in range(100): forward(100) left(100) from turtle_tc import * 印("哈囉,這是龜作圖。") for i in 範圍(100): 前進(100) 左轉(100) 10 The first impression of this kind of program: SHORTER in length, more compact, and more readable, if you understand kanji (漢字).
  11. 11. Ignition: UTF-8 as Source encoding • After version 3.0, the Python language has changed its source coding from ASCII to UNICODE (UTF-8) • This is quite significant because it will be possible that non-English characters can be used as identifiers, which contain names of variables, functions, classes and methods. Here are examples: >>>印 = print >>>範圍= range >>> 甲 = 100 >>> 某數 = 甲 - 10 11
  12. 12. An exception for translation: Python Keywords are NOT translated 12 >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] • Python keywords are usually common seen, short English functional words, not used as identifiers – The number of them is about 30, quite few!
  13. 13. A short example of Python in Kanji (漢字) 13 >>> 印= print >>> 範圍= range >>> 某字串= '你好,世界。' >>> 重複的次數= 10 >>> for 數 in 範圍(重複的次數): 印(某字串, 數) 你好,世界。 0 你好,世界。 1 你好,世界。 2 你好,世界。 3 你好,世界。 4 你好,世界。 5 你好,世界。 6 你好,世界。 7 你好,世界。 8 你好,世界。 9 >>> >>> 印刷= print >>> 範囲= range >>> 文字列= 'こんにちは世界!' >>> 繰り返し数= 10 >>> for 数 in 範囲(繰り返し数): 印刷(文字列, 数) こんにちは世界! 0 こんにちは世界! 1 こんにちは世界! 2 こんにちは世界! 3 こんにちは世界! 4 こんにちは世界! 5 こんにちは世界! 6 こんにちは世界! 7 こんにちは世界! 8 こんにちは世界! 9 >>>
  14. 14. A longer example • An example to find prime numbers within 100. • It can be read aloud, like a normal Chinese article. 14 ''' 本程式可以列出 100 以內的質數。 作者: 呂仁園,2015/03/04 ''' # 內建函數取中文別名 印= print 範圍= range # 自定函數由此開始 def 主程式(): 質數列= [] for 某數 in 範圍(2,101): if 某數為質數(某數): 質數列 += [某數] 印('質數列= ',質數列) def 甲整除乙(甲, 乙): if 甲%乙 == 0: return True else: return False def 某數為質數(x): 答案= True # 這是大膽假設,以下為小心求證 for n in 範圍(2, x): if 甲整除乙(x, n): 答案= False #答案在此逆轉 break return 答案 # 此為 True 或者 False # 開始執行 主程式() >>> 質數列= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] >>>
  15. 15. Python Module for Turtle Graphics • Turtle graphics is a term in computer graphics – [] – part of the original Logo programming language – by Wally Feurzig and Seymour Papert in 1966. • The Python Turtle module is an extended reimplementation • from the Python standard distribution since Python 2.5. 15
  16. 16. Turtle Demo in IDLE Shell • Starting from Python 3.4.2, a set of 18 turtle demo programs was promoted to appear in the main menu of IDLE Shell, just below Python Docs within the Help sub-memu. 16
  17. 17. A typical example • An example from the set of turtle demo programs: 17 陰 陽
  18. 18. Program Translation from turtle import * from turtle_tc import * def yin(radius, color1, color2): def 陰(半徑, 顏色1, 顏色2): width(3) 筆寬(3) color("black", color1) 顏色(黑, 顏色1) begin_fill() 開始填() circle(radius/2., 180) 畫圓(半徑/2., 180) circle(radius, 180) 畫圓(半徑, 180) left(180) 左轉(180) circle(-radius/2., 180) 畫圓(-半徑/2., 180) end_fill() 結束填() left(90) 左轉(90) up() 提筆() forward(radius*0.35) 前進(半徑*0.35) right(90) 右轉(90) down() 下筆() color(color1, color2) 顏色(顏色1, 顏色2) begin_fill() 開始填() circle(radius*0.15) 畫圓(半徑*0.15) end_fill() 結束填() left(90) 左轉(90) up() 提筆() backward(radius*0.35) 後退(半徑*0.35) down() 下筆() left(90) 左轉(90) def main(): def 主函數(): reset() 重設() yin(200, "black", "white") 陰(200, 黑, 白) yin(200, "white", "black") 陰(200, 白, 黑) ht() 藏龜() return "Done!" return "完成!" if __name__ == '__main__': if __name__ == '__main__': main() 主函數() mainloop() 主迴圈() 18 • Is that possible we translate those beautiful and well- coded programs? • The Chinese programs are obviously more readable for those who speak Chinese as their native language.
  19. 19. Readability counts • Anybody remember this Python’s Zen (禪)? 19 >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. ... ... >>>
  20. 20. If Readability really counts,… • Then, what can be more readable to write programs in your own native language, if the readers are those who use the same language, including myself, who read the programs the most frequently! 20
  21. 21. Translation of the whole set of 18 Turtle Demo programs 21
  22. 22. The File list of the whole set of 18 programs File path @ Windows Line number 163 60 133 59 109 139 120 80 227 55 62 182 113 87 64 55 66 50 Total line number 1824 22
  23. 23. 23 18 programs
  24. 24. Inside the turtle module ( • The class diagram of the turtle module 24 class _Screen class Turtle
  25. 25. 25 An analogy to the Scratch language (originated from MIT) Python Turtle Scratch class Turtle Sprite (貓精靈) class _Screen Stage (舞台)
  26. 26. • A simplified class diagram 26
  27. 27. Summary of the turtle module • File path (@ Windows) • Number of lines in source code – About 4000 lines – Rank 2 out of 160 python files in the standard library (Python 3.4.2) • 2 major classes • With the other 8 supporting classes – class Turtle • 80 methods • E.g., forward , backward , left , right , … – class _Screen • 34 methods • E.g., addshape, bgcolor, bgpic, clearscreen, … • 112 Top-level functions – All methods from class Turtle and class _Screen are redefine as the top-level functions with a default turtle and screen objects 27 Diving into improve my Python skills significantly!!
  28. 28. Alias of the turtle module in Traditional Chinese (tc, or zh-tw) • Upon the original turtle module,, we create an associated module called, which provides the alias in traditional Chinese (thus the subscript “_tc” ) for almost all identifiers (names) in 28 DownLoad @
  29. 29. 29 Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> dir() ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> len(dir()) 6 Step 1: start an IDLE session Let’s have some overview about and
  30. 30. 30 >>> from turtle import * >>> dir() ['Pen', 'RawPen', 'RawTurtle', 'Screen', 'ScrolledCanvas', 'Shape', 'Terminator', 'Turtle', 'TurtleScreen', 'Vec2D', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'addshape', 'back', 'backward', 'begin_fill', 'begin_poly', 'bgcolor', 'bgpic', 'bk', 'bye', 'circle', 'clear', 'clearscreen', 'clearstamp', 'clearstamps', 'clone', 'color', 'colormode', 'degrees', 'delay', 'distance', 'done', 'dot', 'down', 'end_fill', 'end_poly', 'exitonclick', 'fd', 'fillcolor', 'filling', 'forward', 'get_poly', 'get_shapepoly', 'getcanvas', 'getpen', 'getscreen', 'getshapes', 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'listen', 'lt', 'mainloop', 'mode', 'numinput', 'onclick', 'ondrag', 'onkey', 'onkeypress', 'onkeyrelease', 'onrelease', 'onscreenclick', 'ontimer', 'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'register_shape', 'reset', 'resetscreen', 'resizemode', 'right', 'rt', 'screensize', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setup', 'setworldcoordinates', 'setx', 'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'textinput', 'tilt', 'tiltangle', 'title', 'towards', 'tracer', 'turtles', 'turtlesize', 'undo', 'undobufferentries', 'up', 'update', 'width', 'window_height', 'window_width', 'write', 'write_docstringdict', 'xcor', 'ycor'] >>> len(dir()) 128 Step 2: import turtle 128-6 == 122, turtle added
  31. 31. 31 >>> from turtle_tc import * >>> dir() ['Pen', 'RawPen', 'RawTurtle', 'Screen', 'ScrolledCanvas', 'Shape', 'TK', 'Terminator', 'Turtle', 'TurtleScreen', 'Vec2D', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'addshape', 'back', 'backward', 'begin_fill', 'begin_poly', 'bgcolor', 'bgpic', 'bk', 'bye', 'circle', 'clear', 'clearscreen', 'clearstamp', 'clearstamps', 'clone', 'color', 'colormode', 'degrees', 'delay', 'distance', 'done', 'dot', 'down', 'end_fill', 'end_poly', 'exitonclick', 'fd', 'fillcolor', 'filling', 'forward', 'get_poly', 'get_shapepoly', 'getcanvas', 'getpen', 'getscreen', 'getshapes', 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'listen', 'lt', 'mainloop', 'mode', 'numinput', 'onclick', 'ondrag', 'onkey', 'onkeypress', 'onkeyrelease', 'onrelease', 'onscreenclick', 'ontimer', 'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'register_shape', 'reset', 'resetscreen', 'resizemode', 'right', 'rt', 'screensize', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setup', 'setworldcoordinates', 'setx', 'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'textinput', 'tilt', 'tiltangle', 'title', 'towards', 'tracer', 'turtles', 'turtlesize', 'undo', 'undobufferentries', 'up', 'update', 'width', 'window_height', 'window_width', 'write', 'write_docstringdict', 'xcor', 'x座標', 'ycor', 'y座標', '下筆', '下筆嗎', '下筆狀態', '中英對照表', '主迴圈', ' 亂取樣', '亂整數', '亂數', '亂選', '二維向量類', '位置', '假', '做完了', '傾斜', '傾斜角度', '再見', '前往', '前進', '加形狀', '半徑數', '印', ' 原生筆類', '原生龜類', '去到', '取回復暫存區的長度', '取多邊形', '取幕', '取幕寬', '取幕高', '取形', '取形狀', '取形狀多邊形', '取時間', '取畫布', '取筆', '取龜', '取龜列表', '可捲畫布類', '可見狀態', '右轉', '向上鍵', '向下鍵', '向右鍵', '向左鍵', '向量2D類', '向量類', '回家', '回家鍵', '回 復', '回復暫存區的個數', '回復暫存區的長度', '圓', '在幕點擊時', '在拖曳時', '在按下鍵時', '在按著鍵時', '在按鍵時', '在按鍵鬆開時', '在滑鼠拖曳龜時 ', '在滑鼠釋放龜時', '在滑鼠鍵點擊幕時', '在滑鼠鍵點擊時', '在滑鼠鬆開龜時', '在滑鼠點擊龜時', '在計時器若干毫秒之後', '在計時後', '在釋放時', '在鬆 開時', '在點擊幕時', '在點擊時', '在點擊時離開', '在點擊龜時', '填色', '填色狀態', '大小', '寫', '寬', '左轉', '幕大小', '幕寬', '幕類', '幕高', '座標x', '座標y', '座標系統', '延遲', '弧度', '弳度', '形', '形狀', '形狀大小', '形狀轉換', '形狀類', '後退', '戳印', '扭曲因子', '提筆', '方形 ', '是否下筆', '是否可見', '是否正在填色', '時間', '更新', '更新畫面', '朝向', '朝向xy', '標題', '模式', '橙', '橙色', '正在填色', '清除', '清除 幕', '清除蓋章', '清除蓋章群', '清除鍵', '灰', '灰色', '烏龜形狀', '無', '生一隻龜', '生龜', '畫圓', '畫點', '登記形狀', '白', '白色', '看時間', '真', '睡', '空白鍵', '窗寬', '窗高', '筆', '筆大小', '筆寬', '筆屬性', '筆粗', '筆粗細', '筆色', '筆類', '等待閉幕', '等時間', '範圍', '紅', ' 紅色', '紫', '紫色', '結束填', '結束填色', '結束多邊形', '綠', '綠色', '聽', '聽鍵盤', '背景圖', '背景色', '脫離鍵', '色模式', '蓋印', '蓋章', ' 藍', '藍色', '藏', '藏龜', '複製', '角度', '角度從北開始順時針', '設x座標', '設y座標', '設位置', '設傾斜角度', '設傾角', '設取扭曲因子', '設回復暫 存區', '設圓為2pi弧', '設圓為360度', '設座標x', '設座標y', '設座標系統', '設成可伸縮模式', '設標題', '設立', '設角為度', '設角為弧', '設角的單位為 半徑數', '設角的單位為角度', '設頭向', '註冊形狀', '距離', '輸入數字', '輸入文字', '追蹤', '追蹤器', '追蹤更新畫面', '速度', '進入主迴圈', '重設', '重設大小模式', '重設幕', '重設幕大小', '重設幕寬高', '重設所有龜', '閉幕', '開始填', '開始填色', '開始多邊形', '開幕', '隨機取樣', '隨機整數', '隨 機數', '隨機選', '隱藏', '離開在點擊時', '青', '青色', '頭向', '顏色', '顯', '顯示', '顯龜', '顯龜嗎', '黃', '黃色', '黑', '黑色', '點', '點擊X 結束', '龜列表', '龜大小', '龜幕基類', '龜幕類', '龜形', '龜筆類', '龜群', '龜行類', '龜類'] >>> len(dir()) 368 Step 3: after downloading turtle_tc, import it 368-128 == 240 , turtle_tc added
  32. 32. Let’s jump into for more detail 32
  33. 33. Alias identifiers 龜幕基類= TurtleScreenBase 烏龜螢幕地基類= TurtleScreenBase 龜幕類= TurtleScreen 烏龜螢幕類= TurtleScreen 龜行類= TNavigator 烏龜航行類= TNavigator 龜筆類= TPen 烏龜畫筆類= TPen 原龜類= RawTurtle 粗龜類= RawTurtle 原生龜類= RawTurtle _幕類= _Screen _螢幕類= _Screen 幕類= Screen 螢幕類= Screen 開幕= Screen 龜類= Turtle 烏龜類= Turtle 33 class TurtleScreen(TurtleScreenBase): 加形狀= addshape 背景色= bgcolor 背景圖= bgpic 清除= clear 清除幕= clearscreen 色模式= colormode 延遲= delay 取畫布= getcanvas : : class TPen(object): 筆粗= pensize 筆粗細= pensize 筆大小= pensize 筆寬= width 寬= width 提筆= penup 下筆= pendown : class TNavigator(object): 重設= reset 前進= forward 後退= back 右轉= right 左轉= left 位置= pos 前往= goto : • A partial list of the alias identifiers in
  34. 34. def x座標(): ... def y座標(): ... def 下筆(): ... def 下筆嗎(): ... def 下筆狀態(): ... def 位置(): ... def 傾斜(): ... def 傾斜角度(): ... def 前往(): ... def 前進(): ... def 半徑數(): ... def 去到(): ... def 點(): ... def 龜大小(): ... 34 def 主迴圈(): ... def 做完了(): ... def 再見(): ... def 加形狀(): ... def 取幕寬(): ... def 取幕高(): ... def 取形(): ... def 取形狀(): ... def 取畫布(): ... def 取龜列表(): ... def 在幕點擊時(): ... def 重設所有龜(): ... def 閉幕(): ... def 離開在點擊時(): ... def 點擊X結束(): ... def 龜列表(): ... def 龜群(): ... • A partial list of the alias identifiers of top-level functions within
  35. 35. 35 Aliasing In Class-level (Hacking the source code??)
  36. 36. 36 Aliasing in method level (within class) (Hacking the source code??)
  37. 37. 37 Automatic code generation • In, the aliasing procedure was not hard coded manually, but an automatic code generation mechanism was adopted. • The only thing we have to do is to translate all the names and keep them as an external file. • This makes it more convenient to transfer into another language.
  38. 38. 38 ey= eval(y) aClass= ip.getsource(ey) cList= 'cList'+y ec= eval(cList) aClassL=[] bClassL=[] cClassL=[] print(aClass) for x in ec[1:]: for n in range(1,len(x)): bClass= ' '*4+x[n]+'= '+x[0]+'n' # # 物類 內, 有 4 個空白 # aClass+= bClass bClassL+= [bClass] print(aClass) exec(aClass) cListTPen= [ ('TPen', '龜筆類', '烏龜畫筆類'), ('pensize', '筆粗', '筆粗細', '筆大小'), ('width', '筆寬', '寬'), ('penup', '提筆'), ('pendown', '下筆'), ('showturtle', '顯龜','顯示','顯'), ('hideturtle', '藏龜','隱藏','藏'), ('color', '顏色'), ('pencolor', '筆色'), ('speed', '速度'), ('pen', '筆', '筆屬性'), ('fillcolor', '填色'), y= 'Tpen’ ThetranslationfileAutomaticcodegeneration Generatedcodetoberunonline
  39. 39. An extra important issue: Providing on-line help • A document file should also be provided to on-line help available. 39 >>> help(前進) Help on function 前進 in module turtle_tc: 前進(distance) 『0053 中文說明』 龜前進指定的距離。 別名: 前進 | forward | fd 參數: 距離, distance - 一個數字(整數或浮點數) 龜前進指定的距離, 往龜的頭之方向。 示例(物件名為「小龜」的實例): >>> from turtle_tc import * >>> 小龜= 龜類() >>> 小龜.位置() (0.00,0.00) >>> 小龜.前進(25) >>> 小龜.位置() (25.00,0.00) >>> 小龜.前進(-75) >>> 小龜.位置() (-50.00,0.00)
  40. 40. Demo • On Youtube: – • On NBViewer – s/4n70b5e82cy74n4/tesing_turtle_tc.ipynb# 40
  41. 41. 41 Submitted by eah13 1whoa I love the single-character self 3 我 (wo3) would probably be three keystrokes: two for "w" and "o", then enter/spacebar to confirm substitution into the Chinese character. 7 me too. Japanese (two character) is pretty cool too: 自己 . I'm thinking using characters would be a cool way to slim down a programming language, a bit like how greek and russian characters are used in math. 8 interestingly, that's self in chinese too. wo3 means I, while what you wrote literally means self. 9I'm going to tattoo those Chinese characters on my arm. An International Discussion Forum about this Task “A Python program written with 90% Traditional Chinese characters” And more ....
  42. 42. Conclusion • We teach Reading, Writing, and Arithmetic to kids in our native or official languages, • which are usually not English in many countries – E.g., in the APAC area. • Why not try to teach kids programming • in the same language with which they have been natively familiar. 42
  43. 43. Reference • [1] The whole set of 18 turtle demo programs • ee/master/tcExamples • [2] Demo on youtube • • [3] GitHub • • [4] A Discussion Forum about this Task • “A Python program written with 90% Traditional Chinese characters” – ten_with_90_traditional/ 43
  44. 44. 44 PyCon JP 2015 Renyuan Lyu 呂仁園 Thank you for Listening. ご清聴 有り難う 御座いました。 真心 感謝 您的收聽。 Yunghsin Kuo 郭詠欣 Translation of Python Programs into non-English Languages for Learners without English Proficiency Oct/11/日曜日 3:35 p.m.–4:05 p.m. in メディアホール/Media Hall Visit my Blog for more Info