Systemy_dialogowe/system_mockup/analizator_jezyka_naturalnego.py
2022-06-01 10:49:28 +02:00

47 lines
1.3 KiB
Python

import re
from acts import hello_act, name_request_act
hello = ['dzien dobry', 'dobry wieczor', 'witam', 'witaj', 'siema', 'elo', 'czesc']
request_name = ['imie', 'nazywasz']
question = ['\?$']
acts = {hello_act: [hello],
name_request_act: [request_name, question]}
def analizator_jezyka_naturalnego(text):
text = text_preprocess(text)
frame = act_check(text)
return frame
def text_preprocess(text):
text = text.lower()
text = text.replace("ą", "a")
text = text.replace("ć", "c")
text = text.replace("ę", "e")
text = text.replace("ł", "l")
text = text.replace("ń", "n")
text = text.replace("ó", "o")
text = text.replace("ś", "s")
text = text.replace("ź", "z")
text = text.replace("ż", "z")
text = text.replace("\n", " ")
text = text.replace("\t", " ")
text = text.replace(" ", " ")
return text
def act_check(text):
frame = []
for act, requirements in acts.items():
req = [check_synonyms(synonyms, text) for synonyms in requirements]
if all(req):
frame.append(act)
return frame
def check_synonyms(synonyms, text):
for expression in synonyms:
match = re.search(expression, text)
if match:
return True
return False