本文介绍了百分比计数动词,名词是否使用Spacy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用类似于

计算动词,名词和其他python的NLTK的语音部分

当前能够检测和计数POS.如何找到百分比拆分.

Currently able to detect and count POS. How to find percentage split.

from __future__ import unicode_literals
import spacy,en_core_web_sm
from collections import Counter
nlp = en_core_web_sm.load()
print Counter(([token.pos_ for token in nlp('The cat sat on the mat.')]))

当前输出:

Counter({u'NOUN': 2, u'DET': 2, u'VERB': 1, u'ADP': 1, u'PUNCT': 1})

预期输出:

Noun: 28.5%
DET: 28.5%
VERB: 14.28%
ADP: 14.28%
PUNCT: 14.28%

如何将输出写入熊猫数据框?

How to write the output to pandas dataframe?

推荐答案

from __future__ import unicode_literals
import spacy,en_core_web_sm
from collections import Counter
nlp = en_core_web_sm.load()
c = Counter(([token.pos_ for token in nlp('The cat sat on the mat.')]))
sbase = sum(c.values())
for el, cnt in c.items():
    print(el, '{0:2.2f}%'.format((100.0* cnt)/sbase))

输出:

(u'NOUN', u'28.57%')
(u'VERB', u'14.29%')
(u'DET', u'28.57%')
(u'ADP', u'14.29%')
(u'PUNCT', u'14.29%')

这篇关于百分比计数动词,名词是否使用Spacy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 06:55