Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. def clean_word(word):
  2.     new_word = ''
  3.     for c in word:
  4.       if c.isalpha():
  5.         new_word += c
  6.     return new_word.lower()
  7.  
  8. def index_play(filename):
  9.   f = open(filename, 'r')
  10.   text = f.read()
  11.   f.close()
  12.   words = text.split()
  13.  
  14.   index = dict()
  15.  
  16.   for i in range(len(words)):
  17.       word = clean_word(words[i])
  18.       if word in index:
  19.         index[word].append(i)
  20.       else:
  21.         index[word] = [i]
  22.  
  23.   print(len(index.keys()), "unique words")
  24.   return index
  25.  
  26. macbeth = index_play('macbeth.txt')
  27. midsummer = index_play('midsummer.txt')
  28.  
  29. query = input("Enter search word: ")
  30. if query in macbeth:
  31.   print("Macbeth:", macbeth[query])
  32. if query in midsummer:
  33.   print("Midsummer Night's Dream:", macbeth[query])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement