1/27
Looks like no tags are added yet.
Các package NLTK cơ bản thường dùng để tải trong bài thi NLP
import nltk; nltk.download('punkt'); nltk.download('stopwords'); nltk.download('averaged_perceptron_tagger'); nltk.download('wordnet'); nltk.download('vader_lexicon')
Thu thập mã HTML thô từ URL bằng thư viện requests
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=30); html_content = response.text
Bóc tách văn bản thô từ HTML và loại bỏ thẻ nhiễu bằng BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser'); raw_text = soup.get_text() (Dùng tag.decompose() để xóa các thẻ nhiễu)
Tách câu (Sentence Segmentation) và Tách từ (Tokenization) trong NLTK
sentences = nltk.sent_tokenize(raw_text); tokens = nltk.word_tokenize(sample_text)
Lọc stopwords tiếng Anh và dấu câu trong NLTK
stop_words = set(stopwords.words('english')); punctuation = set(string.punctuation); cleaned = [w.lower() for w in tokens if w.lower() not in stop_words and w not in punctuation and w.isalnum()]
RegEx tìm các từ duy nhất kết thúc bằng đuôi '-able'
[w for w in vocab if re.search(r'able$', w, re.IGNORECASE)]
RegEx tìm các từ chứa cụm '-ation-' ở giữa từ
[w for w in vocab if re.search(r'.+ation.+', w, re.IGNORECASE)]
RegEx tìm các từ bắt đầu bằng 'T/t' và kết thúc bằng 'ing'
[w for w in vocab if re.search(r'\b[Tt]\w*ing\b', w)]
Tìm gốc từ (Stemming) sử dụng PorterStemmer trong NLTK
porter = nltk.PorterStemmer(); stem = porter.stem(word)
Công thức và Code tính Độ đa dạng từ vựng (Lexical Diversity)
Lexical Diversity = Số Type duy nhất / Tổng số Token; Code: len(set(cleaned_tokens)) / len(cleaned_tokens)
Tạo Bigrams/Trigrams và thống kê Top 10 Bigrams bằng FreqDist
bg_list = list(nltk.bigrams(tokens)); fdist = nltk.FreqDist(bg_list); top10 = fdist.most_common(10)
Tra cứu Synset đầu tiên, định nghĩa và từ đồng nghĩa (lemmas) trong WordNet
syn = wn.synsets('car')[0]; definition = syn.definition(); lemmas = [l.name() for l in syn.lemmas()]
Phân biệt Hypernyms và Hyponyms trong WordNet
Hypernyms = Từ thượng nghĩa (khái niệm tổng quát hơn); Hyponyms = Từ hạ nghĩa (khái niệm cụ thể hơn)
Tính độ tương đồng ngữ nghĩa Path Similarity giữa 2 Synsets trong WordNet
similarity = syn1.path_similarity(syn2)
Gán nhãn từ loại (POS Tagging) trong NLTK
pos_tags = nltk.pos_tag(tokens)
Cấu trúc RegEx Grammar gom cụm danh từ (NP Chunking)
grammar = r"NP: {?
Chuyển cây cú pháp Chunking sang định dạng chuỗi IOB (CoNLL format)
cp = nltk.RegexpParser(grammar); tree = cp.parse(pos_tags); iob_str = nltk.chunk.tree2conllstr(tree)
Xử lý phủ định (Negation Handling) với mark_negation trong NLTK
from nltk.sentiment.util import mark_negation; negated_tokens = mark_negation(tokens) (thêm _NOT vào sau các từ đến trước dấu câu)
Tính điểm cảm xúc Polarity Scores bằng VADER trong NLTK
sid = SentimentIntensityAnalyzer(); scores = sid.polarity_scores(text) (trả về pos, neg, neu, compound)
Ý tưởng trích xuất đặc trưng dạng Binarized (Boolean) Naive Bayes
Chỉ kiểm tra từ có xuất hiện trong văn bản hay không (True/False) thay vì đếm tần số xuất hiện
RegEx Pattern trích xuất bộ ba quan hệ (Organization, Relation, Location)
pattern = r'([A-Z][a-zA-Z-]+(?:\s+[A-Z][a-zA-Z-]+)?)\s+(is\s+(?:located in|based in|headquartered in))\s+([A-Z][a-zA-Z\s]+?).'
Tách từ ghép và Gán nhãn POS tiếng Việt bằng underthesea
word_tokenize(text, format="text"); pos_tag(text)
Phân tích cú pháp phụ thuộc (Dependency Parsing) tiếng Việt bằng underthesea
dep_tree = dependency_parse(text) (trả về danh sách tuple: Từ_phụ_thuộc, Vị_trí_Head, Quan_hệ)
Tách từ ghép và Gán nhãn POS tiếng Việt bằng PyVi
ViTokenizer.tokenize(text); words, tags = ViPosTagger.postag(tokenized_text)
Cấu trúc JSON chuẩn nạp dữ liệu vào Label Studio
[{"data": {"text": "Nội dung câu 1…"}}, {"data": {"text": "Nội dung câu 2…"}}]
Xuất file JSON chuẩn hỗ trợ tiếng Việt UTF-8 trong Python
json.dump(formatted_dataset, f, ensure_ascii=False, indent=2)
Khái niệm cốt lõi của Minimum Edit Distance (MED)
Số lượng phép toán tối thiểu (Chèn, Xóa, Thay thế) để biến đổi chuỗi này thành chuỗi khác
Công thức truy hồi quy hoạch động D(i, j) của Minimum Edit Distance
D(i, j) = min(D(i-1, j) + 1, D(i, j-1) + 1, D(i-1, j-1) + sub_cost) với sub_cost = 0 nếu X[i]==Y[j], ngược lại sub_cost = 1 (hoặc 2)