-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
301 lines (237 loc) · 9.94 KB
/
Copy pathdemo.py
File metadata and controls
301 lines (237 loc) · 9.94 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# coding:utf-8
"""
Utility Tools for NLP
=====================
Demo of the interfaces of parts-of-speech tagging and dependency parsing.
Input: string
Tag output: tagged_list = [(word, pos, ...), (word, pos, ...), ...]
Parse output members: tagged_list, dep_list, dep_graph, root_index
Parse output functions: get_dep_tree, left_edge, right_edge
"""
# todo: doc
# todo: provide conll_10 interface
# todo: import python package -- auto check
# todo: check model data -- auto config or download gzip
# TextBlob
# use NLTK and pattern.en library.
__author__ = "GE Ning <https://github.com/gening/>"
__copyright__ = "Copyright (C) 2017 GE Ning"
__license__ = "LGPL-3.0"
__version__ = "1.0"
from pprint import pprint
# noinspection PyCompatibility
doc_en = (u'Vincent Willem van Gogh was a Dutch Post-Impressionist painter '
u'who is among the most famous and influential figures in the history of Western art. '
u'In just over a decade he created about 2,100 artworks, '
u'including around 860 oil paintings, '
u'most of them in the last two years of his life in France, where he died.\n '
u'They include landscapes, still lives, portraits and self-portraits, '
u'and are characterised by bold colours and dramatic, '
u'impulsive and expressive brushwork that contributed to the foundations of modern art. '
u'His suicide at 37 followed years of mental illness and poverty.')
# noinspection PyCompatibility
sent_en = (u'Vincent Willem van Gogh was a Dutch Post-Impressionist painter '
u'who is among the most famous and influential figures in the history of Western art.')
# noinspection PyCompatibility
doc_zh = (u'莫奈(Claude Monet,1840年11月14日-1926年12月5日),'
u'是法国最重要的画家之一,被誉为“印象派领导者”。'
u'莫奈擅长光与影的实验与表现技法,他改变了阴影和轮廓线的绘画风格。\n '
u'在莫奈的画作中看不到非常明确的阴影,也看不到突显或平涂式的轮廓线。')
# noinspection PyCompatibility
sent_zh = (u'莫奈(Claude Monet,1840年11月14日-1926年12月5日),'
u'是法国最重要的画家之一,被誉为“印象派领导者”。')
"""
For English text
----------------
"""
def print_parsed_result_en(parsed_sent):
from nltk.tree import Tree
tagged_list = parsed_sent.tagged_list
dep_list = parsed_sent.dep_list
dep_tree = parsed_sent.get_dep_tree()
pprint(tagged_list)
pprint(dep_list)
pprint(dep_tree)
Tree.fromstring(dep_tree).draw()
def demo_nltk_nlp_tag_en():
from nlp_util import nltk_nlp
for tagged_list in nltk_nlp.tag_with_ssplit(doc_en):
pprint(tagged_list)
def demo_stanford_nltk_tag_en():
from nlp_util.stanford_nltk_nlp import StanfordNLP
stanford_nlp = StanfordNLP('en')
tagged_list = stanford_nlp.tag(sent_en)
pprint(tagged_list)
def demo_stanford_nltk_parse_en():
from nlp_util.stanford_nltk_nlp import StanfordNLP
stanford_nlp = StanfordNLP('en')
for parsed_sent in stanford_nlp.parse_with_ssplit(doc_en):
print_parsed_result_en(parsed_sent)
def demo_stanford_corenlp_tag_en():
from nlp_util.stanford_corenlp import StanfordNLP
with StanfordNLP('en') as stanford_nlp:
for tagged_list in stanford_nlp.tag_with_ssplit(doc_en):
pprint(tagged_list)
def demo_stanford_corenlp_parse_en():
from nlp_util.stanford_corenlp import StanfordNLP
with StanfordNLP('en') as stanford_nlp:
for parsed_sent in stanford_nlp.parse_with_ssplit(doc_en):
print_parsed_result_en(parsed_sent)
def demo_stanford_corenlp_parse_en_without_ssplit():
from nlp_util.stanford_corenlp import StanfordNLP
with StanfordNLP('en') as stanford_nlp:
for parsed_sent in stanford_nlp.parse(doc_en):
print_parsed_result_en(parsed_sent)
def demo_spacy_nlp_tag_en():
from nlp_util.spacy_nlp import SpaCyNLP
nlp = SpaCyNLP()
for tagged_list in nlp.tag(sent_en):
pprint(tagged_list)
def demo_spacy_nlp_parse_en():
from nlp_util.spacy_nlp import SpaCyNLP
from nltk.tree import Tree
nlp = SpaCyNLP()
for parsed_sent in nlp.parse_with_ssplit(doc_en):
pprint(parsed_sent.dep_graph)
print_parsed_result_en(parsed_sent)
try:
tagged_list = parsed_sent.tagged_list
dep_tree = parsed_sent.get_dep_tree(6)
Tree.fromstring(dep_tree).draw()
dep_tree = parsed_sent.get_dep_tree(24)
Tree.fromstring(dep_tree).draw()
parsed_sent._leaf_func = lambda index: tagged_list[index][0]
dep_tree = parsed_sent.get_dep_tree(parsed_sent.root_index)
Tree.fromstring(dep_tree).draw()
except ValueError as e:
print(e.message)
def demo_antwerp_nlp_tag_en():
from nlp_util import antwerp_nlp
for tagged_list in antwerp_nlp.tag_with_ssplit(doc_en):
pprint(tagged_list)
def demo_antwerp_nlp_parse_en():
from nlp_util import antwerp_nlp
for parsed_sent in antwerp_nlp.parse_with_ssplit(doc_en):
tagged_list = parsed_sent.tagged_list
rdf_triples = parsed_sent.get_rdf_triples()
pprint(tagged_list)
pprint(rdf_triples)
def demo_tf_dragnn_nlp_parse_en():
from nlp_util.tensorflow_dragnn_nlp import TfDragnnNLP
tf_dragnn_nlp = TfDragnnNLP('en')
# noinspection PyShadowingNames
for sent_en in doc_en.replace('\n', '').split('. '):
parsed_sent = tf_dragnn_nlp.parse(sent_en)
print_parsed_result_en(parsed_sent)
dragnn_sent, dragnn_trace_str = tf_dragnn_nlp.annotate("John is eating pizza with a fork")
# Also try: John is eating pizza with a fork
from nlp_util.tensorflow_dragnn_nlp import ParsedSent
# noinspection PyProtectedMember
from nlp_util.tensorflow_dragnn_nlp import _parse_tree_explorer
# noinspection PyProtectedMember
from nlp_util.tensorflow_dragnn_nlp import _trace_explorer
# noinspection PyProtectedMember
from nlp_util.tensorflow_dragnn_nlp import _browse_html
lookup_dict, node_num = ParsedSent.comprehend_dragnn_sent(dragnn_sent)
pprint(dragnn_sent)
pprint(lookup_dict)
pprint(node_num)
neural_graph_html = _trace_explorer(dragnn_trace_str)
_browse_html(neural_graph_html, 'temp_dragnn_graph.html')
dependency_tree_html = _parse_tree_explorer(dragnn_sent)
_browse_html(dependency_tree_html, 'temp_dragnn_tree.html')
"""
For Chinese text
----------------
"""
def print_tagged_result_zh(tagged_list):
if tagged_list:
if hasattr(tagged_list[0], '__iter__'):
tagged_str = ' '.join(['_'.join(map(unicode, token)) for token in tagged_list])
else:
tagged_str = ' '.join(tagged_list)
print(tagged_str)
def print_parsed_result_zh(parsed_sent):
from nltk.tree import Tree
tagged_list = parsed_sent.tagged_list
dep_list = parsed_sent.dep_list
dep_tree = parsed_sent.get_dep_tree()
print_tagged_result_zh(tagged_list)
pprint(dep_list)
print(dep_tree.encode('utf-8'))
Tree.fromstring(dep_tree).draw()
def demo_jieba_nlp_tag_zh():
from nlp_util import jieba_nlp
tagged_list = jieba_nlp.tag(sent_zh)
print_tagged_result_zh(tagged_list)
def demo_jieba_nlp_tag_zh_with_parallel():
from nlp_util import jieba_nlp
from time import sleep
with jieba_nlp.enable_parallel():
for i in range(30):
tagged_list = jieba_nlp.tag(sent_zh)
print_tagged_result_zh(tagged_list)
sleep(1)
def demo_hit_nlp_tag_zh():
import sys
import time
from nlp_util.hit_nlp import HITNLP
with HITNLP() as hit_nlp:
texts = [doc_zh, sent_zh]
iter_texts = (texts[i % 2] for i in range(400))
print(time.ctime())
for i, text in enumerate(iter_texts):
for tagged_list in hit_nlp.tag_with_ssplit(text):
sys.stdout.write('%3d\t' % i)
print_tagged_result_zh(tagged_list)
pass
print(time.ctime())
def demo_hit_nlp_parse_zh():
from nlp_util.hit_nlp import HITNLP
with HITNLP() as hit_nlp:
for parsed_sent in hit_nlp.parse_with_ssplit(doc_zh):
print_parsed_result_zh(parsed_sent)
def demo_stanford_nltk_tag_zh():
from nlp_util.stanford_nltk_nlp import StanfordNLP
stanford_nlp = StanfordNLP('zh')
tagged_list = stanford_nlp.tag(sent_zh)
print_tagged_result_zh(tagged_list)
def demo_stanford_nltk_parse_zh():
from nlp_util.stanford_nltk_nlp import StanfordNLP
stanford_nlp = StanfordNLP('zh')
try:
parsed_sent = stanford_nlp.parse(sent_zh)
if parsed_sent:
print_parsed_result_zh(parsed_sent)
except UnicodeDecodeError as e:
print(e.message)
def demo_stanford_corenlp_tag_zh():
from nlp_util.stanford_corenlp import StanfordNLP
with StanfordNLP('zh') as stanford_nlp:
for tagged_list in stanford_nlp.tag_with_ssplit(doc_zh):
print_tagged_result_zh(tagged_list)
def demo_stanford_corenlp_parse_zh():
from nlp_util.stanford_corenlp import StanfordNLP
with StanfordNLP('zh') as stanford_nlp:
for parsed_sent in stanford_nlp.parse_with_ssplit(doc_zh):
print_parsed_result_zh(parsed_sent)
def demo_tf_dragnn_nlp_parse_zh():
from nlp_util.tensorflow_dragnn_nlp import TfDragnnNLP
tf_dragnn_nlp = TfDragnnNLP('zh')
# noinspection PyCompatibility, PyShadowingNames
for sent_chi in doc_zh.replace('\n', '').split(u'。'):
sent_chi = sent_chi.strip()
if sent_chi != '':
# noinspection PyCompatibility
parsed_sent = tf_dragnn_nlp.parse(sent_chi + u'。')
print_parsed_result_zh(parsed_sent)
def show_demo():
# import nlp_util.xxx
# noinspection PyCompatibility
# reload(nlp_util.xxx)
# demo()
demo = 'demo_stanford_corenlp_parse_en'
import timeit
print(timeit.timeit(demo + '()', setup='from __main__ import ' + demo, number=1))
if __name__ == '__main__':
show_demo()