您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. (before, after) = fragment.split("’")
  21. res.append(before)
  22. res.append(after)
  23. else:
  24. res.append(fragment)
  25. return res
  26. def get_frequencies(words):
  27. res = dict()
  28. for word in words:
  29. if word in res:
  30. res[word] += 1
  31. else:
  32. res[word] = 1
  33. return res
  34. words = ["pomme", "poire", "banane", "poire", "banane", "banane"]
  35. frequencies = get_frequencies(words)
  36. print(frequencies)