第10章現代のリカレントニューラルネットワーク
10.5.1. Downloading and Preprocessing the Dataset10.5.1 データセットをダウンロードして前処理する
To begin, we download an English–French dataset that consists of bilingual sentence pairs from the Tatoeba Project. Each line in the dataset is a tab-delimited pair consisting of an English text sequence (the source) and the translated French text sequence (the target). Note that each text sequence can be just one sentence, or a paragraph of multiple sentences.
英語のヒント
まず、Tatoebaプロジェクトの対訳の文の組からなる英語・フランス語のデータセットをダウンロードする。データセットの各行は、英語のテキスト系列(原文)と、翻訳されたフランス語のテキスト系列(目標)からなる、タブ区切りの組である。各テキスト系列は一つの文のこともあれば、複数の文からなる段落のこともあることに注意してほしい。
class MTFraEng(d2l.DataModule): #@save
"""The English-French dataset."""
def _download(self):
d2l.extract(d2l.download(
d2l.DATA_URL+'fra-eng.zip', self.root,
'94646ad1522d915e7b0f9296181140edcf86a4f5'))
with open(self.root + '/fra-eng/fra.txt', encoding='utf-8') as f:
return f.read()
data = MTFraEng()
raw_text = data._download()
print(raw_text[:75])Downloading ../data/fra-eng.zip from http://d2l-data.s3-accelerate.amazonaws.com/fra-eng.zip...
Go. Va !
Hi. Salut !
Run! Cours !
Run! Courez !
Who? Qui ?
Wow! Ça alors !After downloading the dataset, we proceed with several preprocessing steps for the raw text data. For instance, we replace non-breaking space with space, convert uppercase letters to lowercase ones, and insert space between words and punctuation marks.
英語のヒント
データセットをダウンロードした後、生のテキストデータにいくつかの前処理の手順を施す。たとえば、改行しない空白を通常の空白に置き換え、大文字を小文字に変換し、単語と句読点のあいだに空白を挿入する。
@d2l.add_to_class(MTFraEng) #@save
def _preprocess(self, text):
# Replace non-breaking space with space
text = text.replace('\u202f', ' ').replace('\xa0', ' ')
# Insert space between words and punctuation marks
no_space = lambda char, prev_char: char in ',.!?' and prev_char != ' '
out = [' ' + char if i > 0 and no_space(char, text[i - 1]) else char
for i, char in enumerate(text.lower())]
return ''.join(out)
text = data._preprocess(raw_text)
print(text[:80])go . va !
hi . salut !
run ! cours !
run ! courez !
who ? qui ?
wow ! ça alors !