NLP Code 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/27

encourage image

There's no tags or description

Looks like no tags are added yet.

28 Terms

1
New cards

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')

2
New cards

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

3
New cards

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)

4
New cards

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)

5
New cards

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()]

6
New cards

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)]

7
New cards

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)]

8
New cards

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)]

9
New cards

Tìm gốc từ (Stemming) sử dụng PorterStemmer trong NLTK

porter = nltk.PorterStemmer(); stem = porter.stem(word)

10
New cards

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)

11
New cards

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)

12
New cards

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()]

13
New cards

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)

14
New cards

Tính độ tương đồng ngữ nghĩa Path Similarity giữa 2 Synsets trong WordNet

similarity = syn1.path_similarity(syn2)

15
New cards

Gán nhãn từ loại (POS Tagging) trong NLTK

pos_tags = nltk.pos_tag(tokens)

16
New cards

Cấu trúc RegEx Grammar gom cụm danh từ (NP Chunking)

grammar = r"NP: {?

17
New cards

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)

18
New cards

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)

19
New cards

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)

20
New cards

Ý 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

21
New cards

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]+?).'

22
New cards

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)

23
New cards

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ệ)

24
New cards

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)

25
New cards

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…"}}]

26
New cards

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)

27
New cards

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

28
New cards

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)