-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_model.py
More file actions
32 lines (28 loc) · 953 Bytes
/
run_model.py
File metadata and controls
32 lines (28 loc) · 953 Bytes
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
import sys
import pytext
config_file = sys.argv[1]
model_file = sys.argv[2]
config = pytext.load_config(config_file)
predictor = pytext.create_predictor(config, model_file)
id2name = {
'0': '数理科学',
'1': '化学科学',
'2': '生命科学',
'3': '地球科学',
'4': '工程与材料科学',
'5': '信息科学',
'6': '管理科学',
'7': '医学科学'
}
text = input('请输入一行文本:')
words = list(text.replace('\n', '').replace('\r', '').replace('\t', '').lower())
result = predictor({"raw_text":' '.join(words)})
doc_label_scores_prefix = (
'scores:' if any(r.startswith('scores:') for r in result)
else 'doc_scores:'
)
best_doc_label = max(
(label for label in result if label.startswith(doc_label_scores_prefix)),
key=lambda label: result[label][0],
)[len(doc_label_scores_prefix):]
print(id2name[best_doc_label])