citation-einstein~20171217-152130.py 693 B

123456789101112131415161718192021222324
  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))
  11. print(words_in)
  12. words_in.reverse()
  13. quote_found = False
  14. for word in words_in:
  15. for quote in quotes:
  16. if word in quote:
  17. text_out += "À propos de %s, Einstein a dit un jour : \"%s\"." % (word, quote)
  18. quote_found = True
  19. break
  20. if quote_found:
  21. break
  22. print(text_out);