Skip to content

Commit bb2c10f

Browse files
committed
updated parser Extract() commands function
- parser Extract() commands() now returns a single list - parser Extract() commands now allows quotes to define keywords with spaces in them - updated example for parser Extract() commands - updated formatting and tabulation for certain files
1 parent bc4adbc commit bb2c10f

File tree

7 files changed

+61
-37
lines changed

7 files changed

+61
-37
lines changed

examples/example_chatbot_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
order = {
1616
"pizza" : [{"stem":"pizza","wordclass":"noun"}],
1717
"cheese" : [{"stem":"sajt","wordclass":"noun"}],
18-
"pepperoni" : [{"stem":"szalámi","wordclass":"noun"}],
19-
"pineapple" : [{"stem":"ananász","wordclass":"noun"}],
18+
"pepperoni" : [{"stem":"szalámi","wordclass":"noun"}],
19+
"pineapple" : [{"stem":"ananász","wordclass":"noun"}],
2020
}
2121
order_match = parser.Intents(order).match_set(user_text)
2222

examples/example_chatbot_5.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
###
1414

15-
tone = entities.tone()
16-
tone_match = parser.Intents(tone).match_best(user_text,1) # match_best
17-
common = entities.common()
18-
common_match= parser.Intents(common).match_set(user_text)
15+
tone = entities.tone()
16+
tone_match = parser.Intents(tone).match_best(user_text,1) # match_best
17+
common = entities.common()
18+
common_match = parser.Intents(common).match_set(user_text)
1919

2020
if 'formal' in tone_match:
2121
if 'profanity' in common_match:

examples/example_chatbot_6.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88

99
if __name__ == "__main__":
1010

11-
user_text = '/echo visszhang teszt'
11+
user_text = '/echo "visszhang teszt"'
1212

1313
###
1414

1515
info = parser.Extract(user_text) # /echo command
16-
commands = info.commands()
16+
commands = info.commands()
1717
func = commands[0]
18-
args = commands[1] # list
18+
args = commands[1]
1919

2020
if func:
2121
if func == 'ping':
2222
print('pong')
2323
elif func == 'echo':
24-
print(' '.join(args))
24+
print(args)
2525
else:
2626
print('Parancs nélküli üzenet') # nem íródik ki

examples/example_chatbot_7.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
order = {
1616
"pizza" : [{"stem":"pizza","wordclass":"noun"}],
1717
"cheese" : [{"stem":"sajt","wordclass":"noun"}],
18-
"pepperoni" : [{"stem":"szalámi","wordclass":"noun"}],
19-
"pineapple" : [{"stem":"ananász","wordclass":"noun"}],
18+
"pepperoni" : [{"stem":"szalámi","wordclass":"noun"}],
19+
"pineapple" : [{"stem":"ananász","wordclass":"noun"}],
2020
}
2121
order_match = parser.Intents(order).match_set(user_text)
2222

examples/example_news.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
important = {
1616
"lottery" : [{"stem":"lottó","wordclass":"noun"}],
17-
"winning" : [{"stem":"nyer","wordclass":"verb"},{"stem":"nyertes","wordclass":"noun"}],
17+
"winning" : [{"stem":"nyer","wordclass":"verb"},{"stem":"nyertes","wordclass":"noun"}],
1818
"losing" : [{"stem":"veszt","wordclass":"verb"},{"stem":"veszít","wordclass":"verb"},{"stem":"vesztes","wordclass":"noun"}],
1919
}
2020
important_match = parser.Intents(important).match(article)

examples/example_wiki_intents.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
print(busz_test.match("Lassan beérünk az autóbusszal a buszpályaudvarra."))
3939

4040
hasonul_intents = {
41-
"enni" : [{"stem":"esz","wordclass":"verb","match_stem":False},{"stem":"en","wordclass":"verb","match_stem":False}]
41+
"enni" : [{"stem":"esz","wordclass":"verb","match_stem":False},{"stem":"en","wordclass":"verb","match_stem":False}]
4242
}
4343
hasonul_test = parser.Intents(hasonul_intents)
4444
print(hasonul_test.match("Tőmorfémák: esz, en.")) # nem veszi figyelembe
4545
print(hasonul_test.match("Eszel valamit?"))
4646
print(hasonul_test.match("Azt nem lehet megenni."))
4747

