d2l.DATA_HUB['cmn-eng'] = (d2l.DATA_URL + 'cmn-eng.zip', '94646ad1522d915e7b0f9296181140edcf86a4f5') #@save def read_data_nmt(): """载入中文-英文数据集""" with open(d2l.download('cmn-eng'), 'r') as f: lines = f.readlines() return lines raw_text = read_data_nmt() raw_text[:20] def preprocess_nmt(text): """预处理“英语-汉语”数据集""" new_lines=[re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines] text = preprocess_nmt(raw_text) print(text[:80]) 报这个错NameError Traceback (most recent call last) Cell In[52], line 6 3 """预处理“英语-汉语”数据集""" 4 new_lines=[re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines] ----> 6 text = preprocess_nmt(raw_text) 7 print(text[:80]) Cell In[52], line 4, in preprocess_nmt(text) 2 def preprocess_nmt(text): 3 """预处理“英语-汉语”数据集""" ----> 4 new_lines=[re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines] NameError: name 'lines' is not defined 怎验解决
时间: 2024-02-08 14:03:12 浏览: 172
在preprocess_nmt函数中,你使用了名为"lines"的变量,但是该变量未在preprocess_nmt函数中定义。你需要将函数参数text改为lines或者在函数中使用正确的变量名称。修改后的代码如下:
```python
import re
import d2lzh as d2l
d2l.DATA_HUB['cmn-eng'] = (d2l.DATA_URL + 'cmn-eng.zip',
'94646ad1522d915e7b0f9296181140edcf86a4f5')
def read_data_nmt():
"""载入中文-英文数据集"""
with open(d2l.download('cmn-eng'), 'r') as f:
lines = f.readlines()
return lines
def preprocess_nmt(lines):
"""预处理“英语-汉语”数据集"""
new_lines=[re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines]
return new_lines
raw_text = read_data_nmt()
text = preprocess_nmt(raw_text)
print(text[:20])
```
运行该代码后,应该可以正确输出预处理后的英语-汉语数据集的前20行。
相关问题
d2l.DATA_HUB['cmn-eng'] = (d2l.DATA_URL + 'cmn-eng.zip', '94646ad1522d915e7b0f9296181140edcf86a4f5') #@save def read_data_nmt(): """载入“英语-法语”数据集""" with open(d2l.download('time_machine'), 'r') as f: lines = f.readlines() return f.readlines() 报这个错误I/O operation on closed file.,怎样解决
这个错误通常是因为在已经关闭的文件上进行读写操作所引起的。在上述代码中,`f` 文件在 `with` 语句块执行完毕后自动关闭了。因此,当你在 `return` 语句中再次使用 `f.readlines()` 时,就会报出 I/O 错误。
要解决这个问题,你可以将 `f.readlines()` 存储在一个变量中,然后在 `with` 语句块外面返回该变量。修改后的代码如下:
```
import d2l
d2l.DATA_HUB['cmn-eng'] = (d2l.DATA_URL + 'cmn-eng.zip', '94646ad1522d915e7b0f9296181140edcf86a4f5')
def read_data_nmt():
"""载入中文-英文数据集"""
with open(d2l.download('cmn-eng'), 'r') as f:
lines = f.readlines()
return lines
```
这样就不会再报出 I/O 错误了。
from torch.utils import data def load_data_nmt(batch_size, num_steps, num_examples=600): """返回翻译数据集的迭代器和词表""" with open(d2l.download('cmn-eng'), 'r') as f: lines = f.readlines() return lines num_lines = min(num_examples, len(raw_text.split('\n'))) text = raw_text.split('\n')[:num_lines] src_vocab, tgt_vocab = d2l.load_vocab('cmn-eng') src_iter= d2l.build_data_iter(text, src_vocab, tgt_vocab, batch_size, num_steps) return src_iter, src_vocab, tgt_vocab train_iter, src_vocab, tgt_vocab = load_data_nmt(batch_size=2, num_steps=8) for X, X_valid_len, Y, Y_valid_len in train_iter: print('X:', X.type(torch.int32)) print('X的有效长度:', X_valid_len) print('Y:', Y.type(torch.int32)) print('Y的有效长度:', Y_valid_len) break 报这个错误ValueError: not enough values to unpack (expected 3, got 2)
问题出在这一行代码上:
```
src_iter= d2l.build_data_iter(text, src_vocab, tgt_vocab, batch_size, num_steps)
```
这个函数返回的是一个迭代器对象,但是在你的 load_data_nmt 函数中却没有将其打包到元组中一起返回,导致在使用元组解包时数量不足的错误。你需要修改 load_data_nmt 函数,将 src_iter 也添加到返回的元组中,如下所示:
```
def load_data_nmt(batch_size, num_steps, num_examples=600):
"""返回翻译数据集的迭代器和词表"""
with open(d2l.download('cmn-eng'), 'r') as f:
lines = f.readlines()
num_lines = min(num_examples, len(lines))
lines = lines[:num_lines]
src_vocab, tgt_vocab = d2l.load_vocab('cmn-eng')
src_iter, tgt_iter = d2l.build_data_iter(lines, src_vocab, tgt_vocab,
batch_size, num_steps)
return src_iter, tgt_iter, src_vocab, tgt_vocab
```
这样修改后,你的代码就可以正确运行了。
阅读全文