华为云NLP服务动手实践之分词
【摘要】 未完待续...
得先获取token,其实测试一些云服务的api用postman最简单,不过要是动手实践写代码的话,还是实践一些代码比较实在:
先获取token,
def get_token(username, password, area): """ username, 华为云用户名 password, 华为云密码,注意,如果带有特殊字符,如“@”神马的,需要转义 area,哪个区,如北京四,可以写“cn-north-4” """ auth_endpoint = "https://iam." + area + ".myhuaweicloud.com/v3/auth/tokens" header = {'content-type':'application/json'} data = { "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "name": username, "password": password, "domain": { "name": username } } } }, "scope": { "project": { "name": area } } } } #print(auth_endpoint) try: r = requests.post(url = auth_endpoint, headers = header, json = data, verify = False) except: #其实这样不规范 print("get token error") token = r.headers['x-subject-token'] return token
有了token,加上project_id就可以分词了,
def segment(token, area, project_id, text, pos_switch, criterion, lang): #project_id可以在我的凭证里面找到,不同的project对应不同的id url = "https://nlp-ext." + area + ".myhuaweicloud.com/v1/" + project_id + "/nlp-fundamental/segment" header = {'content-type':'application/json', 'X-Auth-Token':token} data = { "pos_swtich":pos_switch, "text":text, "lang":lang, "criterion":criterion } try: r = requests.post(url, json = data, headers = header, verify = False) #如果有代理,也可以加进来, "proojeces = proxy" print(r.text)#方便调试 if(int(r.status_code) >= 600): logger.info('status_code:%s; url:%s'%(r.status_code, url)) logger.error('Error: code:%s; msg:%s'%(r.status_code, r.text)) seg = r.json()['words'] return seg except: logger.error("Error: segment...") return ''
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)