NLP常用工具包

✨做一次按NLP项目常见工具的使用拆解

1. tokenizer

python 复制代码
from torchtext.data.utils import get_tokenizer

tokenizer = get_tokenizer('basic_english')
text_sample = "We're going on an adventure! The weather is really nice today."
tokens = tokenizer(text_sample)
print(tokens)

'we', "'", 're', 'going', 'on', 'an', 'adventure', '!', 'the', 'weather', 'is', 'really', 'nice', 'today', '.'

2. vocab

python 复制代码
from torchtext.vocab import build_vocab_from_iterator
from torchtext.data.utils import get_tokenizer

# 创建分词器
tokenizer = get_tokenizer('basic_english')

# 测试数据
test_sentences = [
    "The quick brown fox jumps over the lazy dog.",
    "Hello world! This is a test for building vocabulary.",
]

vocab = build_vocab_from_iterator(
    (tokenizer(sentence) for sentence in test_sentences),
    specials=['<unk>', '<pad>'],
    min_freq=1  # 设置最小频率为1
)

vocab.set_default_index(vocab['<unk>'])

print("词表大小:", len(vocab))
print("'fox'的索引:", vocab['fox'])

词表大小: 21

'fox'的索引: 10

3. Dataloader(示例1)

python 复制代码
import torch
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import Dataset, DataLoader
from torchtext.vocab import build_vocab_from_iterator
from torchtext.data.utils import get_tokenizer

# 1. 创建分词器
tokenizer = get_tokenizer('basic_english')

# 2. 测试数据
train_sentences = [
    "The quick brown fox jumps over the lazy dog.",
    "Hello world! This is a test for building vocabulary.",
    # 你可以在这里添加更多训练句子
]
test_sentences = [
    "The quick brown fox jumps over the lazy dog.",
    "Hello world! This is a test for building vocabulary.",
]

# 3. 构建词表
vocab = build_vocab_from_iterator(
    (tokenizer(sentence) for sentence in train_sentences),
    specials=['<unk>', '<pad>'],
    min_freq=1
)
vocab.set_default_index(vocab['<unk>'])

print("词表大小:", len(vocab))
print("'fox'的索引:", vocab['fox'])

# 4. 自定义 Dataset
class TextDataset(Dataset):
    def __init__(self, sentences, vocab, tokenizer):
        self.sentences = sentences
        self.vocab = vocab
        self.tokenizer = tokenizer

    def __len__(self):
        return len(self.sentences)

    def __getitem__(self, idx):
        tokens = self.tokenizer(self.sentences[idx])
        indices = [self.vocab[token] for token in tokens]
        return torch.tensor(indices, dtype=torch.long)

# 5. 创建 Dataset 实例
train_dataset = TextDataset(train_sentences, vocab, tokenizer)
test_dataset  = TextDataset(test_sentences, vocab, tokenizer)

# 6. DataLoader 与 Padding Collate 函数

def collate_fn(batch):
    # batch 是一个 list of tensors
    return pad_sequence(batch, batch_first=True, padding_value=vocab['<pad>'])

train_loader = DataLoader(train_dataset, batch_size=2, shuffle=True, collate_fn=collate_fn)
test_loader  = DataLoader(test_dataset, batch_size=2, shuffle=False, collate_fn=collate_fn)

# 7. 测试 DataLoader 输出
print("\n=== Train Batch Indices ===")
for batch in train_loader:
    print(batch)
    break

print("\n=== Test Batch Indices ===")
for batch in test_loader:
    print(batch)
    break

=== Train Batch Indices ===
tensor(\[11, 20, 4, 18, 12, 5, 17, 9, 7, 19, 2,
3, 16, 6, 10, 13, 15, 3, 14, 8, 2, 1])

=== Test Batch Indices ===
tensor(\[ 3, 16, 6, 10, 13, 15, 3, 14, 8, 2, 1,
11, 20, 4, 18, 12, 5, 17, 9, 7, 19, 2])

4. Dataloader(示例2)

