7367network

Who Said It Past 3

Nov 21st, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import math
  2. # -----
  3. # This function takes a word and a dictionary of
  4. # word counts, and it generates a score that
  5. # approximates the relevance of the word
  6. # in the document from which the word counts
  7. # were generated. The higher the score, the more
  8. # relevant the word.
  9. #
  10. # In many cases, the score returned will be
  11. # negative. Note that the "higher" of two
  12. # negative scores is the one that is less
  13. # negative, or the one that is closer to zero.
  14. def get_score(word, counts):
  15. denominator = float(1 + counts["_total"])
  16. if word in counts:
  17. return math.log((1 + counts[word]) / denominator)
  18. else:
  19. return math.log(1 / denominator)
  20.  
  21. # normalize
  22. # CODE FROM PART 2
  23. # This function takes a word and returns the same word
  24. # with:
  25. # - All non-letters removed
  26. # - All letters converted to lowercase
  27. def normalize(word):
  28. return "".join(letter for letter in word if letter.isalpha()).lower()
  29.  
  30. # get_counts
  31. # -----
  32. # This function takes a filename and generates a dictionary
  33. # whose keys are the unique words in the file and whose
  34. # values are the counts for those words.
  35. def get_counts(filename):
  36. word_count = 0
  37. result_dict = {}
  38. file = open(filename)
  39. for line in file:
  40. for word in line.strip().split():
  41. if word == " ":
  42. continue
  43. else:
  44. word = normalize(word)
  45. if word in result_dict:
  46. result_dict["_total"] = word_count
  47. else:
  48. result_dict[word] = 1
  49. return result_dict
  50. print("Word count: " + len(result_dict))
  51. print("Total word count: " + word_count)
  52. # Get the counts for the two shortened versions
  53. # of the texts
  54. shakespeare_counts = get_counts(open("hamlet.txt", "r"))
  55. austen_counts = get_counts(open("pride_and_prejudice.txt", "r"))
  56.  
  57. #Text to predict
  58. TextInput = str(input("Enter the text to be predicted: "))
  59.  
  60. #Get the counts for the text input by the user
  61. text_counts = get_counts(TextInput)
  62.  
  63. #Split the text sample into words
  64. SplitInput = TextInput.split()
  65. for word in SplitInput:
  66. if len(word) >= 1:
  67. normalize(word)
  68. else:
  69. continue
Advertisement
Add Comment
Please, Sign In to add comment