- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 scikit-learn 的 CountVectorizer
计算字符 2-gram,忽略空格。在docs它提到了参数 analyzer
声明
Whether the feature should be made of word or character n-grams. Option ‘char_wb’ creates character n-grams only from text inside word boundaries.
但是,“char_wb”似乎没有像我预期的那样工作。例如:
corpus = [
"The blue dog Blue",
"Green the green cat",
"The green mouse",
]
# CountVectorizer character 2-grams with word boundaries
vectorizer = CountVectorizer(analyzer='char_wb', ngram_range=(2, 2), min_df=1)
X = vectorizer.fit_transform(corpus)
vectorizer.get_feature_names()
[' b',
' c',
' d',
' g',
' m',
' t',
'at',
'bl',
'ca', ....
请注意像“b”这样的示例,其中包含一个空格。给了什么?
最佳答案
我认为这是文档中长期存在的错误,欢迎您帮助修复。更正确的说法是:
Option ‘char_wb’ creates character n-grams, but does not generate n-grams that cross word boundaries.
更改似乎是在 this commit 中进行的以确保;查看贡献者的 comment .将二元语法输出与 analyzer='char'
的输出进行比较时,它看起来特别尴尬,但是当你增加到三元语法时,你会看到空格可以开始或结束一个 n-gram,但不能在中间.这有助于表示特征的词首或词尾性质,而无需捕获嘈杂的交叉词字符 n-gram。它还确保,与提交之前不同,所有提取的 n-gram 都具有相同的长度!
关于python - CountVectorizer(分析器 ='char_wb')未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36188875/
我正在尝试使用 scikit-learn 的 CountVectorizer 计算字符 2-gram,忽略空格。在docs它提到了参数 analyzer 声明 Whether the feature
我用 singleTFIDF = TfidfVectorizer( analyzer='char_wb', ngram_range=(4,6), stop_words=my_s
我有 import nltk from nltk.stem.snowball import GermanStemmer def my_tokenizer(doc): stemmer= Germa
我是一名优秀的程序员,十分优秀!