Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed Couter so that Python3 can run the project #28

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions pytagcloud/lang/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
import re
from pytagcloud.lang.stopwords import StopWords
from operator import itemgetter
from collections import defaultdict

def get_tag_counts(text):
"""
Search tags in a given text. The language detection is based on stop lists.
This implementation is inspired by https://github.com/jdf/cue.language. Thanks Jonathan Feinberg.
"""
words = map(lambda x:x.lower(), re.findall(r"[\w']+", text, re.UNICODE))

s = StopWords()
s.load_language(s.guess(words))

counted = defaultdict(int)

for word in words:

# words = map(lambda x:x.lower(), re.findall(r'\w+', text, re.UNICODE))
# the line above doesn't work on python 3.5

s = StopWords()
s.load_language(s.guess(map(lambda x:x.lower(), re.findall(r'\w+', text, re.UNICODE))))

counted = {}

for word in map(lambda x:x.lower(), re.findall(r'\w+', text, re.UNICODE)):
if not s.is_stop_word(word) and len(word) > 1:
counted[word] += 1

return sorted(counted.iteritems(), key=itemgetter(1), reverse=True)

if word in counted: #no has_key() method on python 3, use in instead
counted[word] += 1
else:
counted[word] = 1

return sorted(counted.items(), key=itemgetter(1), reverse=True)