Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,8 +69,9 @@ def get_wordnet_pos(word):
|
|
| 69 |
return wordnet.NOUN
|
| 70 |
|
| 71 |
def lemmatize_word(word):
|
| 72 |
-
"""
|
| 73 |
-
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 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 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"-
|
|
|
|
| 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 |
|