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

Example not working on Python 3 #25

Open
UriAceves opened this issue Mar 22, 2017 · 2 comments
Open

Example not working on Python 3 #25

UriAceves opened this issue Mar 22, 2017 · 2 comments

Comments

@UriAceves
Copy link

UriAceves commented Mar 22, 2017

Hello, I tried the example on Python 3.5 and realized it was not compatible. In order to make it function on this version I modified the pytagcloud/lang/counter.pyfile to look like this

# -*- coding: utf-8 -*-
import re
from pytagcloud.lang.stopwords import StopWords
from operator import itemgetter

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))
    # 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:
            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)

I leave this here because I did not find a branch for python 3 and maybe someone could find it useful

@arjunbazinga
Copy link

Thanks @UriAceves , works perfectly.

@fhg-isi
Copy link

fhg-isi commented Mar 21, 2024

In order to be able to find this easier, here is a corresponding error message:


AttributeError Traceback (most recent call last)
Cell In[2], line 7
2 from pytagcloud.lang.counter import get_tag_counts
4 YOUR_TEXT = "A tag cloud is a visual representation for text data, typically
5 used to depict keyword metadata on websites, or to visualize free form text."
----> 7 tags = make_tags(get_tag_counts(YOUR_TEXT), maxsize=80)
9 create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster')
11 import webbrowser

File ...\python-3.11.6.amd64\Lib\site-packages\pytagcloud\lang\counter.py:25, in get_tag_counts(text)
22 else:
23 counted[word] = 1
---> 25 return sorted(counted.iteritems(), key=itemgetter(1), reverse=True)

AttributeError: 'dict' object has no attribute 'iteritems'

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants