def get(self): characters = request.args.get('characters') if characters == None or len(characters) == 0: return jsonpify({'error': 'No characters provided'}) characters = characters.replace(' ', '') path = tempfile.mkdtemp() error_msg = 'generate_infos ' + characters + '\n' try: generate_infos(MAKEMEAHANZI_PATH, CEDICT_PATH, path, characters) except GenException as e: log_error(path, error_msg + str(e)) return jsonpify({'error': str(e)}) except Exception as e: log_error(path, error_msg + str(e)) return jsonpify({'error': INTERNAL_ERROR_MSG}) characters = get_characters(path) words = get_words(path, characters) result = { 'id': path, 'characters': characters, 'words': words } return jsonpify(result)
def test_generate_infos_too_many_characters(self): with tempfile.TemporaryDirectory() as wd: characters = '你好號號號號號號號號號號號號號號號號號' + \ '號號號號號號號號號號號號號號號號號號號' + \ '號號號號號號號號號號號號號號號號號號號' with self.assertRaises(GenException): generate_infos(self.makemeahanzi_path, self.cedict_path, \ wd, characters)
def get(self): characters = request.args.get('characters') if characters == None or len(characters) == 0: return jsonpify({'error': 'No characters provided'}) characters = characters.replace(' ', '') with tempfile.TemporaryDirectory() as path: dataset_path = get_dataset_path() try: generate_infos(dataset_path, path, characters) except GenException as e: log_error('generate_infos ' + characters) return jsonpify({'error': str(e)}) except: log_error('generate_infos ' + characters) return jsonpify({'error': INTERNAL_ERROR_MSG}) with open(os.path.join(path, INFOS_FILE)) as f: infos = [] while 1: line = f.readline() if line == '': break info = json.loads(line) character = info['character'] definition = info['definition'] pinyin = info['pinyin'][0] i = {'character': character, \ 'definition': definition, \ 'pinyin': pinyin} infos.append(i) new_dir = tempfile.mkdtemp() shutil.copy(os.path.join(path, INFOS_FILE), new_dir) result = { "id": new_dir, "infos": infos } return jsonpify(result)
def test_generate_infos_invalid_cedict_path(self): with tempfile.TemporaryDirectory() as wd: characters = '你好' with self.assertRaises(Exception): generate_infos(self.makemeahanzi_path, 'i_dont_exist', \ wd, characters)
def test_generate_infos_unknown_word(self): with tempfile.TemporaryDirectory() as wd: characters = '(你好號號)' generate_infos(self.makemeahanzi_path, self.cedict_path, \ wd, characters) self.__assert_correct_info_files(wd, 4, 1)
def test_generate_infos_no_words(self): with tempfile.TemporaryDirectory() as wd: characters = '你好' generate_infos(self.makemeahanzi_path, self.cedict_path, \ wd, characters) self.__assert_correct_info_files(wd, 2, 0)