NLU
This commit is contained in:
parent
975463e791
commit
be71442103
50
Modules.py
50
Modules.py
@ -1,17 +1,37 @@
|
||||
import os
|
||||
import jsgf
|
||||
|
||||
#Natural Language Understanding
|
||||
class NLU:
|
||||
#Text analysys
|
||||
def __init__(self, acts, arguments):
|
||||
self.acts = acts
|
||||
self.arguments = arguments
|
||||
|
||||
def analyze(self, text):
|
||||
#Turn text into frame
|
||||
#return vector for further use
|
||||
def __init__(self):
|
||||
self.grammars = [jsgf.parse_grammar_file(f'JSGFs/{file_name}') for file_name in os.listdir('JSGFs')]
|
||||
|
||||
def get_dialog_act(self, sentence):
|
||||
for grammar in self.grammars:
|
||||
match = grammar.find_matching_rules(sentence)
|
||||
if match:
|
||||
return grammar.name
|
||||
|
||||
def match_slots(self, expansion, slots):
|
||||
if expansion.tag != '':
|
||||
slots.append((expansion.tag, expansion.current_match))
|
||||
return slots
|
||||
|
||||
for child in expansion.children:
|
||||
self.get_slots(child, slots)
|
||||
|
||||
if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef):
|
||||
self.get_slots(expansion.referenced_rule.expansion, slots)
|
||||
|
||||
def get_slots(self, utterance):
|
||||
slots = []
|
||||
for grammar in self.grammars:
|
||||
matched = grammar.find_matching_rules(utterance)
|
||||
if matched:
|
||||
return self.match_slots(matched[0], slots)
|
||||
return []
|
||||
|
||||
act = "(hello()&request(name))"
|
||||
vector = [[0],[1,0]]
|
||||
return vector
|
||||
|
||||
#Dialogue policy
|
||||
class DP:
|
||||
@ -61,7 +81,7 @@ class Run:
|
||||
0: "name"
|
||||
}
|
||||
|
||||
self.nlu = NLU(self.acts, self.arguments)
|
||||
self.nlu = NLU()
|
||||
self.dp = DP(self.acts, self.arguments)
|
||||
self.nlg = NLG(self.acts, self.arguments)
|
||||
self.dst = DST(self.acts, self.arguments)
|
||||
@ -75,10 +95,10 @@ class Run:
|
||||
|
||||
return self.nlg.vectorToText(basic_act)
|
||||
|
||||
run = Run()
|
||||
while(1):
|
||||
message = input("Napisz coś: ")
|
||||
print(run.inputProcessing(message))
|
||||
# run = Run()
|
||||
# while(1):
|
||||
# message = input("Napisz coś: ")
|
||||
# print(run.inputProcessing(message))
|
||||
|
||||
|
||||
|
||||
|
@ -6,6 +6,7 @@ import numpy as np
|
||||
|
||||
PATTERN = r'[^(]*'
|
||||
|
||||
# Algorytm sprawdzający
|
||||
grammars = [jsgf.parse_grammar_file(f'JSGFs/{file_name}') for file_name in os.listdir('JSGFs')]
|
||||
|
||||
rows = 0
|
||||
@ -29,3 +30,4 @@ for file_name in os.listdir('data'):
|
||||
hits += 1
|
||||
|
||||
print(f"Accuracy: {(hits / rows)*100}")
|
||||
# Dokładność 30%
|
Loading…
Reference in New Issue
Block a user