python
# -*- coding: utf-8 -*-
import re
# 用户问题
question = "我想查询我的残疾人证"
# 文档列表(包含文件名和内容)
documents = [
{
"filename": "《中华人民共和国残疾人证》查询.txt",
"content": "《中华人民共和国残疾人证》查询"
},
{
"filename": "《中华人民共和国残疾人证》查询.txt",
"content": "事项名称:《中华人民共和国残疾人证》查询"
},
{
"filename": "《中华人民共和国残疾人证》查询.txt",
"content": "申请内容:《中华人民共和国残疾人证》查询"
},
{
"filename": "《中华人民共和国残疾人证》残损换新.txt",
"content": "中华人民共和国残疾人证"
},
{
"filename": "《中华人民共和国残疾人证》资料更新.txt",
"content": "中华人民共和国残疾人证"
},
{
"filename": "《中华人民共和国残疾人证》新办证申请.txt",
"content": "中华人民共和国残疾人证"
},
{
"filename": "《中华人民共和国残疾人证》残损换新.txt",
"content": "2、残疾人证污损、残缺不能辨认影响正常使用的,可向残联申请。"
},
{
"filename": "《中华人民共和国残疾人证》查询.txt",
"content": "1)使用姓名+残疾人证号码查询;2)查询结果查看。"
},
{
"filename": "《中华人民共和国残疾人证》资料更新.txt",
"content": "申请内容:《中华人民共和国残疾人证》资料更新"
},
{
"filename": "《中华人民共和国残疾人证》资料更新.txt",
"content": "《中华人民共和国残疾人证》资料更新"
},
{
"filename": "《中华人民共和国残疾人证》残损换新.txt",
"content": "申请内容:《中华人民共和国残疾人证》残损换新"
},
{
"filename": "《中华人民共和国残疾人证》信息查询(广东省内).txt",
"content": "《中华人民共和国残疾人证》信息查询(广东省内)"
},
{
"filename": "《中华人民共和国残疾人证》信息查询(广东省内).txt",
"content": "事项名称:《中华人民共和国残疾人证》信息查询(广东省内)"
},
{
"filename": "《中华人民共和国残疾人证》信息查询(广东省内).txt",
"content": "申请内容:《中华人民共和国残疾人证》信息查询(广东省内)"
},
{
"filename": "《中华人民共和国残疾人证》挂失补办.txt",
"content": "中华人民共和国残疾人证"
}
]
# 提取问题中的关键词
def extract_keywords(text):
# 使用简单的正则表达式提取中文词语
keywords = re.findall(r'[\u4e00-\u9fa5]+', text)
# 可以根据需要添加更多的停用词
stopwords = set(['我', '想', '我的'])
keywords = [word for word in keywords if word not in stopwords]
return keywords
# 计算文档的相关性得分
def calculate_relevance(question_keywords, document_content):
relevance = 0
for keyword in question_keywords:
if keyword in document_content:
relevance += 1
return relevance
# 提取问题关键词
question_keywords = extract_keywords(question)
# 计算每个文档的相关性得分
document_scores = []
for doc in documents:
score = calculate_relevance(question_keywords, doc['content'])
document_scores.append({
'filename': doc['filename'],
'score': score
})
# 按照相关性得分从高到低排序
sorted_documents = sorted(document_scores, key=lambda x: x['score'], reverse=True)
# 选择相关性得分最高的文档
max_score = sorted_documents[0]['score']
relevant_documents = [doc for doc in sorted_documents if doc['score'] == max_score and max_score > 0]
# 输出选择的文档
print("相关的文档有:")
for doc in relevant_documents:
print(f"文件名: {doc['filename']}, 相关性得分: {doc['score']}")
code
相关的文档有:
文件名: 《中华人民共和国残疾人证》查询.txt, 相关性得分: 2
文件名: 《中华人民共和国残疾人证》查询.txt, 相关性得分: 2
文件名: 《中华人民共和国残疾人证》查询.txt, 相关性得分: 2
文件名: 《中华人民共和国残疾人证》查询.txt, 相关性得分: 2
文件名: 《中华人民共和国残疾人证》信息查询(广东省内).txt, 相关性得分: 2
文件名: 《中华人民共和国残疾人证》信息查询(广东省内).txt, 相关性得分: 2
文件名: 《中华人民共和国残疾人证》信息查询(广东省内).txt, 相关性得分: 2