citation-einstein~20171217-151818.py 666 B

12345678910111213141516171819202122
  1. from random import choice
  2. from nltk.tokenize import sent_tokenize, word_tokenize
  3. with open("dixit.txt") as article:
  4. quotes = sent_tokenize(article.read())
  5. text_in = ""
  6. while text_in != "stop":
  7. text_in = input("> ")
  8. text_out = "> "
  9. words_in = word_tokenize(text_in)
  10. words_in.sort(key = lambda s: len(s)).reverse()
  11. quote_found = False
  12. for word in words_in:
  13. for quote in quotes:
  14. if word in quote:
  15. text_out += "À propos de %s, Einstein a dit un jour : \"%s\"." % (word, quote)
  16. quote_found = True
  17. break
  18. if quote_found:
  19. break
  20. print(choice(quotes));