/
oedict.py
executable file
·56 lines (46 loc) · 1.74 KB
/
oedict.py
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
#!/usr/bin/env python
import argparse
import sys
import textwrap
import lexdb
import lexicon
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
p = argparse.ArgumentParser(description="Kef's Old English dictionary")
p.add_argument('-l', '--lexicon', default='lexicon.txt', help="filename of lexicon")
p.add_argument('-i', '--interactive', action='store_true', help="interactive mode")
p.add_argument('-r', '--reverse', action='store_true', help="reverse lookup")
p.add_argument('-d', '--db', default='lexicon.out.sqlite3', help="filename of sqlite database")
p.add_argument('--abc', action='store_true', help="check lexicon is in alphabetical order")
p.add_argument('search_terms', nargs='*')
args = p.parse_args(argv)
with lexdb.LexDB(args.lexicon, args.db) as db:
if args.abc:
db.check_alphabetization()
for term in args.search_terms:
lookup(db, term, args.reverse)
if args.interactive:
interactive_mode(db, args.reverse)
def interactive_mode(db, reverse):
while True:
try:
search_str = input("> ").strip()
except (KeyboardInterrupt, EOFError):
print()
break
lookup(db, search_str, reverse)
def lookup(db, search_str, reverse):
if reverse:
entries = db.reverse_lookup(search_str)
else:
entries = db.lookup(search_str)
if len(entries) == 0:
print("Not found:", search_str, "\n")
else:
for entry in entries:
types = "; ".join(lexicon.expand_word_type(word_type) for word_type in entry.word_types)
print(f"{entry.lemma}: {types}")
print(textwrap.indent(entry.text, " "*4))
if __name__ == '__main__':
main()