forked from pencoa/PCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
69 lines (51 loc) · 1.79 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
62
63
64
65
66
67
68
69
from model.data_utils import getDataset, pos_constrain
from model.pcnn_model import PCNNModel
from model.config import Config
def interactive_shell(model):
"""Creates interactive shell to play with model
Args:
model: instance of PCNNModel
"""
model.logger.info("""
This is an interactive mode.
To exit, enter 'exit'.
You can enter a sentence like
input sentence> Steve_Jobs is one co-founder of Apple_Inc.""")
while True:
sentence = input("input sentence> ")
entity1 = input("input entity1> ")
entity2 = input("input entity2> ")
sequence = sentence.split()
if words_raw == ["exit"]:
break
if entity1 not in sentence or entity2 not in sentence:
print("entity not found in sentence.")
break
ent1 = sentence.index(entity1)
ent2 = sentence.index(entity2)
words, pos1_ids, pos2_ids = [], [], []
for idx, word in enumerate(sequence):
words.append(word)
pos1 = pos_constrain(idx - ent1)
pos1_ids.append(pos1)
pos2 = pos_constrain(idx - ent2)
pos2_ids.append(pos2)
pos = [ent1, ent2, len(sequence)-1]
pos.sort()
preds = model.predict(words, pos1_ids, pos2_ids, pos)
print("{}, {}, {}".format(entity1, preds, entity2))
def main():
# create instance of config
config = Config()
# build model
model = PCNNModel(config)
model.build()
model.restore_session(config.restore_model)
# create dataset
test = getDataset(config.filename_test, config.processing_word,
config.processing_tag, config.max_iter)
# evaluate and interact
model.evaluate(test)
interactive_shell(model)
if __name__ == "__main__":
main()