forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.py
56 lines (44 loc) · 1.35 KB
/
simple.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
import torch
from ltp import LTP
def legacy():
ltp = LTP("LTP/legacy")
ltp.add_word("汤姆去")
result = ltp(
["他叫汤姆去拿外衣。", "树上停着一些小鸟。先飞走了19只,又飞走了15只。两次共飞走了多少只小鸟?"],
tasks=["cws", "pos", "ner"],
)
print(result.cws)
print(result.pos)
print(result.ner)
def neural():
ltp = LTP("LTP/tiny")
if torch.cuda.is_available():
ltp = ltp.to("cuda")
ltp.add_word("汤姆去")
# 未分词的文本
result = ltp.pipeline(
["他叫汤姆去拿外衣。", "韓語:한국의 단오", "树上停着一些小鸟。先飞走了19只,又飞走了15只。两次共飞走了多少只小鸟?"],
tasks=["cws"],
)
print(result.cws)
print(result.pos)
print(result.ner)
print(result.srl)
print(result.dep)
print(result.sdp)
# 已经分词的文本
result = ltp.pipeline(
[["他", "叫", "汤姆", "去", "拿", "外衣", "。"], ["가을동", "叫", "1993", "年", "的", "Ameri", "·"]],
# 注意这里移除了 "cws" 任务
tasks=["pos", "ner", "srl", "dep", "sdp"],
)
print(result.pos)
print(result.ner)
print(result.srl)
print(result.dep)
print(result.sdp)
def main():
legacy()
neural()
if __name__ == "__main__":
main()