NLP code basic

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

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

30 Terms

1
New cards

Cách tải tài nguyên NLTK thường dùng (punkt, stopwords, pos_tag, wordnet, vader_lexicon)?

import nltk; nltk.download('punkt'); nltk.download('stopwords'); nltk.download('averaged_perceptron_tagger'); nltk.download('wordnet'); nltk.download('vader_lexicon')

2
New cards

Cách gửi HTTP request và bóc tách HTML thô bằng requests và BeautifulSoup?

import requests; from bs4 import BeautifulSoup; res = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}); soup = BeautifulSoup(res.text, 'html.parser')

3
New cards

Cách lấy văn bản thuần túy (loại bỏ thẻ HTML) từ BeautifulSoup?

raw_text = soup.get_text()

4
New cards

Cách xóa các thẻ nhiễu (table, script, style, figure) trong BeautifulSoup trước khi lấy text?

for tag in soup.select('table, script, style, figure'): tag.decompose()

5
New cards

Cách tách câu (Sentence Segmentation) tiếng Anh bằng NLTK?

import nltk; sentences = nltk.sent_tokenize(raw_text)

6
New cards

Cách tách từ (Tokenization) tiếng Anh bằng NLTK?

import nltk; tokens = nltk.word_tokenize(text)

7
New cards

Cách làm sạch token: lowercase, loại bỏ stopwords tiếng Anh, dấu câu và ký tự đặc biệt?

from nltk.corpus import stopwords; import string; stop_words = set(stopwords.words('english')); cleaned = [w.lower() for w in tokens if w.lower() not in stop_words and w not in string.punctuation and w.isalnum()]

8
New cards

Cú pháp RegEx tìm từ duy nhất kết thúc bằng đuôi "-able"?

pattern_able = [w for w in vocab if re.search(r'able$', w, re.IGNORECASE)]

9
New cards

Cú pháp RegEx tìm từ chứa cụm "-ation-" ở giữa từ (có ký tự đứng trước và đứng sau)?

pattern_ation = [w for w in vocab if re.search(r'.+ation.+', w, re.IGNORECASE)]

10
New cards

Cú pháp RegEx tìm từ bắt đầu bằng chữ 'T' hoặc 't' và kết thúc bằng đuôi "-ing"?

pattern_ting = [w for w in vocab if re.search(r'\b[Tt]\w*ing\b', w)]

11
New cards

Cách dùng PorterStemmer trong NLTK để tìm gốc từ (stem)?

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

12
New cards

Công thức và code tính Độ đa dạng từ vựng (Lexical Diversity)?

lexical_diversity = len(set(cleaned_tokens)) / len(cleaned_tokens)

13
New cards

Cách tạo danh sách Bigrams và Trigrams từ tập token trong NLTK?

from nltk import bigrams, trigrams; bg = list(bigrams(tokens)); tg = list(trigrams(tokens))

14
New cards

Cách thống kê Top 10 Bigram xuất hiện nhiều nhất bằng FreqDist trong NLTK?

from nltk import FreqDist; fdist = FreqDist(bigrams_list); top10 = fdist.most_common(10)

15
New cards

Cách tra cứu tất cả Synsets của một từ trong WordNet (NLTK)?

from nltk.corpus import wordnet as wn; synsets = wn.synsets('car')

16
New cards

Cách lấy định nghĩa, từ đồng nghĩa (lemmas), hypernyms và hyponyms từ Synset trong WordNet?

syn = wn.synset('car.n.01'); def_text = syn.definition(); lemmas = [l.name() for l in syn.lemmas()]; hyper = syn.hypernyms(); hypo = syn.hyponyms()

17
New cards

Cách tính độ tương đồng ngữ nghĩa (Path Similarity) giữa 2 Synset trong WordNet?

sim = syn1.path_similarity(syn2)

18
New cards

Cách thực hiện POS Tagging cho danh sách token tiếng Anh trong NLTK?

import nltk; pos_tags = nltk.pos_tag(tokenized_sentence)

19
New cards

Cách định nghĩa luật RegEx gom cụm danh từ (NP Chunking) trong NLTK?

grammar = r"NP: {?

20
New cards

Cách chuyển cây cú pháp Chunking (Tree) sang định dạng chuỗi IOB (CoNLL format)?

import nltk; iob_str = nltk.chunk.tree2conllstr(tree)

21
New cards

Cách đánh dấu từ phủ định (mark_negation) trong câu bằng NLTK?

from nltk.sentiment.util import mark_negation; negated_tokens = mark_negation("I don't like it".split())

22
New cards

Cách tính điểm cảm xúc Polarity Scores (pos, neg, neu, compound) bằng VADER NLTK?

from nltk.sentiment.vader import SentimentIntensityAnalyzer; sid = SentimentIntensityAnalyzer(); scores = sid.polarity_scores(text)

23
New cards

Cách trích xuất đặc trưng dạng Binarized (Boolean) cho mô hình Naive Bayes?

def extract_features(doc): words = set(doc.split()); return {f'contains({w})': (w in words) for w in all_words}

24
New cards

Cách huấn luyện và dự đoán xác suất nhãn bằng NaiveBayesClassifier trong NLTK?

classifier = nltk.NaiveBayesClassifier.train(train_set); pred = classifier.classify(test_feat); prob = classifier.prob_classify(test_feat)

25
New cards

Cách trích xuất bộ ba quan hệ (Subject, Relation, Object) bằng RegEx Groups?

pattern = r'([A-Z]\w+)\s+(is located in)\s+([A-Z]\w+)'; matches = re.findall(pattern, text)

26
New cards

Cách tách câu và tách từ ghép tiếng Việt bằng underthesea?

from underthesea import sent_tokenize, word_tokenize; sents = sent_tokenize(text); tokens = word_tokenize(text)

27
New cards

Cách gán nhãn POS và phân tích cú pháp phụ thuộc tiếng Việt bằng underthesea?

from underthesea import pos_tag, dependency_parse; tags = pos_tag(text); dep_tree = dependency_parse(text)

28
New cards

Cách tách từ ghép và POS Tagging tiếng Việt bằng PyVi?

from pyvi import ViTokenizer, ViPosTagger; tokens = ViTokenizer.tokenize(text); words, tags = ViPosTagger.postag(tokens)

29
New cards

Cách chuẩn hóa và xuất dữ liệu danh sách câu sang file JSON chuẩn Label Studio?

import json; data = [{"data": {"text": s}} for s in sentences if len(s) >= 10]; json.dump(data, open('data.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=2)

30
New cards

Định nghĩa thuật toán Minimum Edit Distance (Damerau-Levenshtein)?

Số lượng phép toán tối thiểu (Chèn, Xóa, Thay thế, Đổi chỗ 2 ký tự liền kề) để biến đổi chuỗi này thành chuỗi khác.