Finalne poprawki

This commit is contained in:
s495727 2024-05-10 01:25:13 +02:00
parent 846c6991e7
commit 16af0e732c
7 changed files with 952 additions and 878 deletions

View File

@ -191,11 +191,11 @@
6 napoje? request/drinks NoLabel 6 napoje? request/drinks NoLabel
# text: jaka cena coli? # text: jaka cena coli?
# intent: request(price) # intent: request/price
# slots: # slots:
1 jaka request(price) NoLabel 1 jaka request/price NoLabel
2 cena request(price) NoLabel 2 cena request/price NoLabel
3 coli? request(price) B-drink 3 coli? request/price B-drink
# text: to poproszę. Potwierdzam zamowienie # text: to poproszę. Potwierdzam zamowienie
# intent: inform/order-complete # intent: inform/order-complete
@ -319,10 +319,10 @@
7 ale inform/order NoLabel 7 ale inform/order NoLabel
8 żeby inform/order NoLabel 8 żeby inform/order NoLabel
9 była inform/order NoLabel 9 była inform/order NoLabel
10 zimna inform/order B-temp 10 zimna inform/order NoLabel
11 i inform/order NoLabel 11 i inform/order NoLabel
12 bez inform/order B-sugar/neg 12 bez inform/order NoLabel
13 cukru inform/order I-sugar/neg 13 cukru inform/order NoLabel
# text: ulica niebieska 230/2 # text: ulica niebieska 230/2
# intent: inform/address # intent: inform/address
@ -405,7 +405,7 @@
4 dostępne request/menu NoLabel 4 dostępne request/menu NoLabel
5 pizze? request/menu B-food 5 pizze? request/menu B-food
# text: świetnie, w takim razie poproszę dwie duże pizze diavola oraz margaritę # text: świetnie, w takim razie poproszę trzy duże pizze diavola oraz margaritę
# intent: inform/order # intent: inform/order
# slots: # slots:
1 świetnie, inform/order NoLabel 1 świetnie, inform/order NoLabel
@ -413,7 +413,7 @@
3 takim inform/order NoLabel 3 takim inform/order NoLabel
4 razie inform/order NoLabel 4 razie inform/order NoLabel
5 poproszę inform/order NoLabel 5 poproszę inform/order NoLabel
6 dwie inform/order B-quantity 6 trzy inform/order B-quantity
7 duże inform/order B-size 7 duże inform/order B-size
8 pizze inform/order B-food 8 pizze inform/order B-food
9 diavola inform/order B-pizza 9 diavola inform/order B-pizza
@ -428,12 +428,12 @@
3 jeden affirm B-quantity 3 jeden affirm B-quantity
# text: ze Szczebrzeszyna, powiat Łękołody # text: ze Szczebrzeszyna, powiat Łękołody
# intent: inform/name # intent: inform/address
# slots: # slots:
1 ze inform/name NoLabel 1 ze inform/address NoLabel
2 Szczebrzeszyna, inform/name B-address 2 Szczebrzeszyna, inform/address B-address
3 powiat inform/name I-address 3 powiat inform/address I-address
4 Łękołody inform/name I-address 4 Łękołody inform/address I-address
# text: Grzegorz Brzęczyszczykiewicz # text: Grzegorz Brzęczyszczykiewicz
# intent: inform/name # intent: inform/name
@ -508,7 +508,7 @@
# slots: # slots:
1 Poproszę inform/order NoLabel 1 Poproszę inform/order NoLabel
2 wersję inform/order NoLabel 2 wersję inform/order NoLabel
3 klasyczną inform/order B-type 3 klasyczną inform/order NoLabel
4 średnią inform/order B-size 4 średnią inform/order B-size
# text: Ile będzie ona kosztować? # text: Ile będzie ona kosztować?
@ -566,8 +566,8 @@
2 colę inform/order B-drink 2 colę inform/order B-drink
3 poproszę, inform/order NoLabel 3 poproszę, inform/order NoLabel
4 jednakże inform/order NoLabel 4 jednakże inform/order NoLabel
5 bez inform/order B-option 5 bez inform/order NoLabel
6 cukru inform/order I-option 6 cukru inform/order NoLabel
# text: Płatność będzie kartą. # text: Płatność będzie kartą.
# intent: inform/payment # intent: inform/payment

View File

