我有如下的倒排索引代码。但是我对它不太满意,想知道如何使它更紧凑和pythonic
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")