Update sample.py
Browse files
sample.py
CHANGED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
import joblib
|
| 2 |
-
import nltk
|
| 3 |
-
from nltk.corpus import stopwords
|
| 4 |
-
from nltk.tokenize import word_tokenize
|
| 5 |
-
from nltk.collocations import BigramCollocationFinder
|
| 6 |
-
from nltk.metrics import BigramAssocMeasures
|
| 7 |
-
from nltk.probability import FreqDist
|
| 8 |
-
from nltk.tokenize import MWETokenizer
|
| 9 |
-
|
| 10 |
-
def extract_keywords(text):
|
| 11 |
-
nltk.download('stopwords')
|
| 12 |
-
|
| 13 |
-
stop_words = set(stopwords.words('english'))
|
| 14 |
-
|
| 15 |
-
words = word_tokenize(text)
|
| 16 |
-
|
| 17 |
-
# Xác định các từ ghép bằng cách sử dụng BigramCollocationFinder
|
| 18 |
-
bigram_finder = BigramCollocationFinder.from_words(words)
|
| 19 |
-
bigrams = bigram_finder.nbest(BigramAssocMeasures.chi_sq, 10) # Chỉ xác định 10 từ ghép
|
| 20 |
-
|
| 21 |
-
# Tạo tokenizer với danh sách từ ghép
|
| 22 |
-
tokenizer = MWETokenizer(bigrams)
|
| 23 |
-
|
| 24 |
-
words = tokenizer.tokenize(words)
|
| 25 |
-
|
| 26 |
-
keywords = [word.lower().replace("_", " ") for word in words if word.lower() not in stop_words]
|
| 27 |
-
|
| 28 |
-
fdist = FreqDist(keywords)
|
| 29 |
-
|
| 30 |
-
return fdist
|
| 31 |
-
|
| 32 |
-
# Tải model từ tệp keywords_model.joblib
|
| 33 |
-
keywords_model = joblib.load('keywords_model.joblib')
|
| 34 |
-
|
| 35 |
-
# Nhập văn bản từ người dùng
|
| 36 |
-
user_text = input("Nhập văn bản: ")
|
| 37 |
-
|
| 38 |
-
# Trích xuất từ khóa từ văn bản người dùng sử dụng model
|
| 39 |
-
user_keywords = extract_keywords(user_text)
|
| 40 |
-
|
| 41 |
-
# In ra màn hình những từ khóa có tần suất xuất hiện cao nhất
|
| 42 |
-
for keyword, count in user_keywords.most_common():
|
| 43 |
-
keyword = keyword.replace(",", "").replace(".", "")
|
| 44 |
-
print(keyword)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|