赞
踩
在数据集处理为txt格式的情况下,需要进一步为json格式,先展示一下处理后的json个数据:
每一行是一个json格式数据,包含两个字段,word和tag,word就是每个字的ascll码表示,如果是数字就表示为NUM,tag是两次之间是空格还是标点,是标点要指明是什么标点。
下面介绍一下处理过程:
1、导入需要的模板
import os
from tqdm import tqdm
from collections import Counter
import numpy as np
import codecs
import re
from data.common import SPACE, UNK, PAD, NUM, END, write_json
其中common是自定义文件,用到的代码如下
PAD = "<PAD>"
UNK = "<UNK>"
NUM = "<NUM>"
END = "</S>"
SPACE = "_SPACE"
def write_json(filename, dataset):
print('write to'+filename)
with codecs.open(filename, mode="w", encoding="utf-8") as f:
ujson.dump(dataset, f)
2、定义标点列表
其中只包括逗号句号问号,不在这几个中的也要单独定义列表
# Comma, period & question mark only:
PUNCTUATION_VOCABULARY = [SPACE, ",COMMA", ".PERIOD", "?QUESTIONMARK"]
#PUNCTUATION_VOCABULARY = [SPACE, ",COMMA"]
PUNCTUATION_MAPPING = {
"!EXCLAMATIONMARK": ".PERIOD", ":COLON": ",COMMA", ";SEMICOLON": ".PERIOD", "-DASH": ",COMMA"}
EOS_TOKENS = {
".PERIOD", "?QUESTIONMARK", "!EXCLAMATIONMARK"}
# punctuations that are not included in vocabulary nor mapping, must be added to CRAP_TOKENS
CRAP_TOKENS = {
"<doc>", "<doc.>"}
3、判断字符是否为数字
这里调用了正则表达式进行判断,返回布尔类型
def is_number(word):
numbers = re
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。