├── dictionary ├── Readme.md ├── cha.py └── dictionary_run.py ├── README.md ├── love_curve.py ├── rock_paper_scissors.py ├── clock.py └── peppa_pig.py /dictionary/Readme.md: -------------------------------------------------------------------------------- 1 | # 可以导入自己的单词本的词典 2 | 3 | ### 运行 4 | python dictionary_run.py -v 1 -rd -cn -sp -f xxx.txt[自己的词本] -pre "XXX" [错词本前缀] 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 关于 2 | 短视频中使用的代码。 3 | 4 | ### 1 石头、剪刀、布的游戏 5 | rock_paper_scissors.py 6 | 7 | 8 | ### 2 Python笛卡尔的心形曲线 9 | love_curve.py 10 | 11 | ### 3 Python画时钟 12 | clock.py 13 | 14 | ### 4 Python画小猪佩奇 15 | peppa_pig.py 16 | 17 | ### 5 可以添加自己单词本的词典 18 | dictionary/ 19 | 20 | 21 | 22 | 23 | 24 | # 抖音搜索“程序媛安娜”观看样例 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /love_curve.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | T = np.linspace(0 , 2 * np.pi, 1024) 5 | ax = plt.subplot(111, projection='polar') 6 | ax.plot(T, 1. - np.sin(T),color="r") 7 | ax.set_rmax(2) 8 | ax.set_rticks([0.5, 1, 1.5, 2]) 9 | ax.set_rlabel_position(45) 10 | ax.grid(True) 11 | ax.set_title("Descartes' Love Curve") 12 | plt.show() 13 | -------------------------------------------------------------------------------- /rock_paper_scissors.py: -------------------------------------------------------------------------------- 1 | import random 2 | import os 3 | import re 4 | os.system('cls' if os.name=='nt' else 'clear') 5 | while (1 < 2): 6 | print ("\n") 7 | print ("石头,剪刀,布 - 游戏开始!") 8 | userChoice = input("选择你的武器 [R]石头, [P]布, or [S]剪刀: ") 9 | if not re.match("[SsRrPp]", userChoice): 10 | print ("Please choose a letter:") 11 | print ("[R]石头, [S]剪刀 或者 [P]布.") 12 | continue 13 | 14 | weapon_dic = {'R':"石头", 'r':"石头", "S":"剪刀", "s": "剪刀", "P":"布", "p":"布"} 15 | print ("你选择了: " + weapon_dic[userChoice]) 16 | choices = ['R', 'P', 'S'] 17 | opponenetChoice = random.choice(choices) 18 | print ("机器人选择了: " + weapon_dic[opponenetChoice]) 19 | if opponenetChoice == str.upper(userChoice): 20 | print ("平手,再来吧,愚蠢的人类! ") 21 | 22 | elif opponenetChoice == 'R' and userChoice.upper() == 'S': 23 | print ("我赢了,愚蠢的人类! ") 24 | continue 25 | elif opponenetChoice == 'S' and userChoice.upper() == 'P': 26 | print ("我赢了, 愚蠢的人类! ") 27 | continue 28 | elif opponenetChoice == 'P' and userChoice.upper() == 'R': 29 | print ("我赢了, 愚蠢的人类! ") 30 | continue 31 | else: 32 | print ("你赢了,这局我大意了!") 33 | -------------------------------------------------------------------------------- /clock.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | import datetime 3 | screen = turtle.Screen() 4 | screen.title('Clock ') 5 | screen.setup(1000,800) 6 | screen.setworldcoordinates(-1000,-1000,1000,1000) 7 | screen.tracer(0,0) 8 | screen.bgcolor('sky blue') 9 | 10 | class clock: 11 | def __init__(self,hour,minute,second): 12 | self.hour, self.minute, self.second = hour, minute, second 13 | self.face = turtle.Turtle() 14 | self.hand = turtle.Turtle() 15 | self.face.hideturtle() 16 | self.hand.hideturtle() 17 | 18 | def draw(self): 19 | self.draw_face() 20 | self.draw_hand() 21 | 22 | def draw_face(self): 23 | self.face.clear() 24 | self.face.up() 25 | self.face.goto(0,-700) 26 | self.face.pensize(5) 27 | self.face.down() 28 | self.face.fillcolor('white') 29 | self.face.begin_fill() 30 | self.face.circle(700) 31 | self.face.end_fill() 32 | self.face.up() 33 | self.face.goto(0,0) 34 | self.face.dot(10) 35 | self.face.pensize(2) 36 | for angle in range(0,360,6): 37 | self.face.up() 38 | self.face.goto(0,0) 39 | self.face.seth(90-angle) 40 | self.face.fd(620) 41 | self.face.down() 42 | self.face.fd(30) 43 | self.face.pensize(4) 44 | for angle in range(0,360,30): 45 | self.face.up() 46 | self.face.goto(0,0) 47 | self.face.seth(90-angle) 48 | self.face.fd(600) 49 | self.face.down() 50 | self.face.fd(50) 51 | 52 | def draw_hand(self): 53 | self.hand.clear() 54 | self.hand.up() 55 | self.hand.goto(0,0) 56 | self.hand.seth(90-self.hour%12*360//12) 57 | self.hand.down() 58 | self.hand.color('black') 59 | self.hand.pensize(6) 60 | self.hand.fd(300) 61 | 62 | self.hand.up() 63 | self.hand.goto(0,0) 64 | self.hand.seth(90-self.minute*6) 65 | self.hand.down() 66 | self.hand.color('black') 67 | self.hand.pensize(4) 68 | self.hand.fd(400) 69 | 70 | self.hand.up() 71 | self.hand.color('red') 72 | self.hand.goto(0,0) 73 | self.hand.dot(5) 74 | self.hand.seth(90-self.second*6) 75 | self.hand.down() 76 | self.hand.pensize(2) 77 | self.hand.fd(600) 78 | 79 | def animate(): 80 | global c 81 | d = datetime.datetime.now() 82 | c.hour, c.minute, c.second = d.hour, d.minute, d.second 83 | c.draw_hand() 84 | screen.update() 85 | screen.ontimer(animate,1000) 86 | 87 | d = datetime.datetime.now() 88 | c = clock(d.hour,d.minute,d.second) 89 | c.draw_face() 90 | screen.update() 91 | while True: 92 | animate() 93 | 94 | -------------------------------------------------------------------------------- /dictionary/cha.py: -------------------------------------------------------------------------------- 1 | #coding=gbk 2 | import requests 3 | import bs4 as bs 4 | import argparse 5 | 6 | YOUDAO_API = 'http://dict.youdao.com/search?q=%s&keyfrom=dict.index' 7 | USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; WOW64) ' \ 8 | 'AppleWebKit/537.36 (KHTML, like Gecko) ' \ 9 | 'Chrome/46.0.2490.71 Safari/537.36' 10 | YOUDAO = 'http://dict.youdao.com/' 11 | INPUT_FILE = 'input.txt' 12 | OUTPUT_FILE = 'output.txt' 13 | 14 | def make_parser(): 15 | parser = argparse.ArgumentParser() 16 | parser.add_argument( 17 | "-v", 18 | "--interval-time", 19 | help="the interval time (seconds) between two words (default: 1)", 20 | type=int, 21 | default=1) 22 | 23 | parser.add_argument( 24 | "-f", 25 | "--file", 26 | help="specify the origin of words (default: words.txt)", 27 | type=str, 28 | default="words.txt") 29 | return parser 30 | 31 | def get_interp(word,p=0): 32 | result = [] 33 | r = requests.get(YOUDAO_API % word, 34 | headers={ 35 | 'referer': YOUDAO, 36 | 'user-agent': USER_AGENT 37 | }) 38 | b = bs.BeautifulSoup(r.content) 39 | #print(b) 40 | lis = b.find('div', {'class':'trans-container'}) 41 | #print(lis) 42 | if lis is not None: 43 | for i in lis.findAll('li'): 44 | result.append(i.text) 45 | exams = [] 46 | phones = [] 47 | try: 48 | phonetics = b.findAll('span',{'class':"phonetic"}) 49 | if phonetics is not None: 50 | for ph in phonetics: 51 | phones.append(ph.text) 52 | #print(phones) 53 | examples = b.findAll('a',{'class':'sp dictvoice voice-js log-js'}) 54 | if examples is not None: 55 | for i in examples: 56 | if "dict.main.blng" in str(i): 57 | a = i.get('data-rel') 58 | if a is not None: 59 | e = " ".join( a.split("+")) 60 | exams.append(e.strip("&le=eng")) 61 | except: 62 | pass 63 | if p == 1: 64 | if len(result) == 0 and word != "": 65 | print(word) 66 | #print(word," "*20,result) 67 | #for i in range(len(exams)): 68 | # print(" "*25, exams[i]) 69 | #print() 70 | 71 | return result,exams,phones 72 | 73 | 74 | def file_itp(input_file, output_file): 75 | with open(input_file, 'r') as f: 76 | for each in f.readlines(): 77 | #print(each.split(' ')) 78 | word = each.strip("\n") 79 | #word ="good" 80 | count = 1 81 | make_parser() 82 | result = get_interp(word,p=1) 83 | ''' 84 | with open(output_file, 'a') as fi: 85 | fi.write('\n%s' % (word)) 86 | if len(result) == 0: 87 | fi.write('\t暂未找到') 88 | continue 89 | for i in xrange(len(result)): 90 | try: 91 | fi.write('\t%d. %s\n' % (i+1, result[i].encode('gbk'))) 92 | except Exception as e: 93 | print (word, e.message) 94 | fi.write('\t暂未得到') 95 | ''' 96 | 97 | 98 | if __name__ == '__main__': 99 | parser = make_parser() 100 | args = parser.parse_args() 101 | INPUT_FILE = args.file 102 | file_itp(INPUT_FILE, OUTPUT_FILE) 103 | -------------------------------------------------------------------------------- /peppa_pig.py: -------------------------------------------------------------------------------- 1 | from turtle import * 2 | 3 | 4 | def head(x, y): # head 5 | color((255, 155, 192), "pink") 6 | penup() 7 | goto(x, y) 8 | setheading(0) 9 | pendown() 10 | begin_fill() 11 | setheading(180) 12 | circle(300, -30) 13 | circle(100, -60) 14 | circle(80, -100) 15 | circle(150, -20) 16 | circle(60, -95) 17 | setheading(161) 18 | circle(-300, 15) 19 | penup() 20 | goto(-100, 100) 21 | pendown() 22 | setheading(-30) 23 | a = 0.4 24 | for i in range(60): 25 | if 0 <= i < 30 or 60 <= i < 90: 26 | a = a + 0.08 27 | lt(3.5) 28 | fd(a) 29 | else: 30 | a = a - 0.08 31 | lt(3) 32 | fd(a) 33 | end_fill() 34 | 35 | 36 | def ears(x, y): 37 | color((255, 155, 192), "pink") 38 | penup() 39 | goto(x, y) 40 | pendown() 41 | begin_fill() 42 | setheading(100) 43 | circle(-50, 50) 44 | circle(-10, 120) 45 | circle(-50, 54) 46 | end_fill() 47 | 48 | penup() 49 | setheading(90) 50 | forward(-12) 51 | setheading(0) 52 | forward(30) 53 | pendown() 54 | begin_fill() 55 | setheading(100) 56 | circle(-50, 50) 57 | circle(-10, 120) 58 | circle(-50, 56) 59 | end_fill() 60 | 61 | 62 | def eyes(): # eyes 63 | color((255, 155, 192), "white") 64 | penup() 65 | setheading(90) 66 | forward(-20) 67 | setheading(0) 68 | forward(-95) 69 | pendown() 70 | begin_fill() 71 | circle(15) 72 | end_fill() 73 | 74 | color("black") 75 | penup() 76 | setheading(90) 77 | forward(12) 78 | setheading(0) 79 | forward(-3) 80 | pendown() 81 | begin_fill() 82 | circle(3) 83 | end_fill() 84 | 85 | color((255, 155, 192), "white") 86 | penup() 87 | seth(90) 88 | forward(-25) 89 | seth(0) 90 | forward(40) 91 | pendown() 92 | begin_fill() 93 | circle(15) 94 | end_fill() 95 | 96 | color("black") 97 | penup() 98 | setheading(90) 99 | forward(12) 100 | setheading(0) 101 | forward(-3) 102 | pendown() 103 | begin_fill() 104 | circle(3) 105 | end_fill() 106 | 107 | 108 | def cheek(x, y): 109 | color((255, 155, 192)) 110 | penup() 111 | goto(x, y) 112 | pendown() 113 | setheading(0) 114 | begin_fill() 115 | circle(30) 116 | end_fill() 117 | 118 | 119 | def mouth(x, y): 120 | color(239, 69, 19) 121 | penup() 122 | goto(x, y) 123 | pendown() 124 | setheading(-80) 125 | circle(30, 40) 126 | circle(40, 80) 127 | 128 | 129 | def body(x, y): 130 | color((255, 99, 71)) 131 | penup() 132 | goto(x, y) 133 | pendown() 134 | begin_fill() 135 | setheading(-130) 136 | circle(100, 10) 137 | circle(300, 30) 138 | setheading(0) 139 | forward(230) 140 | setheading(90) 141 | circle(300, 30) 142 | circle(100, 3) 143 | color((255, 155, 192), (255, 100, 100)) 144 | setheading(-135) 145 | circle(-80, 63) 146 | circle(-150, 24) 147 | end_fill() 148 | 149 | 150 | def hands(x, y): 151 | color((255, 155, 192)) 152 | penup() 153 | goto(x, y) 154 | pendown() 155 | setheading(-160) 156 | circle(300, 15) 157 | penup() 158 | setheading(90) 159 | forward(15) 160 | setheading(0) 161 | forward(0) 162 | pendown() 163 | setheading(-10) 164 | circle(-20, 90) 165 | 166 | penup() 167 | setheading(90) 168 | forward(30) 169 | setheading(0) 170 | forward(237) 171 | pendown() 172 | setheading(-20) 173 | circle(-300, 15) 174 | penup() 175 | setheading(90) 176 | forward(20) 177 | setheading(0) 178 | forward(0) 179 | pendown() 180 | setheading(-170) 181 | circle(20, 90) 182 | 183 | 184 | def foot(x, y): 185 | pensize(10) 186 | color((240, 128, 128)) 187 | penup() 188 | goto(x, y) 189 | pendown() 190 | setheading(-90) 191 | forward(40) 192 | setheading(-180) 193 | color("black") 194 | pensize(15) 195 | fd(20) 196 | 197 | pensize(10) 198 | color((240, 128, 128)) 199 | penup() 200 | setheading(90) 201 | forward(40) 202 | setheading(0) 203 | forward(90) 204 | pendown() 205 | setheading(-90) 206 | forward(40) 207 | setheading(-180) 208 | color("black") 209 | pensize(15) 210 | fd(20) 211 | 212 | 213 | def tail(x, y): 214 | pensize(4) 215 | color((255, 155, 192)) 216 | penup() 217 | goto(x, y) 218 | pendown() 219 | seth(0) 220 | circle(70, 20) 221 | circle(10, 330) 222 | circle(70, 30) 223 | 224 | def nose(x, y): 225 | penup() 226 | goto(x, y) 227 | pendown() 228 | setheading(-32) 229 | begin_fill() 230 | a = 0.4 231 | for i in range(120): 232 | if 0 <= i < 30 or 60 <= i < 90: 233 | a = a + 0.08 234 | left(3) 235 | forward(a) 236 | else: 237 | a = a - 0.08 238 | left(3) 239 | forward(a) 240 | end_fill() 241 | 242 | penup() 243 | setheading(90) 244 | forward(25) 245 | setheading(0) 246 | forward(10) 247 | pendown() 248 | pencolor(255, 155, 192) 249 | setheading(10) 250 | begin_fill() 251 | circle(5) 252 | color(160, 82, 45) 253 | end_fill() 254 | 255 | penup() 256 | setheading(0) 257 | forward(20) 258 | pendown() 259 | pencolor(255, 155, 192) 260 | setheading(10) 261 | begin_fill() 262 | circle(5) 263 | color(160, 82, 45) 264 | end_fill() 265 | 266 | def setting(): 267 | pensize(4) 268 | hideturtle() 269 | colormode(255) 270 | color((255, 155, 192)) 271 | setup(840, 500) 272 | speed(10) 273 | 274 | 275 | 276 | setting() 277 | nose(-100, 100) 278 | head(-69, 167) 279 | ears(0, 160) 280 | eyes() 281 | cheek(80, 10) 282 | mouth(-20, 30) 283 | body(-32, -8) 284 | hands(-56, -45) 285 | foot(2, -177) 286 | tail(148, -155) 287 | done() 288 | 289 | 290 | 291 | -------------------------------------------------------------------------------- /dictionary/dictionary_run.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import random 3 | import time 4 | from collections import Iterable 5 | import sys 6 | import os 7 | #ffmpeg_path = "/usr/local/ffmpeg/bin/" 8 | #os.environ["PATH"] += os.pathsep + ffmpeg_path 9 | import numpy as np 10 | 11 | import requests 12 | import simpleaudio as sa 13 | from pydub import AudioSegment 14 | #AudioSegment.ffmpeg = "/usr/local/ffmpeg/bin" 15 | #AudioSegment.converter = "/usr/local/ffmpeg/bin/ffmpeg" 16 | #AudioSegment.ffmpeg = "/usr/local/ffmpeg/bin/ffmpeg" 17 | #AudioSegment.ffprobe ="/usr/local/ffmpeg/bin/ffprobe" 18 | from contextlib import contextmanager 19 | from os import chdir, getcwd, listdir, remove, makedirs 20 | from os.path import isfile, exists, join, expanduser 21 | import cha 22 | import sys 23 | 24 | def check_cache(f): 25 | def _wrapper(words): 26 | if not isinstance(words, Iterable): 27 | words = (words) 28 | for word in words: 29 | if not isfile(word + '.wav'): 30 | f([word]) 31 | return _wrapper 32 | 33 | 34 | def format_transfer(name, ori_format, target_format, remove_ori=False): 35 | """ori_format, target_format: only 'mp3' and 'wav' and supported""" 36 | try: 37 | song = getattr(AudioSegment, "from_" + ori_format)(name + "." + ori_format) 38 | song.export(name + "." + target_format, format=target_format) 39 | except: 40 | print("audio error " + name) 41 | pass 42 | #raise ValueError("Only 'mp3' and 'wav' format are supported") 43 | if remove_ori: 44 | remove(name + "." + ori_format) 45 | 46 | 47 | @check_cache 48 | def download_audio(words, target_format='wav'): 49 | num_words = len(words) 50 | index_i = 0 51 | for word in words: 52 | r = requests.get( 53 | url='http://dict.youdao.com/dictvoice?audio=' + word + '&type=1', 54 | stream=True) 55 | with open(word + '.mp3', 'wb+') as f: 56 | f.write(r.content) 57 | format_transfer(word, 'mp3', target_format, remove_ori=True) 58 | index_i += 1 59 | print("Download audio : ",word,num_words,index_i) 60 | 61 | def play_audio(audio, wait=False, sleep=0): 62 | wave_obj = sa.WaveObject.from_wave_file(audio) 63 | play_obj = wave_obj.play() 64 | if wait: 65 | play_obj.wait_done() 66 | 67 | 68 | def make_parser(): 69 | parser = argparse.ArgumentParser() 70 | parser.add_argument( 71 | "-v", 72 | "--interval-time", 73 | help="the interval time (seconds) between two words (default: 1)", 74 | type=int, 75 | default=1) 76 | 77 | parser.add_argument( 78 | "-f", 79 | "--file", 80 | help="specify the origin of words (default: words.txt)", 81 | type=str, 82 | default="words.txt") 83 | 84 | parser.add_argument( 85 | "-pre", 86 | "--prefix", 87 | help="specify the origin of words (default: words.txt)", 88 | type=str, 89 | default="") 90 | 91 | parser.add_argument( 92 | "-o", 93 | "--output", 94 | help="specify a file storing words with actual order (default ans.txt)", 95 | type=str, 96 | default="ans.txt") 97 | 98 | parser.add_argument( 99 | "-cd", 100 | "--cache-directory", 101 | help="specify the directory storing cache (default cache)", 102 | type=str, 103 | default="cache") 104 | 105 | parser.add_argument( 106 | "-rd", 107 | "--random", 108 | help="play words according to the random order", 109 | action="store_true", 110 | default=False) 111 | 112 | parser.add_argument( 113 | "-s", 114 | "--sort", 115 | help="sort all the words in alphabetical order", 116 | action="store_true", 117 | default=False) 118 | 119 | parser.add_argument( 120 | "-rs", 121 | "--reverse-sort", 122 | help="sort reversely all the words in alphabetical order", 123 | action="store_true", 124 | default=False) 125 | 126 | parser.add_argument( 127 | "-no", 128 | "--normal-order", 129 | help="play words according to order of the appearance in file (default)", 130 | action="store_true", 131 | default=True) 132 | 133 | parser.add_argument( 134 | "-ro", 135 | "--reverse-order", 136 | help="play words according to reverse order of the appearance in file", 137 | action="store_true", 138 | default=False) 139 | 140 | parser.add_argument( 141 | "-cn", 142 | "--chinese", 143 | help="play words according to reverse order of the appearance in file", 144 | action="store_true", 145 | default=False) 146 | 147 | parser.add_argument( 148 | "-sp", 149 | "--spell", 150 | help="play words according to reverse order of the appearance in file", 151 | action="store_true", 152 | default=False) 153 | parser.add_argument( 154 | "-tr", 155 | "--trans", 156 | help="play words according to reverse order of the appearance in file", 157 | action="store_true", 158 | default=False) 159 | parser.add_argument( 160 | "-c2e", 161 | "--cn2en", 162 | help="play words according to reverse order of the appearance in file", 163 | action="store_true", 164 | default=False) 165 | 166 | parser.add_argument( 167 | "-exam", 168 | "--exams", 169 | help="play words according to reverse order of the appearance in file", 170 | action="store_true", 171 | default=False) 172 | 173 | return parser 174 | 175 | @contextmanager 176 | def change_dir(target_path): 177 | """A function assisting change working directory temporarily 178 | 179 | >>> import os 180 | >>> os.chdir(os.path.expanduser('~')) 181 | >>> os.getcwd() == os.path.expanduser('~') # You're in your home directory now 182 | True 183 | >>> with change_dir('/usr/local'): # change working directory to '/usr/local' 184 | ... print(os.getcwd()) 185 | ... pass # Anything you want to do in this directory 186 | ... 187 | /usr/local 188 | >>> os.getcwd() == os.path.expanduser('~') # You're back in your previous working directory 189 | True 190 | 191 | """ 192 | current_path = getcwd() 193 | chdir(target_path) 194 | yield 195 | chdir(current_path) 196 | 197 | import pickle 198 | 199 | def save_obj(obj, name ): 200 | with open(name + '.pkl', 'wb') as f: 201 | pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL) 202 | 203 | def load_obj(name ): 204 | with open(name + '.pkl', 'rb') as f: 205 | return pickle.load(f) 206 | 207 | def translation_list(lst): 208 | print("start translation") 209 | result_dict = {} 210 | word_num = len(lst) 211 | result_dict = load_obj("translation_cache") 212 | '''for line in open("error_11.txt"): 213 | #print(line) 214 | lines = line.strip("\n").split(",") 215 | print(lines) 216 | if len(lines) == 2: 217 | word_key = lines[0] 218 | word_trans = [lines[1]] 219 | result_dict[word_key] = (word_trans,[]) 220 | print(result_dict)''' 221 | word_index = 0 222 | for word in lst: 223 | word_index += 1 224 | if word not in result_dict: 225 | result_dict[word] = cha.get_interp(word) 226 | if word in result_dict: 227 | if len(list(result_dict[word])) == 2: 228 | _,_,p = cha.get_interp(word) 229 | result_dict[word] = (result_dict[word][0],result_dict[word][1],p) 230 | print(word,result_dict[word]," ",word_index,"/",word_num) 231 | print("end translation!") 232 | save_obj(result_dict,"translation_cache") 233 | return result_dict 234 | 235 | if __name__ == '__main__': 236 | parser = make_parser() 237 | args = parser.parse_args() 238 | 239 | with open(args.file) as f: 240 | lst = f.readlines() 241 | lst = (item.strip() for item in lst) 242 | lst = [item for item in lst if item != ''] 243 | 244 | if args.random: 245 | random.shuffle(lst) 246 | elif args.reverse_order: 247 | lst = lst[::-1] 248 | elif args.sort or args.reverse_sort: 249 | lst = sorted(lst, reverse=args.reverse_sort) 250 | 251 | args.cache_directory = expanduser(args.cache_directory) 252 | if not exists(args.cache_directory): 253 | makedirs(args.cache_directory) 254 | input_words = [] 255 | error_words = [] 256 | chinese_error_words = [] 257 | error_count_dict = {} 258 | for line in open("awl_history_error_"+args.prefix): 259 | if len(line.strip("\n").split(",")) != 2: 260 | continue 261 | w_1, count_1 = line.strip("\n").split(",") 262 | error_count_dict[w_1] = int(count_1) 263 | 264 | with change_dir(args.cache_directory): 265 | now_num = 0 266 | error_num = 0 267 | download_audio(lst) 268 | translation_dict = translation_list(lst) 269 | for item,w in [(item+ '.wav',item) for item in lst]: 270 | try: 271 | print ("%s / %s ERROR %s"%(str(now_num) , str(len(lst)), str(error_num))) 272 | now_num += 1 273 | if not args.cn2en and not args.trans: 274 | play_audio(item) 275 | translation,e,ph = translation_dict[w] 276 | ph_str = "" 277 | if len(list(ph)) >= 1: 278 | ph_str = ph[0] 279 | 280 | if args.cn2en: 281 | print(translation) 282 | input_str = '' 283 | if args.spell: 284 | input_str = input("Enter your input: ") 285 | input_words.append(input_str) 286 | if args.spell and input_str.lower() != w.lower(): 287 | error_num += 1 288 | if w not in error_count_dict: 289 | error_count_dict[w] = 1 290 | else: 291 | error_count_dict[w] += 1 292 | 293 | #print ("\n\n Error : %s -> %s %s \n\n"%(input_str,w, ph_str)) 294 | print(" "*20,"ERROR") 295 | print(" "*20,w) 296 | print(" "*20,input_str) 297 | print(" "*20,ph_str) 298 | if args.exams == False: 299 | 300 | for i in range(3): 301 | play_audio(item) 302 | in_s = input("\n\n\nPlease correct your spell \n : ") 303 | if in_s.strip().lower() == w.lower(): 304 | print(" "*19,w) 305 | print(translation) 306 | for i in range(3): 307 | play_audio(item) 308 | input(" "*20) 309 | break 310 | #play_audio(item) 311 | #input_str = input(": ") 312 | error_words.append(w) 313 | if args.cn2en: 314 | play_audio(item) 315 | 316 | if args.chinese == False: 317 | print (w) 318 | else: 319 | ts = "" 320 | #translation,e = cha.get_interp(w) 321 | if args.trans: 322 | s ="\n"*10 + " "*10 + w + "\n"*10 323 | input_ts = input(s + " "*10 + ":") 324 | if input_ts not in " ".join(translation) and input_ts != "9": 325 | print("\n".join(e),"\n\n") 326 | input("GO..........") 327 | play_audio(item) 328 | 329 | print(w,ph_str,translation,"\n") 330 | print("\n".join(e),"\n\n") 331 | 332 | #input("Continue ..........") 333 | 334 | if args.trans: 335 | if input_ts not in " ".join(translation) and input_ts != "9": 336 | error_num += 1 337 | if w not in error_count_dict: 338 | error_count_dict[w] = 1 339 | else: 340 | error_count_dict[w] += 1 341 | error_num += 1 342 | chinese_error_words.append(w) 343 | #print("Error : ",input_ts,translation) 344 | if not args.spell or args.trans: 345 | time.sleep(args.interval_time) 346 | except: 347 | print("Error") 348 | pass 349 | if args.spell and len(error_words)!=0: 350 | error_rate = len(error_words)/len(lst) 351 | print("Error rate:", error_rate) 352 | t = time.strftime("%m%d%H%M", time.localtime()) 353 | print(args.file+"_"+ t) 354 | with open(args.file+"_"+ t, "w") as f: 355 | f.write("\n".join(error_words)) 356 | print("\n".join(error_words)) 357 | 358 | if args.trans and len(chinese_error_words)!=0: 359 | error_rate = len(chinese_error_words)/len(lst) 360 | print("Error rate:", error_rate) 361 | t = time.strftime("%m%d%H%M", time.localtime()) 362 | print(args.file+"_"+ t) 363 | with open(args.file+"_"+ t, "w") as f: 364 | f.write(str(error_rate)+"\n"+"\n".join(chinese_error_words)) 365 | print("\n".join(chinese_error_words)) 366 | with open("awl_history_error_"+args.prefix,"w") as f: 367 | for k,v in error_count_dict.items(): 368 | f.write(k+","+str(v)+"\n") 369 | if args.output: 370 | with open(args.output, 'w+') as f: 371 | f.write("\n".join(lst)) 372 | --------------------------------------------------------------------------------