python问答模块

  1 """
  2 该模块功能:获取用户的输入文本,通过输入文本和数据库中的关键主题文本相比较,
  3 获取最佳的回答内容
  4 """
  5 import xlrd
  6 import jieba
  7 import nltk
  8 
  9 
 10 # 读取excel表格内的数据
 11 def read_excel(filepath):
 12     # 定义一个字典,动态建立一个字典
 13     map_dict = {}
 14     data = xlrd.open_workbook(filepath)
 15     table = data.sheets()[0]
 16     # 获取表格行数和列数
 17     nrows = table.nrows
 18     ncols = table.ncols
 19     # 获取表格第1列的内容,内容是一个list
 20     s = table.col_values(1)[1:]
 21     # 获取长度
 22     length = len(s)
 23     # 迭代每一个元素进行处理
 24     for each in range(length):
 25         each_string = s[each]
 26         # 分离出title和对应的解释
 27         word_key, word_value = each_string.split(':')
 28         # 把数据放入一个字典中,以键值对的形式存在
 29         map_dict[word_key] = word_value
 30 
 31     print(map_dict)
 32     return map_dict
 33     # 已经将数据放入到字典里,接下来是使用数据了
 34     # 通过语音输入一个关键字,这个关键字对应字典的键,采用什么样的模型实现这两个的映射,找到对应的键值就可以找到需要的答案,这就是思路
 35 
 36     # 假设已经获得了一个输入字符
 37     # input_string = "货币型基金"
 38     # 获取字典的键
 39     # title_key = list(map_dict.keys())
 40     # print(title_key)
 41 
 42     # 中间环节,实现input_string和title_key 的映射,相似度匹配
 43 
 44 
 45     # 最后,根据键获取值
 46     # content_value = map_dict.get(input_string)
 47     # print(content_value)
 48 
 49 # 统计key中各个词频
 50 def solve_word(word_key):
 51     seg_list = []
 52 
 53     for each in word_key:
 54         temp_string = list(jieba.cut(each))
 55         seg_list.extend(temp_string)
 56     print(len(seg_list))
 57     # 得到结果列表
 58     seg_list = set(seg_list)
 59     print(seg_list)
 60 
 61 
 62 # 计算两句话的相似程度模板
 63 #   句子A:我喜欢看电视,不喜欢看电影。
 64 #   句子B:我不喜欢看电视,也不喜欢看电影。
 65 def calc_sentence_similarity(sentence_A, sentence_B):
 66     # sentence_A = "我喜欢看电视,不喜欢看电影"
 67     # sentence_B = "我不喜欢看电视,计算两句话的相似程度模板"
 68 
 69     # 第一步,分词
 70     segment_A = list(jieba.cut(sentence_A))
 71     segment_B = list(jieba.cut(sentence_B))
 72     # print(segment_A)
 73     # print(segment_B)
 74 
 75     # 第二步,列出所有的词
 76     all_words = set(segment_A + segment_B)
 77     # print(all_words)
 78 
 79     # 第三步,统计词频。放到字典里面,遍历all_words,看句子A和句子B都是各有多少
 80 #   句子A:我 1,喜欢 2,看 2,电视 1,电影 1,不 1,也 0。
 81 #   句子B:我 1,喜欢 2,看 2,电视 1,电影 1,不 2,也 1。
 82     frequency_dict_A = {}
 83     frequency_dict_B = {}
 84     if all_words is not None:
 85         for t in all_words:
 86             frequency_dict_A[t] = segment_A.count(t)
 87             frequency_dict_B[t] = segment_B.count(t)
 88     # print(frequency_dict_A)
 89     # print(frequency_dict_B)
 90 
 91     # 第四步,写出词频向量
 92     word_frequency_vector_A = list(frequency_dict_A.values())
 93     word_frequency_vector_B = list(frequency_dict_B.values())
 94     # print(word_frequency_vector_A)
 95     # print(word_frequency_vector_B)
 96 
 97     # 第五步,计算两个向量的相似度,夹角的余弦
 98     return cos_vector(word_frequency_vector_A, word_frequency_vector_B)
 99 
100 
101 def cos_vector(x, y):
102     if(len(x) != len(y)):
103         print('输入错误,两个向量不在一个向量空间')
104         return
105     result1 = 0.0
106     result2 = 0.0
107     result3 = 0.0
108     for i in range(len(x)):
109         result1 += x[i]*y[i]   #sum(X*Y)
110         result2 += x[i]**2     #sum(X*X)
111         result3 += y[i]**2     #sum(Y*Y)
112     # print("两个向量的夹角的余弦值:"+str(result1/((result2*result3)**0.5))) #结果显示
113     return result1/((result2*result3)**0.5)
114 
115 
116 # 问句匹配,找出问题的结果
117 def find_result(input_string, word_dict):
118     # 获取用户的输入,将其和数据库中的每一个数据进行对比,计算相似度
119     # input_string = "票汇汇款"
120     temp_dict = {}
121     for temp_string in word_dict.keys():
122         # 计算相似度,key:要查找的字符串;value:相似度的值。max(d.items(),key=lambda x:x[1])[0]
123         temp_dict[temp_string] = calc_sentence_similarity(input_string, temp_string)
124 
125     max_value_key = max(temp_dict, key=temp_dict.get)
126     # 得到对应的文本
127     text_result = word_dict.get(max_value_key)
128     print(max_value_key)
129     print(text_result)
130 
131 #   调用主程序
132 def read_get_answer(file_path, input_string):
133     # 数据源,    file_path = "./data/word_instruction.xls"
134     # 获取用户文本,    input_string = "我想知道什么是止付卡"
135     # 读入数据,返回字典
136     word_dict = read_excel(file_path)
137     # 计算相似度
138     find_result(input_string, word_dict)
139 
140 
141 if __name__ == '__main__':
142     # 数据源
143     file_path = "./data/word_instruction.xls"
144     # 获取用户文本
145     input_string = "我想知道什么是止付卡"
146     # 读入数据,返回字典
147     word_dict = read_excel(file_path)
148     # 计算相似度
149     find_result(input_string, word_dict)
"""
该模块功能:获取用户的输入文本,通过输入文本和数据库中的关键主题文本相比较,
获取最佳的回答内容
"""
import xlrd
import jieba
import nltk


