-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.py
75 lines (59 loc) · 1.55 KB
/
api_test.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
70
71
72
73
74
75
import os
import sys
from textcrafts.deepRank import maybeWord, isAny, pdf2txt
from textcrafts import GraphMaker
from textcrafts.corenlp_api import *
from textcrafts.stanfordnlp_api import *
from timeit import timeit as tm
def apply_api(api, fname):
with open(fname, 'r') as f:
ls = f.readlines()
text = "".join(ls)
return api(text).get_all()
def t1():
print('with coreNLP')
print('')
text = 'The happy cat sleeps. The dog just barks today.'
p = CoreNLP_API(text)
print(p.get_triples())
print('')
print(p.get_lemmas())
print('')
print(p.get_words())
print('')
print(p.get_triples())
print('-'*50)
print('')
def t2():
print('with stanfordnlp - torch based')
print('')
text = 'The happy cat sleeps. The dog just barks today.'
p = StanTorch_API(text)
print(p.get_triples())
print('')
print(p.get_lemmas())
print('')
print(p.get_words())
print('')
print(p.get_tags())
print('')
# benchmark
def bm1(fname):
api = CoreNLP_API
(ds, ls, ws, _) = apply_api(api, fname)
print('coreNLP', 'sents=', len(ws))
def bm2(fname):
api = StanTorch_API
(ds, ls, ws, _) = apply_api(api, fname)
print('stanfordnlp', 'sents=', len(ws))
def bm():
fname = 'examples/const.txt'
#fname = 'examples/einstein.txt'
#fname = 'examples/tesla.txt'
for _ in range(3):
print(fname,tm(lambda: bm1(fname), number=1))
print(fname,tm(lambda: bm2(fname), number=1))
if __name__ == '__main__':
# t1()
t2()
# bm()