-
Notifications
You must be signed in to change notification settings - Fork 3
/
baidunpl.py
30 lines (27 loc) · 893 Bytes
/
baidunpl.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
from aip import AipNlp
import re
class BaiduNpl:
def __init__(self):
APP_ID = 'd491715c5684423e87657ac69449a8eb'
API_KEY = ''
SECRET_KEY = ''
self.client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
def getFenCi(self,text):
text = re.sub(r"[0-9\s+\.\!\/_,$%^*()?;;:-【】+\"\']+|[+——!,;:。?、~@#¥%……&*()]+", " ", text)
res=self.client.lexer(text)
ret=[]
for one in res["items"]:
ret.append(one["item"])
return ret
def getBaiduVec(self,word):
res=self.client.wordEmbedding(word)
return res
def getVecFromJuzi(self,text):
words=self.getFenCi(text)
vecs=[]
for word in words:
res=self.getBaiduVec(word)
if "vec" in res.keys():
vec=res["vec"]
vecs.append(vec)
return vecs