关于考题和书籍上知识相似度匹配的想法实现

首先短文相似度的关键算法用百度AI里面提供的API
其实考题和书籍上知识是提前做出excel表的,用python的pandas库进行处理

说实话实际效果是有点,但并不好,所以不推荐

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

你可能感兴趣的:(关于考题和书籍上知识相似度匹配的想法实现)