python如何高级检索
答案:1 悬赏:30 手机版
解决时间 2021-02-23 00:58
- 提问者网友:藍了天白赴美
- 2021-02-22 10:23
上个检索实验中检索功能要求输出包含全部关键字的行,即各个关键字的“与”(AND)操作。本实验首先需要实现关键字的“或”(OR)操作,即某行只要出现了一个关键字就满足条件。实现程序,当输入的Query以“AND:”开始,则执行“与”检索;如果输入的Query以“OR:”开始,则执行“或”检索。默认情况(不以“AND:”或“OR:”开始),执行“与”检索。 本实验无需打印索引
最佳答案
- 五星知识达人网友:旧脸谱
- 2021-02-22 12:02
$ python baidu_qa.py
Enter Context, leave by empty line
def __init__(self, datafile, default=None):
self.datafile = datafile
self.default = default
self.data = shelve.BsdDbShelf(bsddb.open(self.datafile, 'c'))
def __del__(self):
self.data.sync()
self.data.close()
def __getitem__(self, k):
return self.data.get(k, self.default)
def __setitem__(self, k, v):
self.data[k] = v
Enter and/or keywords to query
and default data
[9]
or get k v
[8, 9, 10, 11]
$
# coding: utf-8
import re
patt = re.compile("\w+")
def makeIndex():
index, lineno = {}, 0
while True:
ln = raw_input()
if ln:
lineno += 1
for word in patt.findall(ln):
index.setdefault(word, []).append(lineno)
else:
break
return index
def indexAndQuery(index, *args):
found = None
for word in args:
got = index.get(word, [])
if not got:
return None
if not found:
found = set(got)
else:
found &= set(got)
if not found:
return None
return list(found)
def indexOrQuery(index, *args):
found = set()
for word in args:
found |= set(index.get(word, []))
return list(found)
def lnparser(ln):
words = patt.findall(ln.lower())
if len(words)<2 or words[0] not in ('and','or'):
# 输入的行不是以and, or开始, 或未给出待查单词
return None
else:
return words[0], words[1:]
cmdswitch = {
'and': indexAndQuery,
'or': indexOrQuery,
}
print "Enter Context, leave by empty line"
index = makeIndex()
print "Enter and/or keywords to query"
while True:
got = lnparser(raw_input())
if not got:
break
print cmdswitch[got[0]](index, *got[1])
Enter Context, leave by empty line
def __init__(self, datafile, default=None):
self.datafile = datafile
self.default = default
self.data = shelve.BsdDbShelf(bsddb.open(self.datafile, 'c'))
def __del__(self):
self.data.sync()
self.data.close()
def __getitem__(self, k):
return self.data.get(k, self.default)
def __setitem__(self, k, v):
self.data[k] = v
Enter and/or keywords to query
and default data
[9]
or get k v
[8, 9, 10, 11]
$
# coding: utf-8
import re
patt = re.compile("\w+")
def makeIndex():
index, lineno = {}, 0
while True:
ln = raw_input()
if ln:
lineno += 1
for word in patt.findall(ln):
index.setdefault(word, []).append(lineno)
else:
break
return index
def indexAndQuery(index, *args):
found = None
for word in args:
got = index.get(word, [])
if not got:
return None
if not found:
found = set(got)
else:
found &= set(got)
if not found:
return None
return list(found)
def indexOrQuery(index, *args):
found = set()
for word in args:
found |= set(index.get(word, []))
return list(found)
def lnparser(ln):
words = patt.findall(ln.lower())
if len(words)<2 or words[0] not in ('and','or'):
# 输入的行不是以and, or开始, 或未给出待查单词
return None
else:
return words[0], words[1:]
cmdswitch = {
'and': indexAndQuery,
'or': indexOrQuery,
}
print "Enter Context, leave by empty line"
index = makeIndex()
print "Enter and/or keywords to query"
while True:
got = lnparser(raw_input())
if not got:
break
print cmdswitch[got[0]](index, *got[1])
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