1

I wrote a simple function.

def common_long_words(text):
    sorted(w for w in set(text) if len(w) > 7 and (FreqDist(text))[w] > 7)

This is stuck.

Also, [w for w in set(text5) if len(w) > 7 and FreqDist(text5)[w] > 7 fails. It just get stuck.

However, this works:

fdist5 = FreqDist(text5)
[w for w in set(text5) if len(w) > 7 and fdist5[w] > 7

Does it not work like that in python? Why is that? Also, why is it stuck, if this is wrong, it should come out as an error, syntax or runtime.


This works, flawlessly and fast:

>>> def common_long_words(text):
...     fdist = FreqDist(text)
...     print(sorted(w for w in set(text) if len(w) > 7 and fdist[w] > 7))
...
>>> for t in all_texts:
...     common_long_words(t)
Anubhav
  • 173
  • 2
  • 7

1 Answers1

1

To stop the condition of loop, you have called two methods, the size of which is unknown. As you mentioned in the comment, you should get the answer FreqDist(text) and then apply it to your condition. (Here, consider the length of FreqDist(text) You gave.) This example is implemented on the fifth book of nltk (text5).

def common_long_words(t):
   fdist = FreqDist(t)
   print(sorted([w for w in set(t) if len(w) > 7 and fdist[w] > 7]))
common_long_words(text5)

The result:

['#14-19teens', '#talkcity_adults', '((((((((((', '........', 'Question', 'actually', 'anything', 'computer', 'cute.-ass', 'everyone', 'football', 'innocent', 'listening', 'remember', 'seriously', 'something', 'together', 'tomorrow', 'watching']
Omid Sotooni
  • 159
  • 5