第15章自然言語処理:事前学習
15.3.1. Reading the Dataset15.3.1 データセットを読む
The dataset that we use here is Penn Tree Bank (PTB). This corpus is sampled from Wall Street Journal articles, split into training, validation, and test sets. In the original format, each line of the text file represents a sentence of words that are separated by spaces. Here we treat each word as a token.
英語のヒント
ここで使うデータセットはPenn Tree Bank(PTB)である。このコーパスはウォール・ストリート・ジャーナルの記事から抜き出したもので、訓練集合、検証集合、テスト集合に分かれている。元の形式では、テキストファイルの各行が、空白で区切られた単語からなる一つの文を表す。ここでは、各単語をトークンとして扱う。
#@save
d2l.DATA_HUB['ptb'] = (d2l.DATA_URL + 'ptb.zip',
'319d85e578af0cdc590547f26231e4e31cdf1e42')
#@save
def read_ptb():
"""Load the PTB dataset into a list of text lines."""
data_dir = d2l.download_extract('ptb')
# Read the training set
with open(os.path.join(data_dir, 'ptb.train.txt')) as f:
raw_text = f.read()
return [line.split() for line in raw_text.split('\n')]
sentences = read_ptb()
f'# sentences: {len(sentences)}'Downloading ../data/ptb.zip from http://d2l-data.s3-accelerate.amazonaws.com/ptb.zip...'# sentences: 42069'After reading the training set, we build a vocabulary for the corpus, where any word that appears less than 10 times is replaced by the “<unk>” token. Note that the original dataset also contains “<unk>” tokens that represent rare (unknown) words.
英語のヒント
訓練集合を読んだ後、コーパスの語彙を作る。そこでは、10回未満しか現れない単語はすべて「<unk>」トークンに置き換えられる。元のデータセットにも、まれな(未知の)単語を表す「<unk>」トークンが含まれていることに注意しよう。
vocab = d2l.Vocab(sentences, min_freq=10)
f'vocab size: {len(vocab)}''vocab size: 6719'