【问题标题】:Removing punctuation from my nested and tokenized list从我的嵌套和标记列表中删除标点符号
【发布时间】: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']]

任何和所有建议都表示赞赏。

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    假设每个标点符号都是一个单独的标记,你可以这样:

    import string
    
    sentences = [[], ['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']]
    
    
    result = [list(filter(lambda x: x not in string.punctuation, sentence)) for sentence in sentences]
    
    print(result)
    

    输出

    [[], ['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']]
    

    这个想法是使用filter,删除那些标点符号,因为过滤器返回一个迭代器使用列表将其转换回列表。您也可以使用等效的列表推导:

    result = [[token for token in sentence if token not in string.punctuation] for sentence in sentences]
    

    【讨论】:

    • 这就是我想要的。谢谢!我在 listcomprehension 方法中添加了一个过滤器,以获得我想要的东西。对于以后阅读本文的其他人,我在其上放置了一个过滤器以删除空格: result = list(filter(None, result))
    【解决方案2】:

    为此,您需要运行 Python 3.x。 此外,b 包含您提供的示例嵌套列表

    import string
    # Remove empty lists
    b = [x for x in b if x]
    # Make flat list
    b = [x for sbl in b for x in sbl]
    # Define translation
    translator = str.maketrans('', '', string.punctuation)
    # Apply translation
    b = [x.translate(translator) for x in b]
    # Remove empty strings
    b = list(filter(None, b))
    

    为什么它以前不起作用的参考: Python 2 maketrans() function doesn't work with Unicode: "the arguments are different lengths" when they actually are

    【讨论】:

    • 这几乎可以满足我的要求。有没有办法扭转列表的扁平化?我需要保留我之前确定的列表中的单词。
    • 试试:b = [x for x in b if x] # Define translation translator = str.maketrans('', '', string.punctuation) # Apply translation b = [[word.translate(translator) for word in sbl] for sbl in b] # Remove empty strings b = [list(filter(None, sbl)) for sbl in b]
    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多