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
|
#Natural Language Understanding
|
||||||
class NLU:
|
class NLU:
|
||||||
#Text analysys
|
|
||||||
def __init__(self, acts, arguments):
|
|
||||||
self.acts = acts
|
|
||||||
self.arguments = arguments
|
|
||||||
|
|
||||||
def analyze(self, text):
|
def __init__(self):
|
||||||
#Turn text into frame
|
self.grammars = [jsgf.parse_grammar_file(f'JSGFs/{file_name}') for file_name in os.listdir('JSGFs')]
|
||||||
#return vector for further use
|
|
||||||
|
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
|
#Dialogue policy
|
||||||
class DP:
|
class DP:
|
||||||
@ -61,7 +81,7 @@ class Run:
|
|||||||
0: "name"
|
0: "name"
|
||||||
}
|
}
|
||||||
|
|
||||||
self.nlu = NLU(self.acts, self.arguments)
|
self.nlu = NLU()
|
||||||
self.dp = DP(self.acts, self.arguments)
|
self.dp = DP(self.acts, self.arguments)
|
||||||
self.nlg = NLG(self.acts, self.arguments)
|
self.nlg = NLG(self.acts, self.arguments)
|
||||||
self.dst = DST(self.acts, self.arguments)
|
self.dst = DST(self.acts, self.arguments)
|
||||||
@ -75,10 +95,10 @@ class Run:
|
|||||||
|
|
||||||
return self.nlg.vectorToText(basic_act)
|
return self.nlg.vectorToText(basic_act)
|
||||||
|
|
||||||
run = Run()
|
# run = Run()
|
||||||
while(1):
|
# while(1):
|
||||||
message = input("Napisz coś: ")
|
# message = input("Napisz coś: ")
|
||||||
print(run.inputProcessing(message))
|
# print(run.inputProcessing(message))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import numpy as np
|
|||||||
|
|
||||||
PATTERN = r'[^(]*'
|
PATTERN = r'[^(]*'
|
||||||
|
|
||||||
|
# Algorytm sprawdzający
|
||||||
grammars = [jsgf.parse_grammar_file(f'JSGFs/{file_name}') for file_name in os.listdir('JSGFs')]
|
grammars = [jsgf.parse_grammar_file(f'JSGFs/{file_name}') for file_name in os.listdir('JSGFs')]
|
||||||
|
|
||||||
rows = 0
|
rows = 0
|
||||||
@ -29,3 +30,4 @@ for file_name in os.listdir('data'):
|
|||||||
hits += 1
|
hits += 1
|
||||||
|
|
||||||
print(f"Accuracy: {(hits / rows)*100}")
|
print(f"Accuracy: {(hits / rows)*100}")
|
||||||
|
# Dokładność 30%
|
Loading…
Reference in New Issue
Block a user