4848
egyutt_intents = {
49-
"jo_ido" : [{"stem":"jó","wordclass":"adjective",
49+
"jo_ido" : [{"stem":"jó","wordclass":"adjective",
5050
"inc":[{"stem":"idő","wordclass":"noun","affix":["járás"]},{"stem":"meleg","wordclass":"adjective"}]}]
5151
}
5252
egyutt_test = parser.Intents(egyutt_intents)
@@ -59,7 +59,7 @@
5959
print(egyutt_test.match("Jó meleg az időjárás.")) # dupla pont
6060

6161
kulon_intents = {
62-
"jobb_ido" : [{"stem":"jó","wordclass":"adjective",
62+
"jobb_ido" : [{"stem":"jó","wordclass":"adjective",
6363
"inc":[{"stem":"idő","wordclass":"noun","affix":["járás"]},{"stem":"meleg","wordclass":"adjective"}],
6464
"exc":[{"stem":"este","wordclass":"noun"}]}]
6565
}
@@ -70,8 +70,8 @@
7070
print(kulon_test.match("Jó meleg esténk van!")) # szintén nem veszi figyelembe
7171

7272
fals_pozitiv = {
73-
"megszerel" : [{"stem":"szerel","wordclass":"verb"}],
74-
"hibasan" : [{"stem":"alma","wordclass":"noun"}],
73+
"megszerel" : [{"stem":"szerel","wordclass":"verb"}],
74+
"hibasan" : [{"stem":"alma","wordclass":"noun"}],
7575
}
7676
hibas_test = parser.Intents(fals_pozitiv)
7777
print(hibas_test.match("Gyönyörű dolog a szerelem")) # elfogadja hibásan

lara/parser.py

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ def currencies(self,normalize=True,convert=True):
956956
return []
957957

958958
def _currencies_fallback(self):
959-
dates = [item.replace('-',':') for item in self.dates()]
959+
dates = [item.replace('-',':') for item in self.dates()]
960960
number = [str(item) for item in self.numbers()]
961961
against = [item[-2] if (len(item)>2 and item[-1]==0 and item[-2]=='.') else item for item in number]
962962
ignore = []
@@ -977,15 +977,39 @@ def _currencies_fallback(self):
977977
okay.append(item)
978978
return okay
979979

980-
# extract commands and arguments from text: "/help lara" will return ('help',['lara'])
980+
# extract commands and arguments from text: /help "about lara" will return ['help','about lara']
981981
def commands(self):
982-
if self.text and self.text[0] == '/':
983-
commands = (lara.nlp.trim(str(self.text[1:]))).split(" ")
984-
if len(commands)>1:
985-
return (commands[0],commands[1:])
986-
else:
987-
return (commands[0],[])
988-
return ('',[])
982+
result = []
983+
if len(self.text)>1 and self.text[0] in ('/','\\'):
984+
quote = ''
985+
keyword = ''
986+
for char in self.text[1:]:
987+
if char in (' ','"',"'"):
988+
if char==' ':
989+
if quote:
990+
keyword += ' '
991+
else:
992+
if keyword:
993+
result.append(keyword)
994+
keyword = ''
995+
elif char in ('"',"'"):
996+
if not quote:
997+
if keyword:
998+
result.append(keyword)
999+
keyword = ''
1000+
quote = char
1001+
elif quote==char:
1002+
if keyword:
1003+
result.append(keyword)
1004+
keyword = ''
1005+
quote = ''
1006+
else:
1007+
keyword += char
1008+
else:
1009+
keyword += char
1010+
if keyword:
1011+
result.append(keyword)
1012+
return result
9891013

9901014
# extract list of emojis from text via https://gist.github.com/naotokui/
9911015
def emojis(self):
@@ -1065,9 +1089,9 @@ def timestamps(self,current=False):
10651089
dates = self.dates(False)
10661090
relative = self.relative_dates(False,c_relative)
10671091
times = self.times(False,True,c_times)
1068-
dates_pos = []
1069-
relative_pos= []
1070-
times_pos = []
1092+
dates_pos = []
1093+
relative_pos = []
1094+
times_pos = []
10711095
for item in dates:
10721096
for match in _re.finditer(r'\b'+re.escape(item), re.IGNORECASE, self.ntext):
10731097
dates_pos.append(match.span()[0])
@@ -1083,13 +1107,13 @@ def timestamps(self,current=False):
10831107
dates = self.dates()
10841108
relative = self.relative_dates(True,c_relative)
10851109
times = self.times(True,True,c_times)
1086-
results = []
1087-
last_date = ''
1088-
last_date_a= ''
1089-
last_time = ''
1090-
dates_i = 0
1091-
relative_i = 0
1092-
times_i = 0
1110+
results = []
1111+
last_date = ''
1112+
last_date_a = ''
1113+
last_time = ''
1114+
dates_i = 0
1115+
relative_i = 0
1116+
times_i = 0
10931117
for i in range(max(dates_pos+relative_pos+times_pos)+1):
10941118
if i == dates_pos[dates_i]:
10951119
if last_date or last_time:

0 commit comments

Comments
 (0)