# 读取excel表格内的数据
def read_excel(filepath):
# 定义一个字典,动态建立一个字典
map_dict = {}
data = xlrd.open_workbook(filepath)
table = data.sheets()[0]
# 获取表格行数和列数
nrows = table.nrows
ncols = table.ncols
# 获取表格第1列的内容,内容是一个list
s = table.col_values(1)[1:]
# 获取长度
length = len(s)
# 迭代每一个元素进行处理
for each in range(length):
each_string = s[each]
# 分离出title和对应的解释
word_key, word_value = each_string.split(':')
# 把数据放入一个字典中,以键值对的形式存在
map_dict[word_key] = word_value

print(map_dict)
return map_dict
# 已经将数据放入到字典里,接下来是使用数据了
# 通过语音输入一个关键字,这个关键字对应字典的键,采用什么样的模型实现这两个的映射,找到对应的键值就可以找到需要的答案,这就是思路

# 假设已经获得了一个输入字符
# input_string = "货币型基金"
# 获取字典的键
# title_key = list(map_dict.keys())
# print(title_key)

# 中间环节,实现input_string和title_key 的映射,相似度匹配


# 最后,根据键获取值
# content_value = map_dict.get(input_string)
# print(content_value)

# 统计key中各个词频
def solve_word(word_key):
seg_list = []

for each in word_key:
temp_string = list(jieba.cut(each))
seg_list.extend(temp_string)
print(len(seg_list))
# 得到结果列表
seg_list = set(seg_list)
print(seg_list)


# 计算两句话的相似程度模板
#   句子A:我喜欢看电视,不喜欢看电影。
#   句子B:我不喜欢看电视,也不喜欢看电影。
def calc_sentence_similarity(sentence_A, sentence_B):
# sentence_A = "我喜欢看电视,不喜欢看电影"
# sentence_B = "我不喜欢看电视,计算两句话的相似程度模板"

# 第一步,分词
segment_A = list(jieba.cut(sentence_A))
segment_B = list(jieba.cut(sentence_B))
# print(segment_A)
# print(segment_B)

# 第二步,列出所有的词
all_words = set(segment_A + segment_B)
# print(all_words)

# 第三步,统计词频。放到字典里面,遍历all_words,看句子A和句子B都是各有多少
#   句子A:我 1,喜欢 2,看 2,电视 1,电影 1,不 1,也 0。
#   句子B:我 1,喜欢 2,看 2,电视 1,电影 1,不 2,也 1。
frequency_dict_A = {}
frequency_dict_B = {}
if all_words is not None:
for t in all_words:
frequency_dict_A[t] = segment_A.count(t)
frequency_dict_B[t] = segment_B.count(t)
# print(frequency_dict_A)
# print(frequency_dict_B)

# 第四步,写出词频向量
word_frequency_vector_A = list(frequency_dict_A.values())
word_frequency_vector_B = list(frequency_dict_B.values())
# print(word_frequency_vector_A)
# print(word_frequency_vector_B)

# 第五步,计算两个向量的相似度,夹角的余弦
return cos_vector(word_frequency_vector_A, word_frequency_vector_B)


def cos_vector(x, y):
if(len(x) != len(y)):
print('输入错误,两个向量不在一个向量空间')
return
result1 = 0.0
result2 = 0.0
result3 = 0.0
for i in range(len(x)):
result1 += x[i]*y[i] #sum(X*Y)
result2 += x[i]**2 #sum(X*X)
result3 += y[i]**2 #sum(Y*Y)
# print("两个向量的夹角的余弦值:"+str(result1/((result2*result3)**0.5))) #结果显示
return result1/((result2*result3)**0.5)


# 问句匹配,找出问题的结果
def find_result(input_string, word_dict):
# 获取用户的输入,将其和数据库中的每一个数据进行对比,计算相似度
# input_string = "票汇汇款"
temp_dict = {}
for temp_string in word_dict.keys():
# 计算相似度,key:要查找的字符串;value:相似度的值。max(d.items(),key=lambda x:x[1])[0]
temp_dict[temp_string] = calc_sentence_similarity(input_string, temp_string)

max_value_key = max(temp_dict, key=temp_dict.get)
# 得到对应的文本
text_result = word_dict.get(max_value_key)
print(max_value_key)
print(text_result)

# 调用主程序
def read_get_answer(file_path, input_string):
# 数据源, file_path = "./data/word_instruction.xls"
# 获取用户文本, input_string = "我想知道什么是止付卡"
# 读入数据,返回字典
word_dict = read_excel(file_path)
# 计算相似度
find_result(input_string, word_dict)


if __name__ == '__main__':
# 数据源
file_path = "./data/word_instruction.xls"
# 获取用户文本
input_string = "我想知道什么是止付卡"
# 读入数据,返回字典
word_dict = read_excel(file_path)
# 计算相似度
find_result(input_string, word_dict)



原文地址:https://www.cnblogs.com/demo-deng/p/8398823.html