Spaces:
Sleeping
Sleeping
Download app.py from alantv/nltk_practice: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/alantv/nltk_practice/resolve/main/app.py
- Command line
-
hf download hf://spaces/alantv/nltk_practice/app.py
-
curl -L -o app.py https://huggingface.co/spaces/alantv/nltk_practice/resolve/main/app.py
2.2 kB
| import nltk | |
| import string | |
| import unicodedata | |
| from nltk.corpus import stopwords | |
| from nltk.stem import WordNetLemmatizer | |
| import ssl | |
| try: | |
| _create_unverified_https_context = ssl._create_unverified_context | |
| except AttributeError: | |
| pass | |
| else: | |
| ssl._create_default_https_context = _create_unverified_https_context | |
| nltk.download('punkt') | |
| nltk.download('punkt_tab') | |
| nltk.download('stopwords') | |
| nltk.download('wordnet') | |
| def remove_non_ascii(words): | |
| return [unicodedata.normalize('NFKD', w).encode('ascii', 'ignore').decode('utf-8') for w in words] | |
| def to_lowercase(words): | |
| return [w.lower() for w in words] | |
| def remove_punctuation(words): | |
| return [w for w in words if w not in string.punctuation] | |
| def remove_stopwords(words): | |
| stop_words = set(stopwords.words('english')) | |
| return [w for w in words if w not in stop_words] | |
| def lemmatize_list(words): | |
| lemmatizer = WordNetLemmatizer() | |
| return [lemmatizer.lemmatize(w) for w in words] | |
| def normalize(text): | |
| steps = {} | |
| words = nltk.word_tokenize(text) | |
| steps['Tokenized'] = words | |
| words = remove_non_ascii(words) | |
| steps['Non-ASCII Removed'] = words | |
| words = to_lowercase(words) | |
| steps['Lowercased'] = words | |
| words = remove_punctuation(words) | |
| steps['Punctuation Removed'] = words | |
| words = remove_stopwords(words) | |
| steps['Stopwords Removed'] = words | |
| words = lemmatize_list(words) | |
| steps['Lemmatized'] = words | |
| steps['Final Output'] = ' '.join(words) | |
| return steps | |
| import gradio as gr | |
| examples = [ | |
| "The quick brown fox jumps over the lazy dog.", | |
| "Natural Language Processing is fascinating!", | |
| "Gradio makes UI design easy for ML apps." | |
| ] | |
| def gradio_pipeline(text): | |
| result = normalize(text) | |
| output = "" | |
| for step, value in result.items(): | |
| output += f"**{step}:**\n{value}\n\n" | |
| return output | |
| demo = gr.Interface( | |
| fn=gradio_pipeline, | |
| inputs=gr.Textbox(lines=3, placeholder="Enter text here..."), | |
| outputs="markdown", | |
| examples=examples, | |
| title="Text Normalization Pipeline", | |
| description="Step-by-step text preprocessing using NLTK" | |
| ) | |
| import shutil | |
| shutil.rmtree(".gradio", ignore_errors=True) | |
| demo.launch() | |