-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
50 lines (39 loc) · 1.55 KB
/
app.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
import pickle
from textwrap import wrap
from utils import get_sentences
def load_model(fn):
with open(fn, mode='rb') as f:
model = pickle.load(f)
f.close()
return model
def fill(text, width):
text_len = len(text)
head_space = (width - text_len) // 2
tail_space = width - text_len - head_space
return ''.join([' ' * head_space, text, ' ' * tail_space])
def print_result(sentences, tags):
max_words_per_line = 15
for sentence, tag in zip(sentences, tags):
formated_sentence = []
formated_tag = []
for (token,), tag_ in zip(sentence, tag):
max_len = max(len(token), len(tag_)) + 4
formated_sentence.append(fill(token, max_len))
formated_tag.append(fill(tag_, max_len))
no_lines = len(formated_sentence) // max_words_per_line + (
0 if len(formated_sentence) % max_words_per_line == 0 else 1)
for i in range(no_lines):
print(' '.join(formated_sentence[max_words_per_line * i: max_words_per_line * (i + 1)]))
print(' '.join(formated_tag[max_words_per_line * i: max_words_per_line * (i + 1)]))
print('\n')
if __name__ == '__main__':
model = load_model('data/model/crf_model_no_pos_chunk.pkl')
is_stop = False
while not is_stop:
paragraph = input('Enter a paragraph: ')
if paragraph == 'n' or paragraph == 'N':
is_stop = True
else:
sentences = get_sentences(paragraph)
tags = model.predict(sentences)
print_result(sentences, tags)