【发布时间】:2019-04-04 12:39:22
【问题描述】:
我正在尝试从我的嵌套和标记化列表中删除标点符号。我已经尝试了几种不同的方法,但无济于事。我最近的尝试是这样的:
def tokenizeNestedList(listToTokenize):
flat_list = [item.lower() for sublist in paragraphs_no_guten for item in sublist]
tokenList = []
for sentence in flat_list:
sentence.translate(str.maketrans(",",string.punctuation))
tokenList.append(nltk.word_tokenize(sentence))
return tokenList
如您所见,我在对列表进行标记时尝试删除标点符号,在调用我的函数时,任何人都会遍历该列表。但是,当尝试这种方法时,我得到了错误
ValueError: the first two maketrans arguments must have equal length
我有点理解为什么会发生。运行我的代码而不尝试删除标点符号并打印前 10 个元素给了我(所以你知道我在做什么):
[[], ['title', ':', 'an', 'inquiry', 'into', 'the', 'nature', 'and', 'causes', 'of', 'the', 'wealth', 'of', 'nations'], ['author', ':', 'adam', 'smith'], ['posting', 'date', ':', 'february', '28', ',', '2009', '[', 'ebook', '#', '3300', ']'], ['release', 'date', ':', 'april', ',', '2002'], ['[', 'last', 'updated', ':', 'june', '5', ',', '2011', ']'], ['language', ':', 'english'], [], [], ['produced', 'by', 'colin', 'muir']]
任何和所有建议都表示赞赏。
【问题讨论】: