请在注释处填入代码完成对训练集和测试集的结巴分词from paddlenlp.datasets import load_dataset def read(data_path): data_set = [] with open(data_path, 'r', encoding='utf-8') as f: for line in f: l = line.strip('\n').split('\t') if len(l) != 2: print (len(l), line) words, labels = line.strip('\n').split('\t') data_set.append((words,labels)) return data_set train_ds = read(data_path='train.txt') dev_ds = read(data_path='dev.txt') test_ds = read(data_path='test.txt') for i in range(5): print("sentence %d" % (i), train_ds[i][0]) print("sentence %d" % (i), train_ds[i][1]) print(len(train_ds),len(dev_ds)) import jieba def data_preprocess(corpus): data_set = [] ####填结巴分词代码 for text in corpus: seg_list = jieba.cut(text) data_set.append(" ".join(seg_list)) return data_set train_corpus = data_preprocess(train_ds) test_corpus = data_preprocess(test_ds) print(train_corpus[:2]) print(test_corpus[:2])
时间: 2024-04-06 18:35:09 浏览: 68
from paddlenlp.datasets import load_dataset
def read(data_path):
data_set = []
with open(data_path, 'r', encoding='utf-8') as f:
for line in f:
l = line.strip('\n').split('\t')
if len(l) != 2:
print (len(l), line)
words, labels = line.strip('\n').split('\t')
data_set.append((words,labels))
return data_set
train_ds = read(data_path='train.txt')
dev_ds = read(data_path='dev.txt')
test_ds = read(data_path='test.txt')
for i in range(5):
print("sentence %d" % (i), train_ds[i][0])
print("sentence %d" % (i), train_ds[i][1])
print(len(train_ds),len(dev_ds))
import jieba
def data_preprocess(corpus):
data_set = []
for text in corpus:
seg_list = jieba.cut(text[0])
data_set.append((" ".join(seg_list), text[1]))
return data_set
train_corpus = data_preprocess(train_ds)
test_corpus = data_preprocess(test_ds)
print(train_corpus[:2])
print(test_corpus[:2])