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