parser.py 822 B

123456789101112131415161718192021222324252627282930313233
  1. from spacy.tokens import Token
  2. class Parser:
  3. suggestions = None
  4. parsed_suggestions = None
  5. def __init__(self, suggestions=None):
  6. self.suggestions = suggestions
  7. def parse_word(self, word: Token):
  8. return word.text
  9. def parse_words(self, words: [Token]):
  10. return [self.parse_word(word) for word in words]
  11. def parse(self):
  12. self.parsed_suggestions = [self.parse_words(words) for words in self.suggestions]
  13. return self
  14. def present_word(self, word: str):
  15. return word
  16. def present_words(self, words: [str]):
  17. presented_words = [self.present_word(word) for word in words]
  18. return ''.join(presented_words)
  19. def present(self):
  20. self.parse()
  21. return [self.present_words(words) for words in self.parsed_suggestions]