forked from FerreroJeremy/ln2sql
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ln2sql.py
executable file
·114 lines (95 loc) · 3.33 KB
/
ln2sql.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
# -*- coding: utf-8 -*
import os, sys, getopt
import unicodedata
from Database import Database
from LangConfig import LangConfig
from Parser import Parser
from Thesaurus import Thesaurus
from StopwordFilter import StopwordFilter
reload(sys)
sys.setdefaultencoding("utf-8")
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
class ln2sql:
def __init__(self, database_path, input_sentence, language_path, thesaurus_path, json_output_path):
database = Database()
database.load(database_path)
database.print_me()
config = LangConfig()
config.load(language_path)
parser = Parser(database, config)
if thesaurus_path is not None:
thesaurus = Thesaurus()
thesaurus.load(thesaurus_path)
parser.set_thesaurus(thesaurus)
queries = parser.parse_sentence(input_sentence)
if json_output_path is not None:
self.remove_json(json_output_path)
for query in queries:
query.print_json(json_output_path)
for query in queries:
print query
def remove_json(self, filename="output.json"):
if os.path.exists(filename):
os.remove(filename)
def print_help_message():
print '\n'
print 'Usage:'
print '\tpython ln2sql.py -d <path> -l <path> -i <input-sentence> [-t <path>] [-j <path>]'
print 'Parameters:'
print '\t-h\t\t\tprint this help message'
print '\t-d <path>\t\tpath to SQL dump file'
print '\t-l <path>\t\tpath to language configuration file'
print '\t-i <input-sentence>\tinput sentence to parse'
print '\t-j <path>\t\tpath to JSON output file'
print '\t-t <path>\t\tpath to thesaurus file'
print '\n'
def main(argv):
try:
opts, args = getopt.getopt(argv,"d:l:i:t:j:")
database_path = None
input_sentence = None
language_path = None
thesaurus_path = None
json_output_path = None
for i in range(0, len(opts)):
if opts[i][0] == "-d":
database_path = opts[i][1]
elif opts[i][0] == "-l":
language_path = opts[i][1]
elif opts[i][0] == "-i":
input_sentence = opts[i][1]
elif opts[i][0] == "-j":
json_output_path = opts[i][1]
elif opts[i][0] == "-t":
thesaurus_path = opts[i][1]
else:
print_help_message()
sys.exit()
if (database_path is None) or (input_sentence is None) or (language_path is None):
print_help_message()
sys.exit()
else:
if thesaurus_path is not None:
thesaurus_path = str(thesaurus_path)
if json_output_path is not None:
json_output_path = str(json_output_path)
#try:
ln2sql(str(database_path), str(input_sentence), str(language_path), thesaurus_path, json_output_path)
#except Exception, e:
# print color.BOLD + color.RED + str(e) + color.END
except getopt.GetoptError:
print_help_message()
sys.exit()
if __name__ == '__main__':
main(sys.argv[1:])