English 中文(简体)
将 nltk. FreqDitt 单词分隔为两个列表吗?
原标题:Separating nltk.FreqDist words into two lists?

我有一系列文本是自定义 WebText 类的例子。 每个文本都是一个带有评级( 10 到 +10) 和单词计数( nltk. FreqDist) 的对象 :

>>trainingTexts = [WebText( train1.txt ), WebText( train2.txt ), WebText( train3.txt ), WebText( train4.txt )]
>>trainingTexts[1].rating
10
>>trainingTexts[1].freq_dist
<FreqDist:  the : 60,  , : 49,  to : 38,  is : 34,...>

您现在如何获得两个单词列表( 或词典), 包含所有正值或负值文本的总字数, 以便您得到这样的信息 :

>>only_positive_words
[( sky , 10), ( good , 9), ( great , 2)...] 
>>only_negative_words
[( earth , 10), ( ski , 9), ( food , 2)...] 

我考虑过使用数据集,因为数据集包含独特的实例, 但我不明白如何用 nltk. FreqDist 来做到这一点, 并且, 此外, 一组不会按字数频率排序 。 有什么想法吗?

最佳回答

好吧,让我们假设你从这个开始 是为了测试的目的:

class Rated(object): 
  def __init__(self, rating, freq_dist): 
    self.rating = rating
    self.freq_dist = freq_dist

a = Rated(5, nltk.FreqDist( the boy sees the dog .split()))
b = Rated(8, nltk.FreqDist( the cat sees the mouse .split()))
c = Rated(-3, nltk.FreqDist( some boy likes nothing .split()))

trainingTexts = [a,b,c]

那么你的代码会看起来像:

from collections import defaultdict
from operator import itemgetter

# dictionaries for keeping track of the counts
pos_dict = defaultdict(int)
neg_dict = defaultdict(int)

for r in trainingTexts:
  rating = r.rating
  freq = r.freq_dist

  # choose the appropriate counts dict
  if rating > 0:
    partition = pos_dict
  elif rating < 0: 
    partition = neg_dict
  else:
    continue

  # add the information to the correct counts dict
  for word,count in freq.iteritems():
    partition[word] += count

# Turn the counts dictionaries into lists of descending-frequency words
def only_list(counts, filtered):
  return sorted(filter(lambda (w,c): w not in filtered, counts.items()), 
                key=itemgetter(1), 
                reverse=True)

only_positive_words = only_list(pos_dict, neg_dict)
only_negative_words = only_list(neg_dict, pos_dict)

结果就是:

>>> only_positive_words
[( the , 4), ( sees , 2), ( dog , 1), ( cat , 1), ( mouse , 1)]
>>> only_negative_words
[( nothing , 1), ( some , 1), ( likes , 1)]
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签