Update app.py
Browse files
app.py
CHANGED
|
@@ -24,13 +24,10 @@ stemmer = PorterStemmer()
|
|
| 24 |
lemmatizer = WordNetLemmatizer()
|
| 25 |
stop_words = set(stopwords.words('english'))
|
| 26 |
|
| 27 |
-
# Hàm huấn luyện classifier
|
| 28 |
def train_classifier():
|
| 29 |
-
|
| 30 |
-
pos_files = movie_reviews.fileids('pos')[:50] # Giới hạn 50 file để nhanh hơn
|
| 31 |
neg_files = movie_reviews.fileids('neg')[:50]
|
| 32 |
-
|
| 33 |
-
# Tạo tập huấn luyện
|
| 34 |
pos_reviews = [({word: True for word in movie_reviews.words(fileid)}, 'positive') for fileid in pos_files]
|
| 35 |
neg_reviews = [({word: True for word in movie_reviews.words(fileid)}, 'negative') for fileid in neg_files]
|
| 36 |
train_set = pos_reviews + neg_reviews
|
|
@@ -39,55 +36,45 @@ def train_classifier():
|
|
| 39 |
|
| 40 |
classifier = train_classifier()
|
| 41 |
|
| 42 |
-
# Hàm
|
| 43 |
-
def
|
| 44 |
if not input_text:
|
| 45 |
return "Vui lòng nhập văn bản!"
|
| 46 |
-
|
| 47 |
if function == "Sentence Tokenization":
|
| 48 |
return "\n".join(sent_tokenize(input_text))
|
| 49 |
-
|
| 50 |
elif function == "Word Tokenization":
|
| 51 |
return "\n".join(word_tokenize(input_text))
|
| 52 |
-
|
| 53 |
elif function == "Part-of-Speech Tagging":
|
| 54 |
words = word_tokenize(input_text)
|
| 55 |
return "\n".join([f"{word}: {tag}" for word, tag in pos_tag(words)])
|
| 56 |
-
|
| 57 |
elif function == "Stemming":
|
| 58 |
words = word_tokenize(input_text)
|
| 59 |
return "\n".join([stemmer.stem(word) for word in words])
|
| 60 |
-
|
| 61 |
elif function == "Lemmatization":
|
| 62 |
words = word_tokenize(input_text)
|
| 63 |
return "\n".join([lemmatizer.lemmatize(word) for word in words])
|
| 64 |
-
|
| 65 |
elif function == "Remove Stop Words":
|
| 66 |
words = word_tokenize(input_text)
|
| 67 |
return "\n".join([word for word in words if word.lower() not in stop_words])
|
| 68 |
-
|
| 69 |
elif function == "Named Entity Recognition":
|
| 70 |
words = word_tokenize(input_text)
|
| 71 |
pos_tags = pos_tag(words)
|
| 72 |
entities = ne_chunk(pos_tags)
|
| 73 |
return str(entities)
|
| 74 |
-
|
| 75 |
elif function == "Text Classification":
|
| 76 |
words = word_tokenize(input_text)
|
| 77 |
result = classifier.classify({word: True for word in words})
|
| 78 |
return f"Sentiment: {result}"
|
| 79 |
-
|
| 80 |
elif function == "N-grams (Bigrams)":
|
| 81 |
words = word_tokenize(input_text)
|
| 82 |
bigrams = list(ngrams(words, 2))
|
| 83 |
return "\n".join([f"{w1} - {w2}" for w1, w2 in bigrams])
|
| 84 |
-
|
| 85 |
elif function == "Collocations":
|
| 86 |
words = word_tokenize(input_text)
|
| 87 |
finder = BigramCollocationFinder.from_words(words)
|
| 88 |
collocations = finder.nbest(nltk.collocations.BigramAssocMeasures().pmi, 5)
|
| 89 |
return "\n".join([f"{w1} - {w2}" for w1, w2 in collocations])
|
| 90 |
-
|
| 91 |
elif function == "WordNet Synsets":
|
| 92 |
words = word_tokenize(input_text)
|
| 93 |
first_word = words[0] if words else ""
|
|
@@ -95,61 +82,121 @@ def nlp_tool(input_text, function):
|
|
| 95 |
if synsets:
|
| 96 |
return f"Definition: {synsets[0].definition()}\nExamples: {synsets[0].examples()}"
|
| 97 |
return "Không tìm thấy từ trong WordNet!"
|
| 98 |
-
|
| 99 |
elif function == "Sample from Brown Corpus":
|
| 100 |
return " ".join(brown.words()[:50])
|
| 101 |
-
|
| 102 |
return "Chức năng chưa được triển khai!"
|
| 103 |
|
| 104 |
-
# Tạo giao diện Gradio
|
| 105 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 106 |
gr.Markdown(
|
| 107 |
"""
|
| 108 |
# Công cụ xử lý ngôn ngữ tự nhiên với NLTK
|
| 109 |
-
|
| 110 |
"""
|
| 111 |
)
|
| 112 |
|
| 113 |
-
with gr.
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
placeholder="Ví dụ: I love coding
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
"
|
| 128 |
-
"Remove Stop Words",
|
| 129 |
-
"
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
lines=
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
# Chạy giao diện
|
| 155 |
demo.launch()
|
|
|
|
| 24 |
lemmatizer = WordNetLemmatizer()
|
| 25 |
stop_words = set(stopwords.words('english'))
|
| 26 |
|
| 27 |
+
# Hàm huấn luyện classifier
|
| 28 |
def train_classifier():
|
| 29 |
+
pos_files = movie_reviews.fileids('pos')[:50]
|
|
|
|
| 30 |
neg_files = movie_reviews.fileids('neg')[:50]
|
|
|
|
|
|
|
| 31 |
pos_reviews = [({word: True for word in movie_reviews.words(fileid)}, 'positive') for fileid in pos_files]
|
| 32 |
neg_reviews = [({word: True for word in movie_reviews.words(fileid)}, 'negative') for fileid in neg_files]
|
| 33 |
train_set = pos_reviews + neg_reviews
|
|
|
|
| 36 |
|
| 37 |
classifier = train_classifier()
|
| 38 |
|
| 39 |
+
# Hàm xử lý cho từng chức năng
|
| 40 |
+
def process_text(input_text, function):
|
| 41 |
if not input_text:
|
| 42 |
return "Vui lòng nhập văn bản!"
|
| 43 |
+
|
| 44 |
if function == "Sentence Tokenization":
|
| 45 |
return "\n".join(sent_tokenize(input_text))
|
|
|
|
| 46 |
elif function == "Word Tokenization":
|
| 47 |
return "\n".join(word_tokenize(input_text))
|
|
|
|
| 48 |
elif function == "Part-of-Speech Tagging":
|
| 49 |
words = word_tokenize(input_text)
|
| 50 |
return "\n".join([f"{word}: {tag}" for word, tag in pos_tag(words)])
|
|
|
|
| 51 |
elif function == "Stemming":
|
| 52 |
words = word_tokenize(input_text)
|
| 53 |
return "\n".join([stemmer.stem(word) for word in words])
|
|
|
|
| 54 |
elif function == "Lemmatization":
|
| 55 |
words = word_tokenize(input_text)
|
| 56 |
return "\n".join([lemmatizer.lemmatize(word) for word in words])
|
|
|
|
| 57 |
elif function == "Remove Stop Words":
|
| 58 |
words = word_tokenize(input_text)
|
| 59 |
return "\n".join([word for word in words if word.lower() not in stop_words])
|
|
|
|
| 60 |
elif function == "Named Entity Recognition":
|
| 61 |
words = word_tokenize(input_text)
|
| 62 |
pos_tags = pos_tag(words)
|
| 63 |
entities = ne_chunk(pos_tags)
|
| 64 |
return str(entities)
|
|
|
|
| 65 |
elif function == "Text Classification":
|
| 66 |
words = word_tokenize(input_text)
|
| 67 |
result = classifier.classify({word: True for word in words})
|
| 68 |
return f"Sentiment: {result}"
|
|
|
|
| 69 |
elif function == "N-grams (Bigrams)":
|
| 70 |
words = word_tokenize(input_text)
|
| 71 |
bigrams = list(ngrams(words, 2))
|
| 72 |
return "\n".join([f"{w1} - {w2}" for w1, w2 in bigrams])
|
|
|
|
| 73 |
elif function == "Collocations":
|
| 74 |
words = word_tokenize(input_text)
|
| 75 |
finder = BigramCollocationFinder.from_words(words)
|
| 76 |
collocations = finder.nbest(nltk.collocations.BigramAssocMeasures().pmi, 5)
|
| 77 |
return "\n".join([f"{w1} - {w2}" for w1, w2 in collocations])
|
|
|
|
| 78 |
elif function == "WordNet Synsets":
|
| 79 |
words = word_tokenize(input_text)
|
| 80 |
first_word = words[0] if words else ""
|
|
|
|
| 82 |
if synsets:
|
| 83 |
return f"Definition: {synsets[0].definition()}\nExamples: {synsets[0].examples()}"
|
| 84 |
return "Không tìm thấy từ trong WordNet!"
|
|
|
|
| 85 |
elif function == "Sample from Brown Corpus":
|
| 86 |
return " ".join(brown.words()[:50])
|
|
|
|
| 87 |
return "Chức năng chưa được triển khai!"
|
| 88 |
|
| 89 |
+
# Tạo giao diện Gradio với các tab
|
| 90 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 91 |
gr.Markdown(
|
| 92 |
"""
|
| 93 |
# Công cụ xử lý ngôn ngữ tự nhiên với NLTK
|
| 94 |
+
Chọn tab và nhập văn bản để khám phá các tính năng!
|
| 95 |
"""
|
| 96 |
)
|
| 97 |
|
| 98 |
+
with gr.Tabs():
|
| 99 |
+
# Tab 1: Tokenization
|
| 100 |
+
with gr.TabItem("Tokenization"):
|
| 101 |
+
with gr.Row():
|
| 102 |
+
token_input = gr.Textbox(label="Nhập văn bản", placeholder="Ví dụ: I love coding.", lines=5)
|
| 103 |
+
token_dropdown = gr.Dropdown(
|
| 104 |
+
label="Chọn chức năng",
|
| 105 |
+
choices=["Sentence Tokenization", "Word Tokenization"],
|
| 106 |
+
value="Sentence Tokenization"
|
| 107 |
+
)
|
| 108 |
+
token_output = gr.Textbox(label="Kết quả", lines=10, interactive=False)
|
| 109 |
+
token_btn = gr.Button("Xử lý", variant="primary")
|
| 110 |
+
gr.Markdown(
|
| 111 |
+
"""
|
| 112 |
+
### Hướng dẫn:
|
| 113 |
+
- **Sentence Tokenization:** Tách văn bản thành các câu riêng biệt.
|
| 114 |
+
- **Word Tokenization:** Tách văn bản thành các từ riêng lẻ.
|
| 115 |
+
"""
|
| 116 |
)
|
| 117 |
+
token_btn.click(fn=process_text, inputs=[token_input, token_dropdown], outputs=token_output)
|
| 118 |
+
|
| 119 |
+
# Tab 2: Morphology
|
| 120 |
+
with gr.TabItem("Morphology"):
|
| 121 |
+
with gr.Row():
|
| 122 |
+
morph_input = gr.Textbox(label="Nhập văn bản", placeholder="Ví dụ: Running is fun.", lines=5)
|
| 123 |
+
morph_dropdown = gr.Dropdown(
|
| 124 |
+
label="Chọn chức năng",
|
| 125 |
+
choices=["Stemming", "Lemmatization", "Remove Stop Words"],
|
| 126 |
+
value="Stemming"
|
| 127 |
+
)
|
| 128 |
+
morph_output = gr.Textbox(label="Kết quả", lines=10, interactive=False)
|
| 129 |
+
morph_btn = gr.Button("Xử lý", variant="primary")
|
| 130 |
+
gr.Markdown(
|
| 131 |
+
"""
|
| 132 |
+
### Hướng dẫn:
|
| 133 |
+
- **Stemming:** Rút gọn từ về dạng gốc thô (VD: 'running' → 'run').
|
| 134 |
+
- **Lemmatization:** Rút gọn từ về dạng gốc có nghĩa (VD: 'better' → 'good').
|
| 135 |
+
- **Remove Stop Words:** Loại bỏ từ dừng như 'the', 'is' (chỉ hỗ trợ tiếng Anh).
|
| 136 |
+
"""
|
| 137 |
)
|
| 138 |
+
morph_btn.click(fn=process_text, inputs=[morph_input, morph_dropdown], outputs=morph_output)
|
| 139 |
+
|
| 140 |
+
# Tab 3: Syntax & Semantics
|
| 141 |
+
with gr.TabItem("Syntax & Semantics"):
|
| 142 |
+
with gr.Row():
|
| 143 |
+
syntax_input = gr.Textbox(label="Nhập văn bản", placeholder="Ví dụ: Apple is in California.", lines=5)
|
| 144 |
+
syntax_dropdown = gr.Dropdown(
|
| 145 |
+
label="Chọn chức năng",
|
| 146 |
+
choices=["Part-of-Speech Tagging", "Named Entity Recognition", "WordNet Synsets"],
|
| 147 |
+
value="Part-of-Speech Tagging"
|
| 148 |
+
)
|
| 149 |
+
syntax_output = gr.Textbox(label="Kết quả", lines=10, interactive=False)
|
| 150 |
+
syntax_btn = gr.Button("Xử lý", variant="primary")
|
| 151 |
+
gr.Markdown(
|
| 152 |
+
"""
|
| 153 |
+
### Hướng dẫn:
|
| 154 |
+
- **Part-of-Speech Tagging:** Gắn nhãn từ loại (VD: danh từ, động từ).
|
| 155 |
+
- **Named Entity Recognition:** Nhận diện thực thể (VD: tên người, địa điểm).
|
| 156 |
+
- **WordNet Synsets:** Tra cứu định nghĩa và ví dụ từ WordNet (cho từ đầu tiên).
|
| 157 |
+
"""
|
| 158 |
)
|
| 159 |
+
syntax_btn.click(fn=process_text, inputs=[syntax_input, syntax_dropdown], outputs=syntax_output)
|
| 160 |
+
|
| 161 |
+
# Tab 4: Text Analysis
|
| 162 |
+
with gr.TabItem("Text Analysis"):
|
| 163 |
+
with gr.Row():
|
| 164 |
+
analysis_input = gr.Textbox(label="Nhập văn bản", placeholder="Ví dụ: I love this movie!", lines=5)
|
| 165 |
+
analysis_dropdown = gr.Dropdown(
|
| 166 |
+
label="Chọn chức năng",
|
| 167 |
+
choices=["Text Classification", "N-grams (Bigrams)", "Collocations"],
|
| 168 |
+
value="Text Classification"
|
| 169 |
+
)
|
| 170 |
+
analysis_output = gr.Textbox(label="Kết quả", lines=10, interactive=False)
|
| 171 |
+
analysis_btn = gr.Button("Xử lý", variant="primary")
|
| 172 |
+
gr.Markdown(
|
| 173 |
+
"""
|
| 174 |
+
### Hướng dẫn:
|
| 175 |
+
- **Text Classification:** Phân loại cảm xúc (tích cực/tiêu cực).
|
| 176 |
+
- **N-grams (Bigrams):** Tạo các cặp từ liên tiếp.
|
| 177 |
+
- **Collocations:** Tìm các cặp từ thường xuất hiện cùng nhau.
|
| 178 |
+
"""
|
| 179 |
+
)
|
| 180 |
+
analysis_btn.click(fn=process_text, inputs=[analysis_input, analysis_dropdown], outputs=analysis_output)
|
| 181 |
+
|
| 182 |
+
# Tab 5: Corpus
|
| 183 |
+
with gr.TabItem("Corpus"):
|
| 184 |
+
with gr.Row():
|
| 185 |
+
corpus_input = gr.Textbox(label="Nhập văn bản (không cần thiết)", placeholder="Để trống cũng được", lines=5)
|
| 186 |
+
corpus_dropdown = gr.Dropdown(
|
| 187 |
+
label="Chọn chức năng",
|
| 188 |
+
choices=["Sample from Brown Corpus"],
|
| 189 |
+
value="Sample from Brown Corpus"
|
| 190 |
+
)
|
| 191 |
+
corpus_output = gr.Textbox(label="Kết quả", lines=10, interactive=False)
|
| 192 |
+
corpus_btn = gr.Button("Xử lý", variant="primary")
|
| 193 |
+
gr.Markdown(
|
| 194 |
+
"""
|
| 195 |
+
### Hướng dẫn:
|
| 196 |
+
- **Sample from Brown Corpus:** Lấy mẫu 50 từ từ Brown Corpus bất kể văn bản nhập vào.
|
| 197 |
+
"""
|
| 198 |
+
)
|
| 199 |
+
corpus_btn.click(fn=process_text, inputs=[corpus_input, corpus_dropdown], outputs=corpus_output)
|
| 200 |
|
| 201 |
# Chạy giao diện
|
| 202 |
demo.launch()
|