atizo / PyTagCloud

Create beautiful tag clouds as images or HTML
https://labs.atizo.com/software/#pytagcloud
BSD 3-Clause "New" or "Revised" License
397 stars 128 forks source link

Example not working on Python 3 #25

Open UriAceves opened 7 years ago

UriAceves commented 7 years ago

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 commented 7 years ago

Thanks @UriAceves , works perfectly.

fhg-isi commented 6 months ago

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'