Ideas about the questions and book knowledge to achieve similarity matching

The key algorithm first essay similarity with Baidu AI which increased API
is actually on the exam are made in advance and book knowledge excel table, the pandas were treated with python libraries
---

import  pandas  as pd
import codecs
import chardet
from aip import AipNlp

#百度API的短文相似度文本处理的关键代码
APP_ID = '18141823'
API_KEY = 'eEmvBrXfCdexVmjAyoPNBoxE'
SECRET_KEY = 'p10xZogTbVDe7PphkB9zIjyZ8QkRBAqu'
client = AipNlp(APP_ID,API_KEY,SECRET_KEY)
#client.simnet(txt1,txt2) txt1和txt2不能超过512个字节

df1=pd.read_excel('Article_guanli.xlsx')
df2=pd.read_excel('Topic_guanli.xlsx')
test_data=[]
height1,width1 = df1.shape
height2,width2 = df2.shape


for i in xrange(0,1):
    for j in xrange(0,height1):
        try:
            txt1 = df1.ix[j,0].encode('utf-8')
            txt2 = df2.ix[i,0].encode('utf-8')
        
            ret = client.simnet(str(txt1),str(txt2))
            while("error_code" in ret):
                ret = client.simnet(str(txt1),str(txt2))
        
            print ret

            f = codecs.open('xiangsidu.txt','a',encoding="utf-8")
            k = ret['texts']['text_1'] + "#" + ret['texts']['text_2'] + "#" + str(ret['score'])
            f.write(k + "\n")
        except:
            pass
        continue

Guess you like

Origin www.cnblogs.com/CQ-LQJ/p/12122259.html