def query(keyword, use_db=True, use_api=False, play_voice=False, use_dict=True): update_word = [True] word = Word.get_word(keyword) result = {'query': keyword, 'errorCode': 60} if use_db and word: result.update(json.loads(word.json_data)) update_word[0] = False elif update_word[0]: # 从starditc中查找 if use_dict and config.config.get('stardict'): try: from lib.cpystardict import Dictionary except ImportError: from lib.pystardict import Dictionary colors = deque(['cyan', 'yellow', 'blue']) stardict_base = config.config.get('stardict') stardict_trans = [] for dic_dir in os.listdir(stardict_base): dic_file = os.listdir(os.path.join(stardict_base, dic_dir))[0] name, ext = os.path.splitext(dic_file) name = name.split('.')[0] dic = Dictionary(os.path.join(stardict_base, dic_dir, name)) try: dic_exp = dic[keyword.encode("utf-8")] except KeyError: pass else: dic_exp = unicode(dic_exp.decode('utf-8')) stardict_trans.append( colored( u"[{dic}]:{word}".format(dic=name, word=keyword), 'green')) color = colors.popleft() colors.append(color) stardict_trans.append(colored(dic_exp, color)) stardict_trans.append( colored(u'========================', 'magenta')) if stardict_trans: result['stardict'] = u'\n'.join(stardict_trans) result['errorCode'] = 0 # 从stardict中没有匹配单词 if not result['errorCode'] == 0: spider = YoudaoSpider(keyword) try: result.update(spider.get_result(use_api)) except requests.HTTPError, e: print colored(u'网络错误: %s' % e.message, 'red') sys.exit() # 更新数据库 new_word = word if word else Word() new_word.keyword = keyword new_word.json_data = json.dumps(result) new_word.save()
def update(): # 从0.2.0开始更改了数据库 # 重新设置数据库 if config.get('version', '0') < '0.2.0': # silent_remove(DB_DIR) from model import db, Word db.drop_table(Word, fail_silently=True) Word.create_table()
def update(): # 从0.2.0开始更改了数据库 # 重新设置数据库 if config.get('version', '0') < '0.2.0': # silent_remove(DB_DIR) from model import db, Word try: db.drop_table(Word, fail_silently=True) except AttributeError: pass Word.create_table()
def del_word(keyword): if keyword: try: word = Word.select().where(Word.keyword == keyword).get() word.delete_instance() print (colored(u"已删除{0}".format(keyword), "blue")) except Word.DoesNotExist: print (colored(u"没有找到{0}".format(keyword), "red")) config.silent_remove(os.path.join(config.VOICE_DIR, keyword + ".mp3")) else: count = Word.delete().execute() shutil.rmtree(config.VOICE_DIR, ignore_errors=True) print (colored(u"共删除{0}个单词".format(count), "blue"))
def update(): # 从0.2.0开始更改了数据库 # 重新设置数据库 if config.get('version', '0') < '0.2.0': # silent_remove(DB_DIR) from model import db, Word try: db.drops_table(Word, fail_silently=True) except: # drop_table -> drops_table # fail_silently didn't work pass Word.create_table()
def del_word(keyword): if keyword: try: word = Word.select().where(Word.keyword == keyword).get() word.delete_instance() print(colored(u'已删除{0}'.format(keyword), 'blue')) except Word.DoesNotExist: print(colored(u'没有找到{0}'.format(keyword), 'red')) config.silent_remove(os.path.join(config.VOICE_DIR, keyword + '.mp3')) else: count = Word.delete().execute() shutil.rmtree(config.VOICE_DIR, ignore_errors=True) print(colored(u'共删除{0}个单词'.format(count), 'blue'))
def add_word(): data = request.get_json() word = data.get('word') new_word = Word(word=word) db.session.add(new_word) db.session.commit() return 'word added!'
def post(self): result_hash = {"errors": []} word = None word_name = self.request.get('word') if not word_name: result_hash['errors'].append('word is empty') else: word = Word.get_or_insert_by_name(word_name) description_body = self.request.get('description') if not description_body: result_hash['errors'].append('description is empty') else: word.add_description(description_body) memcache.delete("words") memcache.delete("word-"+word_name) memcache.delete("index") logging.info("description add(%s, %s)" % (word.name, description_body)) result_hash['word'] = word.to_hash() if word and not word.descriptions(): logging.info("word delete(%s)" % (word.name)) word.delete() memcache.delete("words") redirect_to = self.request.get('redirect') if redirect_to: self.redirect(redirect_to) else: result = simplejson.dumps(result_hash, ensure_ascii=False) self.response.content_type = "application/json" self.response.out.write(result) return
def gTrans(query, destlanguage='en', prompterror=True): log.info( "Using Google translate to determine the unknown translation of %s", query) # No meanings if we don't have a query if query == None: return None # No meanings if our query is blank after stripping HTML out query = utils.striphtml(query) if query.strip() == u"": return None try: # Return the meanings (or lack of them) directly return lookup(query, destlanguage) except urllib2.URLError, e: # The only 'meaning' should be an error telling the user that there was some problem log.exception("Error while trying to obtain Google response") if prompterror: return [[ Word(Text('<span style="color:gray">[Internet Error]</span>')) ]] else: return None
def main(): reload(sys) sys.setdefaultencoding("utf-8") config.prepare() try: options, args = getopt.getopt(sys.argv[1:], 'anld:cvs:y', ['help']) except getopt.GetoptError: options = [('--help', '')] if ('--help', '') in options: show_help() return use_api = False use_db = True play_voice = False use_dict = True for opt in options: if opt[0] == '-a': use_api = True elif opt[0] == '-n': use_db = False elif opt[0] == '-l': show_db_list() sys.exit() elif opt[0] == '-d': del_word(opt[1]) sys.exit() elif opt[0] == '-c': del_word(None) sys.exit() elif opt[0] == '-v': play_voice = True elif opt[0] == '-s': if os.path.isdir(opt[1]): print u'stardict 路径设置成功:', opt[1] config.set_dict_path(opt[1]) else: print u'stardict 路径设置失败. 原因可能是路径"%s"不存在.' % opt[1] sys.exit() elif opt[0] == '-y': use_dict = False use_db = False SNAP = os.environ['SNAP'] print "SNAP: " + SNAP config.set_dict_path(SNAP + "/dicts") # keyword = unicode(' '.join(args), encoding=sys.getfilesystemencoding()) keyword = unicode(' '.join(args), encoding='utf-8') if not keyword: if play_voice: word = Word.get_last_word() keyword = word.keyword query(keyword, play_voice=True, use_db=True) else: show_help() else: query(keyword, use_db, use_api, play_voice, use_dict)
def display_selection(): """Stores and displays embedded video, transcript, and vocabulary of selected talk.""" key_word = request.args.get('key_word') title = request.args.get('title') slug = request.args.get('slug') talk_id = request.args.get('talk_id') video= get_video(slug) stored_transcript = Transcript.query.get(talk_id) #vocab_transcript: a string --used for parsing vocabulary #webpage_transcript: a dict --used display text in paragraph format if stored_transcript: webpage_transcript = get_webpage_transcript(slug) vocab_list = Word.query.filter_by(talk_id=talk_id).all() else: vocab_transcript = get_vocab_transcript(slug) #a string that get's stored for parsing Transcript.add_transcript(talk_id, slug, vocab_transcript, title) webpage_transcript = get_webpage_transcript(slug) # a dict of transcript paragraphs vocab_list = [] my_text = VocabFactory(vocab_transcript) my_text.get_vocab()#get_vocab()returns a list of tuple pairs: [(vocab, (attributes))] for vocab, attributes in my_text.vocab_list: #make sure each vocabulary is stored first stored_word = Word.query.filter_by(word = vocab, talk_id = talk_id).first() if stored_word: vocab_list.append(stored_word) else: vocab = vocab stem = attributes[0] freq = attributes[1] sentence = attributes[2] selection = attributes[3] word = Word.add_word(word=vocab, talk_id=talk_id, stem=stem, freq=freq, sentence=unicode(sentence, 'utf-8'), selection=selection) vocab_list.append(word) return render_template("display_selection.html", video = video, webpage_transcript = webpage_transcript, vocab_list = vocab_list, key_word = key_word, slug = slug, talk_id = talk_id, title = title)
def _add_translation_if_not_exist(word="", tr="", l1="", l2=""): language = Language(l1=l1, l2=l2) wordrow = Word.query.filter_by(word=word).first() if not wordrow: wordrow = Word(word=word, state="learning", language=language) db.session.add(wordrow) translation = Translation(translation=tr, word=wordrow) db.session.add(translation) db.session.commit()
def get(self): result = memcache.get("words") if not result: result = simplejson.dumps( {"words": [ w.name for w in Word.all().fetch(1000) ]}, ensure_ascii=False) memcache.set("words", result) self.response.content_type = "application/json" self.response.out.write(result) return
def section4(data, n, text, *etc): text = text.lower() text = long_words.get(text, text) word = make_object(data.vocabulary, Word, n) if word.text is None: # this is the first word with index "n" word.text = text else: # there is already a word sitting at "n", so create a synonym original = word word = Word() word.n = n word.text = text original.add_synonym(word) word.kind = ['travel', 'noun', 'verb', 'snappy_comeback'][n // 1000] if word.kind == 'noun': n %= 1000 obj = make_object(data.objects, Object, n) obj.names.append(text) obj.is_treasure = (n >= 50) data.objects[text] = obj if text not in data.vocabulary: # since duplicate names exist data.vocabulary[text] = word
def get(self): result = memcache.get("index") if not result: logging.info("cache not hit(index)") template_values = { 'words': Word.all().fetch(1000) } path = os.path.join(os.path.dirname(__file__), '..', 'view', 'index.html') result = template.render(path, template_values) memcache.set("index", result, 600) self.response.out.write(result)
def section4(data, n, text, *etc): text = text.lower() text = long_words.get(text, text) word = make_object(data.vocabulary, Word, n) if word.text is None: # this is the first word with index "n" word.text = text else: # there is already a word sitting at "n", so create a synonym original = word word = Word() word.n = n word.text = text original.add_synonym(word) word.kind = ["travel", "noun", "verb", "snappy_comeback"][n // 1000] if word.kind == "noun": n %= 1000 obj = make_object(data.objects, Object, n) obj.names.append(text) obj.is_treasure = n >= 50 data.objects[text] = obj if text not in data.vocabulary: # since duplicate names exist data.vocabulary[text] = word
def main(): config.prepare() try: options, args = getopt.getopt(sys.argv[1:], 'anld:cvs:yb:', ['help']) except getopt.GetoptError: options = [('--help', '')] if ('--help', '') in options: show_help() return use_api = False use_db = True play_voice = False use_dict = True for opt in options: if opt[0] == '-a': use_api = True elif opt[0] == '-n': use_db = False elif opt[0] == '-l': show_db_list() sys.exit() elif opt[0] == '-d': del_word(opt[1]) sys.exit() elif opt[0] == '-c': del_word(None) sys.exit() elif opt[0] == '-v': play_voice = True elif opt[0] == '-s': print u'stardict 路径设置成功:', opt[1] config.set_dict_path(opt[1]) sys.exit() elif opt[0] == '-y': use_dict = False use_db = False elif opt[0] == '-b': batch(opt[1], args) sys.exit() keyword = unicode(' '.join(args), encoding=sys.getfilesystemencoding()) if not keyword: if play_voice: word = Word.get_last_word() keyword = word.keyword query(keyword, play_voice=True, use_db=True) else: show_help() else: query(keyword, use_db, use_api, play_voice, use_dict)
def query(keyword, use_db=True, use_api=False, play_voice=False, use_dict=True): update_word = [True] word = Word.get_word(keyword) result = {"query": keyword, "errorCode": 60} if use_db and word: result.update(json.loads(word.json_data)) update_word[0] = False elif update_word[0]: # 从starditc中查找 if use_dict and config.config.get("stardict"): try: from lib.cpystardict import Dictionary except ImportError: from lib.pystardict import Dictionary colors = deque(["cyan", "yellow", "blue"]) stardict_base = config.config.get("stardict") stardict_trans = [] for dic_dir in os.listdir(stardict_base): dic_file = os.listdir(os.path.join(stardict_base, dic_dir))[0] name, ext = os.path.splitext(dic_file) name = name.split(".")[0] dic = Dictionary(os.path.join(stardict_base, dic_dir, name)) try: dic_exp = dic[keyword.encode("utf-8")] except KeyError: pass else: dic_exp = unicode(dic_exp.decode("utf-8")) stardict_trans.append(colored(u"[{dic}]:{word}".format(dic=name, word=keyword), "green")) color = colors.popleft() colors.append(color) stardict_trans.append(colored(dic_exp, color)) stardict_trans.append(colored(u"========================", "magenta")) if stardict_trans: result["stardict"] = u"\n".join(stardict_trans) result["errorCode"] = 0 # 从stardict中没有匹配单词 if not result["errorCode"] == 0: spider = YoudaoSpider(keyword) try: result.update(spider.get_result(use_api)) except requests.HTTPError, e: print colored(u"网络错误: %s" % e.message, "red") sys.exit() # 更新数据库 new_word = word if word else Word() new_word.keyword = keyword new_word.json_data = json.dumps(result) new_word.save()
def main(): config.prepare() try: options, args = getopt.getopt(sys.argv[1:], "anld:cvs:y", ["help"]) except getopt.GetoptError: options = [("--help", "")] if ("--help", "") in options: show_help() return use_api = False use_db = True play_voice = False use_dict = True for opt in options: if opt[0] == "-a": use_api = True elif opt[0] == "-n": use_db = False elif opt[0] == "-l": show_db_list() sys.exit() elif opt[0] == "-d": del_word(opt[1]) sys.exit() elif opt[0] == "-c": del_word(None) sys.exit() elif opt[0] == "-v": play_voice = True elif opt[0] == "-s": print u"stardict 路径设置成功:", opt[1] config.set_dict_path(opt[1]) sys.exit() elif opt[0] == "-y": use_dict = False use_db = False keyword = unicode(" ".join(args), encoding=sys.getfilesystemencoding()) if not keyword: if play_voice: word = Word.get_last_word() keyword = word.keyword query(keyword, play_voice=True, use_db=True) else: show_help() else: query(keyword, use_db, use_api, play_voice, use_dict)
def save_book_word(book: Book, word: str): db_word = Word(word=word, cnt=1) try: db_session.add(db_word) db_session.commit() except IntegrityError as e: db_session.rollback() db_session.query(Word).filter(Word.word == word).update( {Word.cnt: Word.cnt + 1}) db_word = db_session.query(Word).filter(Word.word == word).first() db_book_word = BookWord(book_id=book.id, word_id=db_word.id) try: db_session.add(db_book_word) db_session.commit() except IntegrityError as e: db_session.rollback()
def get_words_remaining(gid: str) -> List[Word]: """ Get a list of Words that have not yet been used in the current round """ query = f"SELECT word.* FROM word " \ f"WHERE word.gid = '{gid}' " \ f"AND NOT EXISTS " \ f"(SELECT 1 FROM attempt " \ f"INNER JOIN game ON game.id = attempt.gid " \ f"WHERE attempt.wid = word.id " \ f"AND game.round = attempt.round " \ f"AND attempt.success IS NOT NULL);" words = list() for word_args in conn.execute(query): words.append(Word(*word_args)) return words
def post_words(): category = request.form['category'] content = request.form['content'] if not category or not content: flash('내용이 비어있습니다.') return redirect(url_for('words')) word = Word(category=category, content=content) db.session.add(word) try: db.session.commit() except Exception as e: hairpin.logger.exception(e) flash('저장에 실패했습니다. 로그를 확인해주세요.') return redirect(url_for('words')) flash('성공적으로 저장했습니다.') return redirect(url_for('words', category=category))
def load_words(): """load words data""" ## printing the table? print("Words") Word.query.delete() with open("seed.data/vocabs.csv") as file: for row in file: row= row.rstrip() english, farsi_phonetic,farsi,_ = row.split(",") word = Word(english= english.lower(), farsi_phonetic=farsi_phonetic,farsi =farsi) db.session.add(word) db.session.commit()
def get(self): result_hash = {"errors": []} word_name = self.request.get('word') if not word_name: result_hash['errors'].append('word is empty') result = simplejson.dumps(result_hash, ensure_ascii=False) else: result = memcache.get("word-" + word_name) if not result: logging.info("cache not hit(%s)" % (word_name)) word = Word.get_by_name(word_name) if word: result_hash['word'] = word.to_hash() else: result_hash['errors'].append('word not found') result = simplejson.dumps(result_hash, ensure_ascii=False) memcache.set("word-" + word_name, result) self.response.content_type = "application/json" self.response.out.write(result) return
def main(): # resolve issue #9 reload(sys) sys.setdefaultencoding('utf-8') args = parse_args() config.prepare() if args.stardict: if os.path.isdir(args.stardict): config.set_dict_path(args.stardict) print('stardict 路径设置成功:{}'.format(args.stardict)) else: print('stardict 路径设置失败. 路径"%s"不存在.'.format(args.stardict)) return if args.list: show_db_list() return if args.clean: del_word(None) return if len(args.word) > 0: keyword = unicode(' '.join(args.word), encoding=sys.getfilesystemencoding()) else: word = Word.get_last_word() keyword = word.keyword if args.delete: del_word(keyword) return use_db = not args.new use_dict = not args.youdao play_voice = args.voice query(keyword, use_db, use_dict, play_voice)
def learn(ctx): words = Word.select().where((Word.stage == 0) & (Word.next_review == datetime.today())) if not len(words): click.echo(f"Your day is clear!") return for index, word in enumerate(words): click.echo(f"{index+1}/{len(words)} {word.en} {word.ch}") click.echo("now input this word three times!") correct_time = 0 while correct_time < 3: if click.prompt("") == word.en: correct_time += 1 click.echo(f"correct! {correct_time} count!") else: click.echo(f"Try Again!") click.echo("Good work!\n") click.echo("You've learned all the words!\n" "Now It's time to check how you remember them!\n" "Let's go to our first review process!\n") ctx.invoke(review, words)
def fetch_vocab(): vocab_transcript = request.args.get('vocab_transcript') vocab_list = [] for vocab, attributes in get_vocab(vocab_transcript): #get_vocab()returns a list of tuple pairs: (vocab, (attributes)) #need make sure each vocabulary is stored first stored_word = Word.query.filter_by(word = vocab, talk_id = talk_id).first() if stored_word: vocab_list.append(stored_word) else: vocab = vocab stem = attributes[0] freq = attributes[1] sentence = attributes[2] selection = attributes[3] word = Word.add_word(word=vocab, talk_id=talk_id, stem=stem, freq=freq, sentence=unicode(sentence, 'utf-8'), selection=selection) vocab_list.append(word) return jsonify({"vocab_list":vocab_list})
def seed_words_table(): uniques = total_corpus_list() for word in uniques: n = Word(word = word, city = city.name, probability = prob_word_given_city(city, word)) model.session.add(n) model.session.commit()
def show_db_list(): print colored(u'保存在数据库中的单词及查询次数:', 'blue') for word in Word.select(): print colored(word.keyword, 'cyan'), colored(str(word.count), 'green')
# The only 'meaning' should be an error telling the user that there was some problem log.exception("Error while trying to obtain Google response") if prompterror: return [[ Word(Text('<span style="color:gray">[Internet Error]</span>')) ]] else: return None except ValueError, e: # Not an internet problem log.exception( "Error while interpreting translation response from Google") if prompterror: return [[ Word( Text( '<span style="color:gray">[Error In Google Translate Response]</span>' )) ]] else: return None # This function will send a sample query to Google Translate and return true or false depending on success # It is used to determine connectivity for the Anki session (and thus whether Pinyin Toolkit should use online services or not) def gCheck(destlanguage='en'): try: lookup(u"好", destlanguage) return True except urllib2.URLError: return False except ValueError:
def show_db_list(): print colored(u"保存在数据库中的单词及查询次数:", "blue") for word in Word.select(): print colored(word.keyword, "cyan"), colored(str(word.count), "green")
def main(): reload(sys) config.prepare() try: # print(sys.argv[1:]) options, args = getopt.getopt(sys.argv[1:], 'aenfjkl:t:d:cvs:y', ['help', 'l']) # print(sys.argv, options, args) except getopt.GetoptError: # print("------") options = [('--help', '')] if ('--help', '') in options: # print("------") show_help() return use_api = False use_db = True play_voice = False use_dict = True open_ee = False to_lang = 'en' # print(options) for opt in options: if opt[0] == '-a': use_api = True if opt[0] == '-e': open_ee = True elif opt[0] == '-n': use_db = False elif opt[0] == '--l': show_db_list() sys.exit() elif opt[0] == '-l': show_today_list(opt[1]) sys.exit() elif opt[0] == '-t': to_lang = opt[1] elif opt[0] == '-d': del_word(opt[1]) sys.exit() elif opt[0] == '-c': del_word(None) sys.exit() elif opt[0] == '-v': play_voice = True elif opt[0] == '-s': if os.path.isdir(opt[1]): print(u'stardict 路径设置成功:', opt[1]) config.set_dict_path(opt[1]) else: print(u'stardict 路径设置失败. 原因可能是路径"%s"不存在.' % opt[1]) sys.exit() elif opt[0] == '-y': use_dict = False use_db = False else: print(opt) # print(options, args) dict_path = os.path.join(config.HOME, "dicts") config.set_dict_path(dict_path) # keyword = unicode(' '.join(args), encoding=sys.getfilesystemencoding()) # keyword = unicode(' '.join(args), encoding='utf-8') keyword = ' '.join(args) # print(keyword, to_lang, args) if not keyword: if play_voice: word = Word.get_last_word() keyword = word.keyword query(keyword, play_voice=True, use_db=True, to_lang=to_lang) else: show_help() else: query(keyword, use_db, use_api, play_voice, use_dict, open_ee, to_lang)
def show_today_list(args): days = int(args) print(colored(u'近%d天内查询的单词:' % (days), 'blue')) for word in Word.get_today_words(days): print(colored(word.keyword, 'cyan'), colored(str(word.count), 'green'))
def _process_select(self) -> Word: selections = self.category.split('|') selected = random.choice(selections) return Word(content=selected)
def _process_numbers(self) -> Word: matched = re.match(self.regex_numbers, self.content) start, end = matched.group('start'), matched.group('end') rand = random.randrange(int(start), int(end)) return Word(content=str(rand))