evaluation
This commit is contained in:
parent
43b80ffa36
commit
6acdea3310
42
chatbot/modules/nlu.py
Normal file
42
chatbot/modules/nlu.py
Normal file
@ -0,0 +1,42 @@
|
||||
import copy
|
||||
from copy import deepcopy
|
||||
import json
|
||||
import os
|
||||
import jsgf
|
||||
|
||||
|
||||
class NLU:
|
||||
def __init__(self):
|
||||
self.grammars = [
|
||||
jsgf.parse_grammar_file(f"grammars/{file_name}")
|
||||
for file_name in os.listdir("grammars")
|
||||
]
|
||||
|
||||
def get_dialog_act(self, rule):
|
||||
slots = []
|
||||
self.get_slots(rule.expansion, slots)
|
||||
return {"act": rule.grammar.name, "slots": slots}
|
||||
|
||||
def get_slots(self, expansion, slots):
|
||||
if expansion.tag != "":
|
||||
slots.append((expansion.tag, expansion.current_match))
|
||||
return
|
||||
|
||||
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 match(self, utterance):
|
||||
list_of_illegal_character = [",", ".", "'", "?", "!", ":", "-", "/"]
|
||||
for illegal_character in list_of_illegal_character[:-2]:
|
||||
utterance = utterance.replace(f"{illegal_character}", "")
|
||||
for illegal_character in list_of_illegal_character[-2:]:
|
||||
utterance = utterance.replace(f"{illegal_character}", " ")
|
||||
|
||||
for grammar in self.grammars:
|
||||
matched = grammar.find_matching_rules(utterance.lower())
|
||||
if matched:
|
||||
return self.get_dialog_act(matched[0])
|
||||
return {"act": "null", "slots": []}
|
26
evaluate.py
Normal file
26
evaluate.py
Normal file
@ -0,0 +1,26 @@
|
||||
import os
|
||||
import re
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from chatbot.modules.nlu import NLU
|
||||
|
||||
rows = 0
|
||||
hits = 0
|
||||
|
||||
nlu = NLU()
|
||||
|
||||
for file_name in os.listdir("data"):
|
||||
df = pd.read_csv(f"data/{file_name}", sep="\t", names=["user", "sentence", "acts"])
|
||||
df = df[df.user == "user"]
|
||||
data = np.array(df)
|
||||
|
||||
for row in data:
|
||||
rows += 1
|
||||
sentence = row[1]
|
||||
cleaned_text = re.sub(r'\([^)]*\)', '', row[2])
|
||||
user_acts = cleaned_text.split("&")
|
||||
nlu_match = nlu.match(sentence)
|
||||
if nlu_match["act"] in user_acts:
|
||||
hits += 1
|
||||
|
||||
print(f"Accuracy: {(hits / rows) * 100}")
|
@ -16,7 +16,7 @@ public <cena> = <liczba> zł;
|
||||
public <liczba> = jeden | dwie | trzy | cztery | pięć | sześć | siedem | osiem | dziewięć | dziesięć;
|
||||
|
||||
public <adres> = ulica <ulica> miasto <miasto> kod pocztowy <kod_pocztowy>;
|
||||
public <ulica> = ul. <nazwa_ulicy>;
|
||||
public <ulica> = ul <nazwa_ulicy>;
|
||||
public <miasto> = <nazwa_miasta>;
|
||||
public <kod_pocztowy> = <cyfra><cyfra>-<cyfra><cyfra><cyfra>;
|
||||
|
||||
|
@ -3,4 +3,4 @@
|
||||
grammar welcomemsg;
|
||||
|
||||
public <welcomemsg> = <welcomemsgs>;
|
||||
<welcomemsgs> = Witamy w sklepie internetowym XYZ W czym mogę pomóc | Witaj! W czym mogę Ci dzisiaj pomóc| Witamy w sklepie internetowym XYZ W swojej ofercie mamy artykuły ogrodowe meblowe oraz kosmetyki | Witam tutaj sklep wielobranzowy w czym moge pomoc;
|
||||
<welcomemsgs> = Witamy w sklepie internetowym XYZ W czym mogę pomóc | Witaj W czym mogę Ci dzisiaj pomóc | Witamy w sklepie internetowym XYZ W swojej ofercie mamy artykuły ogrodowe meblowe oraz kosmetyki | Witam tutaj sklep wielobranzowy w czym moge pomoc | cześć chciałbym kupić {product};
|
Loading…
Reference in New Issue
Block a user