บทนำสู่การสร้าง Text Processing Pipeline

Deep Learning สำหรับข้อความด้วย PyTorch

Shubham Jain

Data Scientist

ทบทวน: การ Preprocessing

Preprocessing Pipeline

  • การ Preprocessing:
    • Tokenization
    • การตัดคำหยุด (Stopword removal)
    • Stemming
    • การตัดคำที่พบน้อย (Rare word removal)
Deep Learning สำหรับข้อความด้วย PyTorch

Text Processing Pipeline

Preprocessing Pipeline

  • การ Encoding:
    • One-hot encoding
    • Bag-of-words
    • TF-IDF
  • Embedding
Deep Learning สำหรับข้อความด้วย PyTorch

Text Processing Pipeline

Data Processing Pipeline

  • Dataset เป็นตัวเก็บข้อความที่ผ่านการประมวลผลและ Encoding แล้ว

  • DataLoader: การแบ่ง Batch, การสุ่ม และ Multiprocessing

Deep Learning สำหรับข้อความด้วย PyTorch

ทบทวน: การสร้าง Dataset และ DataLoader

# Import libraries
from torch.utils.data import Dataset, DataLoader

# Create a class class TextDataset(Dataset):
def __init__(self, text): self.text = text
def __len__(self): return len(self.text)
def __getitem__(self, idx): return self.text[idx]
Deep Learning สำหรับข้อความด้วย PyTorch

ทบทวน: การใช้ Dataset และ DataLoader ร่วมกัน

dataset = TextDataset(encoded_text)

dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
Deep Learning สำหรับข้อความด้วย PyTorch

การใช้ฟังก์ชันช่วย

def preprocess_sentences(sentences):
    processed_sentences = []
    for sentence in sentences:
        sentence = sentence.lower()
        tokens = tokenizer(sentence)
        tokens = [token for token in tokens 
                  if token not in stop_words]
        tokens = [stemmer.stem(token) 
                  for token in tokens]
        freq_dist = FreqDist(tokens)
        threshold = 2
        tokens = [token for token in tokens if 
        freq_dist[token] > threshold]
        processed_sentences.append(
                   ' '.join(tokens))
    return processed_sentences
def encode_sentences(sentences):
    vectorizer = CountVectorizer()
    X = vectorizer.fit_transform(sentences)
    encoded_sentences = X.toarray()
    return encoded_sentences, vectorizer
def extract_sentences(data):
    sentences = re.findall(r'[A-Z][^.!?]*[.!?]', 
                           data)
    return sentences
Deep Learning สำหรับข้อความด้วย PyTorch

การสร้าง Text Processing Pipeline

def text_processing_pipeline(text):

tokens = preprocess_sentences(text)
encoded_sentences, vectorizer = encode_sentences(tokens)
dataset = TextDataset(encoded_sentences)
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
return dataloader, vectorizer
Deep Learning สำหรับข้อความด้วย PyTorch

การนำ Text Processing Pipeline ไปใช้

text_data = "This is the first text data. And here is another one."

sentences = extract_sentences(text_data) dataloaders, vectorizer = [text_processing_pipeline(text) for text in sentences]
print(next(iter(dataloader))[0, :10])
[[1, 1, 1, 1, 1], [0, 0, 0, 1, 1]]
Deep Learning สำหรับข้อความด้วย PyTorch

Text Processing Pipeline: สรุปทั้งหมด!

Full Pipeline

Deep Learning สำหรับข้อความด้วย PyTorch

มาฝึกกันเถอะ!

Deep Learning สำหรับข้อความด้วย PyTorch

Preparing Video For Download...