您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
此存儲庫已封存,您能瀏覽檔案及複製此存儲庫,但不能推送、建立問題及拉取請求。
 
 
 
 
 
 

72 行
1.5 KiB

  1. def clean_fragment(fragment):
  2. result = ""
  3. for c in fragment:
  4. if c.isalpha() or c in ["-", "'"]:
  5. result += c
  6. return result
  7. def split_words(text):
  8. fragments = split_fragments(text)
  9. res = list()
  10. for fragment in fragments:
  11. fragment = fragment.lower()
  12. fragment = clean_fragment(fragment)
  13. if fragment:
  14. res.append(fragment)
  15. return res
  16. def split_fragments(text):
  17. res = list()
  18. for fragment in text.split():
  19. if "’" in fragment:
  20. try:
  21. (before, after) = fragment.split("’")
  22. except:
  23. breakpoint()
  24. res.append(before)
  25. res.append(after)
  26. else:
  27. res.append(fragment)
  28. return res
  29. def get_frequencies(words):
  30. res = dict()
  31. for word in words:
  32. if word in res:
  33. res[word] += 1
  34. else:
  35. res[word] = 1
  36. return res
  37. def get_scores(frequencies):
  38. res = list()
  39. for word, count in frequencies.items():
  40. res.append((count, word))
  41. res.sort(reverse=True)
  42. return res
  43. def print_scores(scores):
  44. for count, word in scores:
  45. print(count, word)
  46. def main():
  47. filename = "../ruffin.txt"
  48. file = open(filename)
  49. contents = file.read()
  50. file.close()
  51. words = split_words(contents)
  52. frequencies = get_frequencies(words)
  53. scores = get_scores(frequencies)
  54. top_words = scores[:20]
  55. print_scores(top_words)
  56. main()