python 复制代码
import torch
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import Dataset, DataLoader
from torchtext.vocab import build_vocab_from_iterator
from torchtext.data.utils import get_tokenizer

# 1. 创建分词器
tokenizer = get_tokenizer('basic_english')

# 2. 带标签的训练与测试数据 (句子, 标签)
train_data = [
    ("The quick brown fox jumps over the lazy dog.", 1),  # 正面情感
    ("Hello world! This is a test for building vocabulary.", 0),  # 负面情感
    # 可添加更多 (sentence, label)
]
test_data = [
    ("The quick brown fox jumps over the lazy dog.", 1),
    ("Hello world! This is a test for building vocabulary.", 0),
]

# 3. 构建词表,只基于训练数据中的句子
vocab = build_vocab_from_iterator(
    (tokenizer(sentence) for sentence, _ in train_data),
    specials=['<unk>', '<pad>'],
    min_freq=1
)
vocab.set_default_index(vocab['<unk>'])

print("词表大小:", len(vocab))
print("'fox'的索引:", vocab['fox'])

# 4. 自定义 Dataset,返回 (indices_tensor, label_tensor)
class TextDataset(Dataset):
    def __init__(self, data, vocab, tokenizer):
        self.data = data
        self.vocab = vocab
        self.tokenizer = tokenizer

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        sentence, label = self.data[idx]
        tokens = self.tokenizer(sentence)
        indices = [self.vocab[token] for token in tokens]
        return torch.tensor(indices, dtype=torch.long), torch.tensor(label, dtype=torch.long)

# 5. Padding 与 collate_fn
def collate_fn(batch):
    sequences, labels = zip(*batch)
    padded_seqs = pad_sequence(sequences, batch_first=True, padding_value=vocab['<pad>'])
    labels_tensor = torch.stack(labels)
    return padded_seqs, labels_tensor

# 6. 创建 DataLoader
train_dataset = TextDataset(train_data, vocab, tokenizer)
test_dataset  = TextDataset(test_data, vocab, tokenizer)

train_loader = DataLoader(
    train_dataset, batch_size=2, shuffle=True, collate_fn=collate_fn
)
test_loader = DataLoader(
    test_dataset, batch_size=2, shuffle=False, collate_fn=collate_fn
)

# 7. 测试输出
print("\n=== Train Batch ===")
for seq_batch, label_batch in train_loader:
    print("Sequences:", seq_batch)
    print("Labels:   ", label_batch)
    break

print("\n=== Test Batch ===")
for seq_batch, label_batch in test_loader:
    print("Sequences:", seq_batch)
    print("Labels:   ", label_batch)
    break
相关推荐
叠层归一研究院4 分钟前
基于极限自指的叠层归一宇宙结构理论——无元外部封闭系统的内生区分模型
人工智能·经验分享·算法·agi
IT_陈寒7 分钟前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
why-geo15 分钟前
Hermes Agent 与 Python 的会产生什么样的碰撞
人工智能·python·ai编程
正经教主22 分钟前
【FDE系列】阶段2:Day 29:FastAPI 进阶 — Pydantic 模型与完整 CRUD 实战
人工智能·python·fde
时速GEO系统38 分钟前
初元 AI V1.1 版本升级,首个正式版发布一周・实现生成、部署、上线、优化全流程自动化闭环
人工智能·数据挖掘·node.js
咕泡科技42 分钟前
咕泡科技FDE系列最新产品重磅发布!
人工智能·大模型·ai落地·fde·前沿部署工程师
犀利豆1 小时前
为什么全世界的 AI 都画不好一只骑自行车的鹈鹕
人工智能·llm·aigc
天天被压力1 小时前
【跨市场数据实战 #08】可转债折价机会怎么筛:3个接口抓比价、列表和实时盘口
java·人工智能·python
baopixiaoz1 小时前
BeeQuant × BeeAgent:用AI加速策略验证
大数据·人工智能·python·区块链
咸鱼老弟1 小时前
AI Agent 的"自主性悖论"——为什么给它的自由度越大,越要配一套更硬的护栏
前端·人工智能