@ -332,11 +332,11 @@
3 w request/menu NoLabel 3 w request/menu NoLabel
4 ofercie request/menu NoLabel 4 ofercie request/menu NoLabel
# text: chciałbym 3 pizze, hawajskie duże # text: chciałbym trzy pizze, hawajskie duże
# intent: inform/order # intent: inform/order
# slots: # slots:
1 chciałbym inform/order NoLabel 1 chciałbym inform/order NoLabel
2 3 inform/order B-quantity 2 trzy inform/order B-quantity
3 pizze, inform/order B-food 3 pizze, inform/order B-food
4 hawajskie inform/order B-pizza 4 hawajskie inform/order B-pizza
5 duże inform/order B-size 5 duże inform/order B-size
@ -585,11 +585,11 @@
4 tuna inform/order B-pizza 4 tuna inform/order B-pizza
5 XL inform/order B-size 5 XL inform/order B-size
# text: wezmę 3 pizze tuna, średnią, dużą i bardzo dużą # text: wezmę 3x pizze tuna, średnią, dużą i bardzo dużą
# intent: inform/order # intent: inform/order
# slots: # slots:
1 wezmę inform/order NoLabel 1 wezmę inform/order NoLabel
2 3 inform/order B-quantity 2 3x inform/order B-quantity
3 pizze inform/order B-food 3 pizze inform/order B-food
4 tuna, inform/order B-pizza 4 tuna, inform/order B-pizza
5 średnią, inform/order B-size 5 średnią, inform/order B-size
@ -825,6 +825,14 @@
1 jakie request/ingredients NoLabel 1 jakie request/ingredients NoLabel
2 składniki request/ingredients NoLabel 2 składniki request/ingredients NoLabel
# text: co jest na pizzy
# intent: request/ingredients
# slots:
1 co request/ingredients NoLabel
2 jest request/ingredients NoLabel
3 na request/ingredients NoLabel
4 pizzy request/ingredients NoLabel
# text: jakie są napoje # text: jakie są napoje
# intent: request/drinks # intent: request/drinks
# slots: # slots:
@ -850,3 +858,54 @@
2 macie request/drinks NoLabel 2 macie request/drinks NoLabel
3 do request/drinks NoLabel 3 do request/drinks NoLabel
4 picia request/drinks NoLabel 4 picia request/drinks NoLabel
# text: czy są dostępne jakieś sosy?
# intent: request/sauce
# slots:
1 czy request/sauce NoLabel
2 są request/sauce NoLabel
3 dostępne request/sauce NoLabel
4 jakieś request/sauce NoLabel
5 sosy? request/sauce NoLabel
# text: Grzegorz Pieczarski
# intent: inform/name
# slots:
1 Grzegorz inform/name B-name
2 Pieczarski inform/name I-name
# text: Sergiusz Kaczmarek
# intent: inform/name
# slots:
1 Sergiusz inform/name B-name
2 Kaczmarek inform/name I-name
# text: jaki koszt dowozu
# intent: request/delivery-price
# slots:
1 jaki request/delivery-price NoLabel
2 koszt request/delivery-price NoLabel
3 dowozu request/delivery-price NoLabel
# text: jakie sosy w menu?
# intent: request/sauce
# slots:
1 jakie request/sauce NoLabel
2 sosy request/sauce NoLabel
3 w request/sauce NoLabel
4 menu? request/sauce NoLabel
# text: Napój pepsi i cola
# intent: inform/order
# slots:
1 Napój inform/order NoLabel
2 pepsi inform/order B-drink
3 i inform/order NoLabel
4 cola inform/order B-drink
# text: woda i sok
# intent: inform/order
# slots:
1 woda inform/order B-drink
2 i inform/order NoLabel
3 sok inform/order B-drink

View File

@ -4,7 +4,30 @@ import pandas as pd
import numpy as np import numpy as np
from nlu_utils import predict_multiple from nlu_utils import predict_multiple
from flair.models import SequenceTagger from flair.models import SequenceTagger
from conllu import parse_incr
from flair.data import Corpus
from nlu_utils import conllu2flair, nolabel2o
# Frame model evaluation
frame_model = SequenceTagger.load('frame-model-prod/best-model.pt')
with open('data/test_dialog_46.conllu', encoding='utf-8') as trainfile:
testset = list(parse_incr(trainfile, fields=['id', 'form', 'frame', 'slot'], field_parsers={}))
corpus = Corpus(test=conllu2flair(testset, "frame"))
result = frame_model.evaluate(corpus.test, mini_batch_size=1, gold_label_type="frame")
print(result.detailed_results)
# Slot model evaluation
slot_model = SequenceTagger.load('slot-model-prod/best-model.pt')
with open('data/test_dialog_46.conllu', encoding='utf-8') as trainfile:
testset = list(parse_incr(trainfile, fields=['id', 'form', 'frame', 'slot'], field_parsers={'slot': nolabel2o}))
corpus = Corpus(test=conllu2flair(testset, "slot"))
result = slot_model.evaluate(corpus.test, mini_batch_size=8, gold_label_type="slot")
print(result.detailed_results)
# Custom evaluation
def __parse_acts(acts): def __parse_acts(acts):
acts_split = acts.split('&') acts_split = acts.split('&')
remove_slot_regex = "[\(\[].*?[\)\]]" remove_slot_regex = "[\(\[].*?[\)\]]"
@ -13,10 +36,6 @@ def __parse_acts(acts):
def __parse_predictions(predictions): def __parse_predictions(predictions):
return set(prediction.split('/')[0] for prediction in predictions) return set(prediction.split('/')[0] for prediction in predictions)
# Exploratory tests
frame_model = SequenceTagger.load('frame-model-prod/best-model.pt')
# slot_model = SequenceTagger.load('slot-model-prod/final-model.pt')
total_acts = 0 total_acts = 0
act_correct_predictions = 0 act_correct_predictions = 0
slot_correct_predictions = 0 slot_correct_predictions = 0
@ -41,5 +60,4 @@ for file_name in os.listdir('data'):
if act in predictions: if act in predictions:
act_correct_predictions += 1 act_correct_predictions += 1
print(f"Accuracy - predicting acts: {(act_correct_predictions / total_acts)*100} ({act_correct_predictions}/{total_acts})") print(f"Accuracy - predicting acts: {(act_correct_predictions / total_acts)*100} ({act_correct_predictions}/{total_acts})")

