文本数字表示
统计文档中的字符,并且统计字符个数。这里是为了将文字转换为数字表示。
import numpy as np
import re
import torch
class TextConverter(object):
def __init__(self,text_path,max_vocab=5000):
"""
建立一个字符索引转换,主要还是为了生成一个词汇表
:param text_path: 文本位置
:param max_vocab: 最大的单词数量
"""
with open(text_path,'r',encoding='utf-8') as f:
text_file=f.readlines()
# print('查看部分数据', text_file[:100])
# 去掉一些特殊字符
text_file = [re.sub(r'\n', '', _) for _ in text_file]
text_file = [re.sub(r' ', '', _) for _ in text_file]
text_file = [re.sub(r'\u3000', '', _) for _ in tex