寻找代词并在python中制作一个计数器

时间:2013-03-14 09:16:04

标签: python python-2.7

我是python的新手,我正在编写一个程序,寻找作为主语的代词,并计算计数器,例如 贝拉喜欢音乐,她也喜欢时尚。 柜台应该返回贝拉,2包括代词。 有一个简单的方法来做到这一点 感谢

2 个答案:

答案 0 :(得分:0)

1。使用NLTK。对你的句子进行标记,并且pos标记会给你代词。您还可以使用频率分配进行计数。有关代码示例,请访问他们的网站

<强> EDITED

import nltk

tokens = nltk.word_tokenize("Bella loves music, she also loves fashion")
tagged = nltk.pos_tag(tokens)

还可以使用nltk.download()下载任何缺少的nltk软件包。

标记将每个单词pos标记,然后您可以在循环中计算它们或进行某种频率分配。以下是pos标记列表http://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html

答案 1 :(得分:0)

我会假设它是家庭作业或其他东西(因为个人项目不常见),因此你不能使用图书馆。

#!/usr/bin/python
# homework style, not using libraries

source = "the cat sat on the mat"
pronouns = ["i", "you", "bella", "fred", "he", "she"] # download a list from somewhere

count = 0
words = source.split ()
for w in words:
    if w.lower() in pronouns:
         count += 1
print count

编辑:是的,它变红了,我上午10点30分,当我还没有去睡觉时,我无法匹配报价。