YanaGabelev commited on
Commit
1e2f6c5
·
verified ·
1 Parent(s): 4355c29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -34
app.py CHANGED
@@ -69,8 +69,9 @@ def get_wordnet_pos(word):
69
  return wordnet.NOUN
70
 
71
  def lemmatize_word(word):
72
- """Pure lemmatization with debug info"""
73
- # Try spaCy first if available
 
74
  if SPACY_AVAILABLE:
75
  try:
76
  doc = nlp(word)
@@ -79,39 +80,47 @@ def lemmatize_word(word):
79
  except:
80
  pass
81
 
82
- # Try WordNet with POS - add debug
 
 
 
83
  try:
84
- pos_tags = nltk.pos_tag([word])
85
- pos_tag = pos_tags[0][1] if pos_tags else 'NN'
86
-
87
- # Map to WordNet POS with more detailed mapping
88
- wordnet_pos = wordnet.NOUN # default
89
- if pos_tag.startswith('V'): # VB, VBD, VBG, VBN, VBP, VBZ
90
- wordnet_pos = wordnet.VERB
91
- elif pos_tag.startswith('J'): # JJ, JJR, JJS
92
- wordnet_pos = wordnet.ADJ
93
- elif pos_tag.startswith('R'): # RB, RBR, RBS
94
- wordnet_pos = wordnet.ADV
95
- elif pos_tag.startswith('N'): # NN, NNS, NNP, NNPS
96
- wordnet_pos = wordnet.NOUN
97
-
98
- lemma = lemmatizer.lemmatize(word, wordnet_pos)
99
-
100
- # If no change, try all POS types
101
- if lemma == word:
102
- for pos in [wordnet.VERB, wordnet.ADJ, wordnet.ADV, wordnet.NOUN]:
103
- candidate = lemmatizer.lemmatize(word, pos)
104
- if candidate != word:
105
- return candidate
106
-
107
- return lemma
108
-
109
  except:
110
- # Basic WordNet fallback
111
- try:
112
- return lemmatizer.lemmatize(word)
113
- except:
114
- return word
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  def process_text(text, selected_model):
117
  """Main function to process text with tokenization, stemming, and lemmatization"""
@@ -185,7 +194,8 @@ def process_text(text, selected_model):
185
  lemmatized_words = [lemmatize_word(word) for word in words]
186
 
187
  stemming_info = f"**Stemming Results (Porter Stemmer):**\n"
188
- stemming_info += f"- Test: running -> {stemmer.stem('running')}, quickly -> {stemmer.stem('quickly')}\n"
 
189
  stemming_info += f"- Original words: {words}\n"
190
  stemming_info += f"- Stemmed words: {stemmed_words}\n\n"
191
 
 
69
  return wordnet.NOUN
70
 
71
  def lemmatize_word(word):
72
+ """Simple but effective lemmatization"""
73
+
74
+ # Try spaCy first if available (most reliable)
75
  if SPACY_AVAILABLE:
76
  try:
77
  doc = nlp(word)
 
80
  except:
81
  pass
82
 
83
+ # Try WordNet with all possible POS tags
84
+ candidates = []
85
+
86
+ # Try as verb (most important for ran, running)
87
  try:
88
+ verb_lemma = lemmatizer.lemmatize(word, wordnet.VERB)
89
+ if verb_lemma != word:
90
+ candidates.append(verb_lemma)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  except:
92
+ pass
93
+
94
+ # Try as adjective (important for better)
95
+ try:
96
+ adj_lemma = lemmatizer.lemmatize(word, wordnet.ADJ)
97
+ if adj_lemma != word:
98
+ candidates.append(adj_lemma)
99
+ except:
100
+ pass
101
+
102
+ # Try as noun
103
+ try:
104
+ noun_lemma = lemmatizer.lemmatize(word, wordnet.NOUN)
105
+ if noun_lemma != word:
106
+ candidates.append(noun_lemma)
107
+ except:
108
+ pass
109
+
110
+ # Try as adverb
111
+ try:
112
+ adv_lemma = lemmatizer.lemmatize(word, wordnet.ADV)
113
+ if adv_lemma != word:
114
+ candidates.append(adv_lemma)
115
+ except:
116
+ pass
117
+
118
+ # Return best candidate (shortest one)
119
+ if candidates:
120
+ return min(candidates, key=len)
121
+
122
+ # If nothing worked, return original
123
+ return word
124
 
125
  def process_text(text, selected_model):
126
  """Main function to process text with tokenization, stemming, and lemmatization"""
 
194
  lemmatized_words = [lemmatize_word(word) for word in words]
195
 
196
  stemming_info = f"**Stemming Results (Porter Stemmer):**\n"
197
+ stemming_info += f"- Direct test: 'running' -> '{stemmer.stem('running')}' (should be 'runn')\n"
198
+ stemming_info += f"- Direct test: 'quickly' -> '{stemmer.stem('quickly')}' (should be 'quickli')\n"
199
  stemming_info += f"- Original words: {words}\n"
200
  stemming_info += f"- Stemmed words: {stemmed_words}\n\n"
201