View File

@ -8,24 +8,20 @@ from flair.models import SequenceTagger
from flair.trainers import ModelTrainer from flair.trainers import ModelTrainer
from nlu_utils import conllu2flair, nolabel2o from nlu_utils import conllu2flair, nolabel2o
import random
import torch import torch
random.seed(42)
torch.manual_seed(42)
if torch.cuda.is_available(): if torch.cuda.is_available():
torch.cuda.manual_seed(0)
torch.cuda.manual_seed_all(0)
torch.backends.cudnn.enabled = False torch.backends.cudnn.enabled = False
torch.backends.cudnn.benchmark = False torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True torch.backends.cudnn.deterministic = True
def train_model(label_type, field_parsers = {}): def train_model(label_type, field_parsers = {}):
with open('data/train_dialog.conllu', encoding='utf-8') as trainfile: with open('data/train_dialog.conllu', encoding='utf-8') as f:
trainset = list(parse_incr(trainfile, fields=['id', 'form', 'frame', 'slot'], field_parsers=field_parsers)) trainset = list(parse_incr(f, fields=['id', 'form', 'frame', 'slot'], field_parsers=field_parsers))
with open('data/test_dialog_46.conllu', encoding='utf-8') as f:
testset = list(parse_incr(f, fields=['id', 'form', 'frame', 'slot'], field_parsers=field_parsers))
corpus = Corpus(train=conllu2flair(trainset, label_type), test=conllu2flair(trainset, label_type)) breakpoint()
corpus = Corpus(train=conllu2flair(trainset, label_type), test=conllu2flair(testset, label_type))
label_dictionary = corpus.make_label_dictionary(label_type=label_type) label_dictionary = corpus.make_label_dictionary(label_type=label_type)
embedding_types = [ embedding_types = [
@ -39,8 +35,8 @@ def train_model(label_type, field_parsers = {}):
tagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=label_dictionary, tag_type=label_type, use_crf=True, tag_format="BIO") tagger = SequenceTagger(hidden_size=256, embeddings=embeddings, tag_dictionary=label_dictionary, tag_type=label_type, use_crf=True, tag_format="BIO")
frame_trainer = ModelTrainer(tagger, corpus) frame_trainer = ModelTrainer(tagger, corpus)
frame_trainer.train(f'{label_type}-model', learning_rate=0.1, mini_batch_size=32, max_epochs=75, train_with_dev=False) frame_trainer.train(f'{label_type}-model', learning_rate=0.1, mini_batch_size=16, max_epochs=75, train_with_dev=False)
if __name__ == '__main__': if __name__ == '__main__':
train_model("frame") train_model("frame")
train_model('slot', field_parsers={'slot': nolabel2o}) # train_model('slot', field_parsers={'slot': nolabel2o})

View File

@ -25,7 +25,6 @@ def conllu2flair_frame(sentences, label=None):
def conllu2flair_slot(sentences, label=None): def conllu2flair_slot(sentences, label=None):
fsentences = [] fsentences = []
for sentence in sentences: for sentence in sentences:
fsentence = Sentence(' '.join(token['form'] for token in sentence), use_tokenizer=False) fsentence = Sentence(' '.join(token['form'] for token in sentence), use_tokenizer=False)
start_idx = None start_idx = None
@ -35,6 +34,8 @@ def conllu2flair_slot(sentences, label=None):
if label: if label:
for idx, (token, ftoken) in enumerate(zip(sentence, fsentence)): for idx, (token, ftoken) in enumerate(zip(sentence, fsentence)):
if token[label].startswith('B-'): if token[label].startswith('B-'):
if start_idx is not None:
fsentence[start_idx:end_idx+1].add_label(label, tag)
start_idx = idx start_idx = idx
end_idx = idx end_idx = idx
tag = token[label][2:] tag = token[label][2:]