-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwordseg.py
executable file
·50 lines (38 loc) · 1.2 KB
/
wordseg.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
# encoding: utf-8
import requests
import json
import os
curdir = os.path.dirname(os.path.abspath(__file__))
# config
URL = 'http://corsair:3007/api/v1'
SEGWORD_TYPE = "nostopword"
INPUT_DATA = "%s/data/zhwiki-latest-pages-articles.0620.chs.normalized" % curdir
def rest_post(endpoint, data):
headers = {"Accept": "application/json",
"Content-Type": "application/json"}
# call get service with headers and params
response = requests.post(
'%s%s' %
(URL, endpoint), data=json.dumps(data), headers=headers)
return response.json()
def segment_sentence(sentence, type=SEGWORD_TYPE):
result = rest_post('/tokenizer', data=dict({
"type": type,
"content": sentence
}))
concat = []
if(result["status"] == "success"):
[concat.append(data["word"]) for data in result["data"]]
else:
raise "fail to get response"
return " ".join(concat)
def load_all_data():
with open(INPUT_DATA, "r", encoding="utf-8") as infile:
for line in infile:
yield line
if __name__ == "__main__":
for x in load_all_data():
try:
print(segment_sentence(x))
except BaseException:
pass