A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
41
Task/Inverted-index/Python/inverted-index-1.py
Normal file
41
Task/Inverted-index/Python/inverted-index-1.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
'''
|
||||
This implements: http://en.wikipedia.org/wiki/Inverted_index of 28/07/10
|
||||
'''
|
||||
|
||||
from pprint import pprint as pp
|
||||
from glob import glob
|
||||
try: reduce
|
||||
except: from functools import reduce
|
||||
try: raw_input
|
||||
except: raw_input = input
|
||||
|
||||
|
||||
def parsetexts(fileglob='InvertedIndex/T*.txt'):
|
||||
texts, words = {}, set()
|
||||
for txtfile in glob(fileglob):
|
||||
with open(txtfile, 'r') as f:
|
||||
txt = f.read().split()
|
||||
words |= set(txt)
|
||||
texts[txtfile.split('\\')[-1]] = txt
|
||||
return texts, words
|
||||
|
||||
def termsearch(terms): # Searches simple inverted index
|
||||
return reduce(set.intersection,
|
||||
(invindex[term] for term in terms),
|
||||
set(texts.keys()))
|
||||
|
||||
texts, words = parsetexts()
|
||||
print('\nTexts')
|
||||
pp(texts)
|
||||
print('\nWords')
|
||||
pp(sorted(words))
|
||||
|
||||
invindex = {word:set(txt
|
||||
for txt, wrds in texts.items() if word in wrds)
|
||||
for word in words}
|
||||
print('\nInverted Index')
|
||||
pp({k:sorted(v) for k,v in invindex.items()})
|
||||
|
||||
terms = ["what", "is", "it"]
|
||||
print('\nTerm Search for: ' + repr(terms))
|
||||
pp(sorted(termsearch(terms)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue