Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def clean_word(word):
- new_word = ''
- for c in word:
- if c.isalpha():
- new_word += c
- return new_word.lower()
- def index_play(filename):
- f = open(filename, 'r')
- text = f.read()
- f.close()
- words = text.split()
- index = dict()
- for i in range(len(words)):
- word = clean_word(words[i])
- if word in index:
- index[word].append(i)
- else:
- index[word] = [i]
- print(len(index.keys()), "unique words")
- return index
- macbeth = index_play('macbeth.txt')
- midsummer = index_play('midsummer.txt')
- query = input("Enter search word: ")
- if query in macbeth:
- print("Macbeth:", macbeth[query])
- if query in midsummer:
- print("Midsummer Night's Dream:", macbeth[query])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement