使用torchtext将文本转为ID表示
此文详细介绍了如何使用torchtext加载文本数据并且转为由ID表示的数据。
概览图:
torchtext 加载文本数据
def get_dataset(path, text_field, label_field):
train, dev = data.TabularDataset.splits(
# 支持 json, dict, csv, tsv 文件格式
path=path, format='csv', skip_header=True,
train='train.csv', validation='test.csv',
fields=[
('index', None),
('label', label_field),
('text', text_field)
]
)
return train, dev
使用torchtext 进行数据预处理
def word_cut(text):
# 此处传入的是句子
# 去除异常符号,数字等操作可以在分词之前操作
return [word for wor