为什么gensim在预处理过程中会忽略下划线?

Why does gensim ignore underscores during preprocessing?

通过 gensim 源代码,我注意到 simple_preprocess 效用函数清除了所有标点符号,除了那些单词以下划线开头的标点符号 _。这有什么原因吗?

def simple_preprocess(doc, deacc=False, min_len=2, max_len=15):
    tokens = [
        token for token in tokenize(doc, lower=True, deacc=deacc, errors='ignore')
        if min_len <= len(token) <= max_len and not token.startswith('_')
    ]
    return tokens

下划线 ('_') 通常不是有意义的标点符号,但在编程和文本处理中通常被视为 "word" 字符。

例如,常见的正则表达式语法使用 \w 表示 "word character"。每 https://www.regular-expressions.info/shorthand.html

\w stands for "word character". It always matches the ASCII characters [A-Za-z0-9_]. Notice the inclusion of the underscore and digits. In most flavors that support Unicode, \w includes many characters from other scripts. There is a lot of inconsistency about which characters are actually included. Letters and digits from alphabetic scripts and ideographs are generally included. Connector punctuation other than the underscore and numeric symbols that aren't digits may or may not be included. XML Schema and XPath even include all symbols in \w. Again, Java, JavaScript, and PCRE match only ASCII characters with \w.

因此,它经常用于创作或其他文本预处理步骤,以连接 letters/numbers 的其他组, 应该 保持为一个单元.因此,它通常不会与其他真正的标点符号一起清除。

您引用的代码还做了其他事情,这与您关于清除标点符号的问题不同:它删除以 _ 开头的单词标记。

我不确定为什么会这样;在某些时候,代码的设计可能考虑了一些特定的文本格式,其中前导下划线标记是语义上不重要的格式指令。

gensim 中的 simple_preprocess() 函数只是一个快速而简单的基线,有助于内部测试和紧凑的初学者教程。它不应被视为 "best practice"。

真正的项目应该更多地考虑对他们的数据和目的有意义的词标记化——或者寻找具有更多选项的库,或者自定义方法(仍然不需要超过几行Python),以实施最适合他们需求的标记化。