Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,7 @@ def get_wordnet_pos(word):
|
|
| 69 |
return wordnet.NOUN
|
| 70 |
|
| 71 |
def lemmatize_word(word):
|
| 72 |
-
"""Pure lemmatization
|
| 73 |
# Try spaCy first if available
|
| 74 |
if SPACY_AVAILABLE:
|
| 75 |
try:
|
|
@@ -79,18 +79,39 @@ def lemmatize_word(word):
|
|
| 79 |
except:
|
| 80 |
pass
|
| 81 |
|
| 82 |
-
# Try WordNet with POS
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
except:
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def process_text(text, selected_model):
|
| 96 |
"""Main function to process text with tokenization, stemming, and lemmatization"""
|
|
@@ -164,6 +185,7 @@ def process_text(text, selected_model):
|
|
| 164 |
lemmatized_words = [lemmatize_word(word) for word in words]
|
| 165 |
|
| 166 |
stemming_info = f"**Stemming Results (Porter Stemmer):**\n"
|
|
|
|
| 167 |
stemming_info += f"- Original words: {words}\n"
|
| 168 |
stemming_info += f"- Stemmed words: {stemmed_words}\n\n"
|
| 169 |
|
|
@@ -368,5 +390,4 @@ def create_interface():
|
|
| 368 |
|
| 369 |
if __name__ == "__main__":
|
| 370 |
demo = create_interface()
|
| 371 |
-
demo.launch(share=True)
|
| 372 |
-
|
|
|
|
| 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:
|
|
|
|
| 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 |
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 |
|
|
|
|
| 390 |
|
| 391 |
if __name__ == "__main__":
|
| 392 |
demo = create_interface()
|
| 393 |
+
demo.launch(share=True)
|
|
|