forked from Disiok/poetry-seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
61 lines (40 loc) · 1.36 KB
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env python
#-*- coding:utf-8 -*-
import numpy as np
from quatrains import get_quatrains
from rhyme import RhymeEvaluator
from predict import Seq2SeqPredictor
from plan import Planner
from IPython import embed
def eval_poems(evaluator, poems):
scores = []
for poem in poems:
score = evaluator.eval(poem)
scores.append(score)
mean_score = np.mean(scores)
std_score = np.std(scores)
print "Mean score = {}, standard deviation = {}".format(mean_score, std_score)
return scores, mean_score, std_score
def eval_train_data():
evaluator = RhymeEvaluator()
quatrains = get_quatrains()
poems = map(lambda quatrain: quatrain['sentences'], quatrains) # Strip out metadata information
print "Testing {} quatrains from the corpus.".format(len(poems))
eval_poems(evaluator, poems)
def eval_generated_data(num=100):
evaluator = RhymeEvaluator()
planner = Planner()
predictor = Seq2SeqPredictor()
poems = []
for _ in range(num):
keywords = planner.plan(u'')
assert 4 == len(keywords)
sentences = predictor.predictor(keywords)
poems.append(sentences)
print "Testing {} quatrains generated by model.".format(num)
eval_poems(evaluator, poems)
def main():
eval_train_data()
# eval_generated_data()
if __name__ == '__main__':
main()