aboutsummaryrefslogtreecommitdiff
path: root/dicofr.py
blob: b41f6581963bab28f80f89cd68692c7e9f3c7e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/env python

import sys
import argparse
import msgpack
import sqlite3
from os.path import exists


DIR_PATH = '/usr/share/dicofr'
sys.path.insert(-1, DIR_PATH)

import ui

dico = 'wiktfr.sql'


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
    """
    print("Error: Not implemented yet", file=sys.stderr)
    pass


if __name__ == '__main__':
    if len(sys.argv) < 2:
      print("Erreur: Rechercher un mot", file=sys.stderr)
      exit()

    if not exists(dico):
        if not exists(f'{DIR_PATH}/{dico}'):
            print('Error: No sqlite dictionnary', file=sys.stderr)
            exit(1)
        else:
            dico = f'{DIR_PATH}/{dico}'

    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('--matching', dest='matching', action='store_true',
                        help='search the french words that match the regex')
    parser.add_argument('word', metavar='PATTERN', type=str,
                        help='the word or the pattern to match')

    arg = parser.parse_args()

    if arg.matching:
        matching(arg.word)
    else:
        for w in arg.action(arg.word):
            ui.show_terminal(w)