相关疑难解决方法(0)

如何使用sklearn的CountVectorizerand()来获取包含任何标点符号作为单独标记的ngram?

我使用sklearn.feature_extraction.text.CountVectorizer来计算n-gram.例:

import sklearn.feature_extraction.text # FYI http://scikit-learn.org/stable/install.html
ngram_size = 4
string = ["I really like python, it's pretty awesome."]
vect = sklearn.feature_extraction.text.CountVectorizer(ngram_range=(ngram_size,ngram_size))
vect.fit(string)
print('{1}-grams: {0}'.format(vect.get_feature_names(), ngram_size))
Run Code Online (Sandbox Code Playgroud)

输出:

4-grams: [u'like python it pretty', u'python it pretty awesome', u'really like python it']
Run Code Online (Sandbox Code Playgroud)

删除标点符号:如何将它们作为单独的标记包含在内?

python nlp tokenize n-gram scikit-learn

5
推荐指数
1
解决办法
4483
查看次数

python计算字符串列表中的单词数

考虑

doc = ["i am a fellow student", "we both are the good student", "a student works hard"]
Run Code Online (Sandbox Code Playgroud)

我有这个作为输入我只想打印整个列表中每个单词出现的次数:

例如 student 出现 3 次所以 预期输出student=3, a=2, etc

我能够打印文档中的唯一单词,但无法打印出现的次数。这是我使用的功能:

def fit(doc):    
    unique_words = set() 
    if isinstance(dataset, (list,)):
        for row in dataset:
            for word in row.split(" "): 
                if len(word) < 2:
                    continue
                unique_words.add(word)
        unique_words = sorted(list(unique_words))
        return (unique_words)
doc=fit(docs)

print(doc)

['am', 'are', 'both', 'fellow', 'good', 'hard', 'student', 'the', 'we', 'works']
Run Code Online (Sandbox Code Playgroud)

我得到这个作为输出我只想要unique_words的出现次数。请问我该怎么做?

python string list count cpu-word

4
推荐指数
1
解决办法
2546
查看次数

标签 统计

python ×2

count ×1

cpu-word ×1

list ×1

n-gram ×1

nlp ×1

scikit-learn ×1

string ×1

tokenize ×1