【发布时间】:2019-10-28 12:48:05
【问题描述】:
我正在尝试使用 tensorflow 来模拟波斯诗歌。为此,我需要在我的标记中包含 '\n'。但是,当我使用tokenizer() 时,它不包括下一行。 tf.keras.preprocessing.text.Tokenizer 是否可以包含 '\n'?
data = open(link + "/hafez.txt").readlines() # removing the first two lines
data = data[2:]
data = ''.join(data)
corpus = data.lower().split("\n")
for c in corpus: # including \n in the text
c += '\n'
# update the vocab based on the list of texts ( corpus) returns a dictionary
# of Vocabulary
tokenizer.fit_on_texts(corpus)
print(tokenizer.word_index['\n'])
现在,我们看到 '\n' 不包括在内。
KeyError Traceback(最近调用 最后)在() ----> 1 tokenizer.word_index['\n'] 键错误:'\n'
但是,我稍后需要这个,以便我的神经网络有望将生成的单词按'\n' 分割。
【问题讨论】:
-
您是否阅读了您链接的文档?
__init__()接受一个参数filters。\n在过滤器中。重新定义没有它的字符串。 -
谢谢。我想我需要使用: filter = '!"#$%&()*+,-./:;?@[\]^_`{|}~\t' 我可以使用正则表达式或更简单吗如何做到这一点?
标签: python tensorflow tokenize