|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
有没有大佬会用python做一个简单的搜索引擎,这个是我们的期末作业report7,会的还请帮一下孩子
问题,保存好3个文件file1.txt、file2.txt、file3.txt,文件里的内容:
file1.txt:the cat sat on the mat
file2.txt:the dog stood on the mat
file3.txt:the cat stood while a dog sat
然后通过自己写一段代码
需要实现的目标结果
下面是一个实现后的Python代码示例
>>> import report7
>>> filenames = ['file1.txt', 'file2.txt', 'file3.txt']
index生成部
>>> index = report7.make_index(filenames)
>>> print(len(index))
9
>>> print(index)
{'the': ['file1.txt', 'file2.txt', 'file3.txt'], 'cat': ['file1.txt', 'file3.txt'], 'sat': ['file1.txt', 'file3.txt'], 'on': ['file1.txt', 'file2.txt'], 'mat': ['file1.txt', 'file2.txt'], 'dog': ['file2.txt', 'file3.txt'], 'stood': ['file2.txt', 'file3.txt'], 'while': ['file3.txt'], 'a': ['file3.txt']}
匹配部1: 「cat」搜索结果
>>> print(report7.search_words(index, ['cat']))
['file1.txt', 'file3.txt']
匹配部2: 「cat dog」搜索结果
>>> print(report7.search_words(index, ['cat', 'dog']))
['file3.txt']
如果输入「hoge」这个词在3个文件中都是没有的,所以搜索结果
>>> print(report7.search_words(index, ['hoge']))
None
>>> print(type(report7.search_words(index, ['hoge'])))
<class 'NoneType'>
*注意,它不返回字符串“ None”,而是返回class的NoneType。
函数名1: make_index
函数名2: search_words
有大佬会的话还请帮下小弟,万分感谢!
看看这个能不能达到要求吧:
def make_index(filenames):
word_dict = {}
for filename in filenames:
with open(filename,encoding='utf-8') as file:
word_list = file.read().split()
for word in set(word_list):
if word not in word_dict.keys():
word_dict[word] = [filename]
else:
word_dict[word].append(filename)
return word_dict
def search_words(word_dict,word_list):
items_ = word_dict.items()
filenames = []
for i in items_:
if i[0] in word_list:
filenames.append(i[1])
result = set()
for i in filenames:
if result == set():
result |= set(i)
continue
result &= set(i)
if result == set():
return None
return list(result)
"""_______测试使用时候删掉以下内容_________________"""
filenames = ['file1.txt', 'file2.txt', 'file3.txt']
index = make_index(filenames)
print(index)
print(search_words(index,['cat','dog']))
|
|