You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.9 KiB

4 years ago
  1. # coding: utf8
  2. from __future__ import unicode_literals
  3. from .symbols import POS, NOUN, VERB, ADJ, PUNCT
  4. from .symbols import VerbForm_inf, VerbForm_none, Number_sing, Degree_pos
  5. class Lemmatizer(object):
  6. @classmethod
  7. def load(cls, path, index=None, exc=None, rules=None, lookup=None):
  8. return cls(index, exc, rules, lookup)
  9. def __init__(self, index=None, exceptions=None, rules=None, lookup=None):
  10. self.index = index
  11. self.exc = exceptions
  12. self.rules = rules
  13. self.lookup_table = lookup if lookup is not None else {}
  14. def __call__(self, string, univ_pos, morphology=None):
  15. if not self.rules:
  16. return [self.lookup_table.get(string, string)]
  17. if univ_pos in (NOUN, 'NOUN', 'noun'):
  18. univ_pos = 'noun'
  19. elif univ_pos in (VERB, 'VERB', 'verb'):
  20. univ_pos = 'verb'
  21. elif univ_pos in (ADJ, 'ADJ', 'adj'):
  22. univ_pos = 'adj'
  23. elif univ_pos in (PUNCT, 'PUNCT', 'punct'):
  24. univ_pos = 'punct'
  25. else:
  26. return list(set([string.lower()]))
  27. # See Issue #435 for example of where this logic is requied.
  28. if self.is_base_form(univ_pos, morphology):
  29. return list(set([string.lower()]))
  30. lemmas = lemmatize(string, self.index.get(univ_pos, {}),
  31. self.exc.get(univ_pos, {}),
  32. self.rules.get(univ_pos, []))
  33. return lemmas
  34. def is_base_form(self, univ_pos, morphology=None):
  35. """
  36. Check whether we're dealing with an uninflected paradigm, so we can
  37. avoid lemmatization entirely.
  38. """
  39. morphology = {} if morphology is None else morphology
  40. others = [key for key in morphology
  41. if key not in (POS, 'Number', 'POS', 'VerbForm', 'Tense')]
  42. if univ_pos == 'noun' and morphology.get('Number') == 'sing':
  43. return True
  44. elif univ_pos == 'verb' and morphology.get('VerbForm') == 'inf':
  45. return True
  46. # This maps 'VBP' to base form -- probably just need 'IS_BASE'
  47. # morphology
  48. elif univ_pos == 'verb' and (morphology.get('VerbForm') == 'fin' and
  49. morphology.get('Tense') == 'pres' and
  50. morphology.get('Number') is None and
  51. not others):
  52. return True
  53. elif univ_pos == 'adj' and morphology.get('Degree') == 'pos':
  54. return True
  55. elif VerbForm_inf in morphology:
  56. return True
  57. elif VerbForm_none in morphology:
  58. return True
  59. elif Number_sing in morphology:
  60. return True
  61. elif Degree_pos in morphology:
  62. return True
  63. else:
  64. return False
  65. def noun(self, string, morphology=None):
  66. return self(string, 'noun', morphology)
  67. def verb(self, string, morphology=None):
  68. return self(string, 'verb', morphology)
  69. def adj(self, string, morphology=None):
  70. return self(string, 'adj', morphology)
  71. def punct(self, string, morphology=None):
  72. return self(string, 'punct', morphology)
  73. def lookup(self, string):
  74. if string in self.lookup_table:
  75. return self.lookup_table[string]
  76. return string
  77. def lemmatize(string, index, exceptions, rules):
  78. string = string.lower()
  79. forms = []
  80. forms.extend(exceptions.get(string, []))
  81. oov_forms = []
  82. if not forms:
  83. for old, new in rules:
  84. if string.endswith(old):
  85. form = string[:len(string) - len(old)] + new
  86. if not form:
  87. pass
  88. elif form in index or not form.isalpha():
  89. forms.append(form)
  90. else:
  91. oov_forms.append(form)
  92. if not forms:
  93. forms.extend(oov_forms)
  94. if not forms:
  95. forms.append(string)
  96. return list(set(forms))