Skip to content

Commit 9f071b9

Browse files
Make package simpler to use
This would allow ln2sql to be used as: from ln2sql import Ln2sql ln2obj = Ln2sql(<database>, <language>) ln2obj.get_query('input sentence')
1 parent d236041 commit 9f071b9

9 files changed

+34
-27
lines changed

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ clean:
22
rm -rf *.json *.pyc
33
test:
44
pytest
5-
rm -rf *.json *.pyc
5+
rm -rf *.json *.pyc
6+
lint:
7+
pep8 .

ln2sql/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .ln2sql import Ln2sql

ln2sql/database.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, re
1+
import os
2+
import re
23

34
from .constants import Color
45
from .table import Table

ln2sql/langConfig.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, unicodedata
1+
import os
2+
import unicodedata
23

34

45
class LangConfig:

ln2sql/ln2sql.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,47 @@ def __init__(
1414
self,
1515
database_path,
1616
language_path,
17-
input_sentence,
1817
json_output_path=None,
1918
thesaurus_path=None,
2019
stopwords_path=None,
2120
):
2221

2322
database = Database()
24-
stopwordsFilter = None
23+
self.stopwordsFilter = None
2524

2625
if thesaurus_path:
2726
thesaurus = Thesaurus()
2827
thesaurus.load(thesaurus_path)
2928
database.set_thesaurus(thesaurus)
3029

3130
if stopwords_path:
32-
stopwordsFilter = StopwordFilter()
33-
stopwordsFilter.load(stopwords_path)
31+
self.stopwordsFilter = StopwordFilter()
32+
self.stopwordsFilter.load(stopwords_path)
3433

3534
database.load(database_path)
3635
# database.print_me()
3736

3837
config = LangConfig()
3938
config.load(language_path)
4039

41-
parser = Parser(database, config)
40+
self.parser = Parser(database, config)
41+
self.json_output_path = json_output_path
4242

43-
queries = parser.parse_sentence(input_sentence, stopwordsFilter)
43+
def get_query(self, input_sentence):
44+
queries = self.parser.parse_sentence(input_sentence, self.stopwordsFilter)
4445

45-
if json_output_path:
46+
if self.json_output_path:
4647
self.remove_json(json_output_path)
4748
for query in queries:
48-
query.print_json(json_output_path)
49+
query.print_json(self.json_output_path)
50+
51+
full_query = ''
4952

50-
self.full_query = ''
5153
for query in queries:
52-
self.full_query += str(query)
54+
full_query += str(query)
5355
print(query)
5456

55-
def get_query(self):
56-
return self.full_query
57+
return full_query
5758

5859
def remove_json(self, filename="output.json"):
5960
if os.path.exists(filename):

ln2sql/ln2sql_gui.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def lanch_parsing(self):
9494
if (str(self.database_path_label["text"]) != "No SQL dump selected...") and (
9595
str(self.language_path_label["text"]) != "No configuration file selected...") and (
9696
str(self.input_sentence_string.get()) != "Enter a sentence..."):
97-
Ln2sql(self.database_path_label["text"], self.input_sentence_string.get(),
97+
Ln2sql(self.database_path_label["text"],
9898
self.language_path_label["text"], thesaurus_path=thesaurus_path,
99-
json_output_path='./output.json')
99+
json_output_path='./output.json').get_query(self.input_sentence_string.get())
100100
showinfo('Result', 'Parsing done!')
101101
else:
102102
showwarning('Warning', 'You must fill in all fields, please.')

ln2sql/main.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .ln2sql import Ln2sql
44

5+
56
def main():
67
arg_parser = argparse.ArgumentParser(description='A Utility to convert Natural Language to SQL query')
78
arg_parser.add_argument('-d', '--database', help='Path to SQL dump file', required=True)
@@ -13,15 +14,13 @@ def main():
1314

1415
args = arg_parser.parse_args()
1516

16-
Ln2sql(
17+
ln2sql = Ln2sql(
1718
database_path=args.database,
1819
language_path=args.language,
19-
input_sentence=args.sentence,
2020
json_output_path=args.json_output,
2121
thesaurus_path=args.thesaurus,
2222
stopwords_path=args.stopwords,
23-
)
24-
23+
).get_query(args.sentence)
2524

2625
if __name__ == '__main__':
2726
main()

tests/test_thesaurus.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, re
1+
import os
2+
import re
23

34
from ln2sql.ln2sql import Ln2sql
45

@@ -96,5 +97,5 @@ def test_main():
9697

9798
for test in thesaurusTest:
9899
assert _clean_output(Ln2sql(
99-
test['database'], test['language'], test['input'], thesaurus_path=test['thesaurus']
100-
).get_query()) == test['output']
100+
test['database'], test['language'], thesaurus_path=test['thesaurus']
101+
).get_query(test['input'])) == test['output']

tests/test_unit.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, re
1+
import os
2+
import re
23

34
import pytest
45

@@ -195,7 +196,7 @@ def test_main():
195196

196197
for test in correctTest:
197198
assert _clean_output(
198-
Ln2sql(test['database'], test['language'], test['input']).get_query()
199+
Ln2sql(test['database'], test['language']).get_query(test['input'])
199200
) == test['output']
200201

201202

@@ -231,4 +232,4 @@ def test_exception():
231232
]
232233
for test in errorTest:
233234
with pytest.raises(Exception):
234-
Ln2sql(test['database'], test['language'], test['input']).get_query()
235+
Ln2sql(test['database'], test['language']).get_query(test['input'])

0 commit comments

Comments
 (0)