-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdata_zh.py
67 lines (51 loc) · 1.9 KB
/
data_zh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import torch
class Dictionary(object):
"""
词汇表,将文本中的词转换为数字id表示。
"""
def __init__(self):
self.word2idx = {}
self.idx2word = []
def add_word(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
def __len__(self):
return len(self.idx2word)
class Corpus(object):
"""
文本预处理,获取词汇表,并将字符串文本转换为数字序列。
"""
def __init__(self, path):
self.dictionary = Dictionary()
self.train = self.tokenize(path)
def tokenize(self, path):
"""文本符号化,转换为数字id表示。"""
assert os.path.exists(path)
# 将新词加入到词汇表中
with open(path, 'r', encoding='utf-8') as f:
tokens = 0
for line in f:
if len(line.strip()) == 0: # 过滤空的行
continue
words = list(line.strip()) + ['<eos>'] # 此处与原文档不同,基于字符级
tokens += len(words)
for word in words:
self.dictionary.add_word(word)
# 将字符转换为数字
with open(path, 'r', encoding='utf-8') as f:
ids = torch.LongTensor(tokens)
token = 0
for line in f:
if len(line.strip()) == 0: # 过滤空的行
continue
words = list(line.strip()) + ['<eos>'] # 此处与原文档不同,基于字符级
for word in words:
ids[token] = self.dictionary.word2idx[word]
token += 1
return ids
def __repr__(self):
return "Corpus length: %d, Vocabulary size: %d" % (self.train.size(0), len(self.dictionary))