我有一个invertedIndex的代码如下。不过,我对它不太满意,还想知道如何使它更紧凑、更像Python
class invertedIndex(object):
def __init__(self,docs):
self.docs,self.termList,self.docLists=docs,[],[]
for index,doc in enumerate(docs):
for term in doc.split(" "):
if term in self.termList:
i=self.termList.index(term)
if index not in self.docLists[i]:
self.docLists[i].append(index)
else:
self.termList.append(term)
self.docLists.append([index])
def search(self,term):
try:
i=self.termList.index(term)
return self.docLists[i]
except:
return "No results"
docs=["new home sales top forecasts june june june",
"home sales rise in july june",
"increase in home sales in july",
"july new home sales rise"]
i=invertedIndex(docs)
print invertedIndex.search("sales")
将doc指示符存储在Pythonset中,并使用dict引用每个术语的“doc set”。在
set
的工作方式有点像列表,但无序,不能包含重复的条目。在defaultdict
基本上是一个dict
,当没有数据可用时,它有一个默认类型(在本例中是一个空集)。在这个解决方案与@Peter Gibson的几乎相同,在这个版本中,索引是数据,不涉及委派的docSets对象。这使得代码稍微简短和清晰。在
代码还保留了文档的原始顺序。。。这是一个bug,我更喜欢Peter的
set()
实现。在还请注意,引用不存在的项,如
ix['garbage']
,会隐式地修改索引。如果唯一的API是search
,这是可以的,但是这个例子值得注意。在来源
输出
^{pr2}$相关问题 更多 >
编程相关推荐