diff options
author | ache <ache@ache.one> | 2021-09-05 01:15:32 +0200 |
---|---|---|
committer | ache <ache@ache.one> | 2021-09-05 01:15:32 +0200 |
commit | 3f678c164a954588869b30f97dd1ec160f065f72 (patch) | |
tree | 6fbdff2c791f1b8653c5a252264bed3887cecfe9 /dicofr.py | |
parent | Typos in README.md (diff) |
Last rename dicofr => dfr
Diffstat (limited to 'dicofr.py')
-rwxr-xr-x | dicofr.py | 157 |
1 files changed, 0 insertions, 157 deletions
diff --git a/dicofr.py b/dicofr.py deleted file mode 100755 index 720e239..0000000 --- a/dicofr.py +++ /dev/null @@ -1,157 +0,0 @@ -#!/bin/env python - -''' -# Main file. - -A program to retrieve the definition of a french word (in french language). - -Maybe extended to other languages later. -''' - -import sys -import argparse -import msgpack -import sqlite3 -from os.path import exists -import os - -import ui - - -DEBUG = True - -if DEBUG: - DIR_PATH = os.getcwd() -else: - DIR_PATH = '/usr/share/dfr/' - -dico = 'dfr.db' - - -def get_def_sql(word): - with sqlite3.connect(dico) as con: - cur = con.cursor() - data = (word, ) - cur.execute('''SELECT * FROM entry WHERE word = ?''', data) - - res = cur.fetchall() - return list(map(lambda w: {'mot': w[0], - 'cat-gram': w[1], - 'API': w[2], - 'infos': w[3].split('\t'), - 'genre': w[4], - 'accord': w[5], - 'def': msgpack.unpackb(w[6], raw=False), - }, res)) - - -def get_def_sql_reg(word): - with sqlite3.connect(dico) as con: - cur = con.cursor() - data = (word, ) - cur.execute('''SELECT * FROM entry WHERE word LIKE ?''', data) - - res = cur.fetchall() - return list(map(lambda w: {'mot': w[0], - 'cat-gram': w[1], - 'API': w[2], - 'infos': w[3].split('\t'), - 'genre': w[4], - 'accord': w[5], - 'def': msgpack.unpackb(w[6], raw=False), - }, res)) - - -def matching(word): - ''' - Find matching words in the list of words - ''' - - matchingWord = [] - - if not exists(arg.wordList): - print(f'Error: Word list {arg.wordList} not found', file=sys.stderr) - return - - with open(arg.wordList, 'rb') as f: - msgpackList = f.read() - listWord = msgpack.unpackb(msgpackList, raw=False) - - if word[0] != '/': - for w in listWord: - if word == w: - matchingWord.append(w) - else: - if word[-1] != '/' or len(word) <= 2: - print('Erreur: Le format matching pour les regex est /MOT/', - file=sys.stderr) - return [] - import re - regex = re.compile(word[1:-1]) - - for w in listWord: - if regex.match(w): - matchingWord.append(w) - - return matchingWord - - -if __name__ == '__main__': - if len(sys.argv) < 2: - print('''Erreur: Rechercher un mot\nUtilisez l'option -h pour avoir de l'aide''', - file=sys.stderr) - exit(-1) - - parser = argparse.ArgumentParser(description='Get a french word\'s definition') - parser.add_argument('--sql', dest='action', action='store_const', - const=get_def_sql_reg, default=get_def_sql, - help='search a definition using SQL regex, ' - '_ to match a letter, %% to match a group of letters') - parser.add_argument('-m', '--matching', dest='matching', action='store_true', - help='search the french words that match the regex') - parser.add_argument('word', metavar='PATTERN', type=str, nargs='?', - help='the word or the pattern to match') - parser.add_argument('-l', '--word-list', dest='wordList', action='store', - help='the filename of the word list', - default=None) - parser.add_argument('-d', '--dico', dest='dico', action='store', - help='the filename of the dictionnary', - default='dfr.db') - parser.add_argument('-u', '--update', dest='update', type=str, nargs='?', const=dico, - help='update the database') - - arg = parser.parse_args() - - if arg.dico: - dico = arg.dico - - if arg.update: - os.chdir(DIR_PATH) - from subprocess import call - if arg.wordList: - r = call(['/usr/bin/env', 'python', f'{DIR_PATH}/download/download.py', '--download', '--output', dico, '--word-list', arg.wordList]) - else: - print(['/usr/bin/env', 'python', - f'{DIR_PATH}/download/download.py', '--download', '--output', - dico]) - r = call(['/usr/bin/env', 'python', f'{DIR_PATH}/download/download.py', '--download', '--output', dico]) - exit(r) - - # Si on n'arrive pas à trouver le dictionnaire - if not exists(dico): - if not exists(f'{DIR_PATH}/assets/{dico}'): - print('Error: No sqlite dictionnary', file=sys.stderr) - print(f'Default directory is set to "{DIR_PATH}"', file=sys.stderr) - exit(1) - else: - dico = f'{DIR_PATH}/assets/{dico}' - - if arg.matching: - ret = matching(arg.word) - for word in ret: - print(word) - if not ret: - exit(1) - else: - for w in arg.action(arg.word): - ui.show_terminal(w) |