2

This is my tweets file

#!/usr/bin/env python3

    import os
    import sys

    from analyzer import Analyzer
    from termcolor import colored
    from helpers import get_user_timeline

    def main():

        # ensure proper usage
        if len(sys.argv)!=2 or sys.argv[1][0]!="@":
            sys.exit("Usage: ./tweets @userstweeter")

        # get tweets
        tweets = get_user_timeline(sys.argv[1][1:], 50)
        if tweets == None:
            sys.exit("Error")  
        print (tweets)

        # absolute paths to lists
        positives = os.path.join(sys.path[0], "positive-words.txt")
        negatives = os.path.join(sys.path[0], "negative-words.txt")

        # instantiate analyzer
        analyzer = Analyzer(positives, negatives)

        # analyze word
        score = analyzer.analyze(tweets)
        if score > 0.0:
            print(colored(":)", "green"))
        elif score < 0.0:
            print(colored(":(", "red"))
        else:
            print(colored(":|", "yellow"))

    if __name__ == "__main__":
        main()

This is my analyzer.py

import nltk
import string
import sys

from nltk.tokenize import TweetTokenizer

class Analyzer():
    """Implements sentiment analysis."""

    def __init__(self, positives, negatives):
        """Initialize Analyzer."""

        self.positives = set()
        with open("positive-words.txt") as file:
            for line in file:
                if line[0]!=" " and line[0]!=";":
                    self.positives.add(line.rstrip("\n"))

        self.negatives = set()
        with open("positive-words.txt") as file:
            for line in file:
                if not line.startswith(';') and not line.startswith(' '):
                    self.positives.add(line.rstrip("\n"))

    def analyze(self, text):
        """Analyze text for sentiment, returning its score."""

        tokenizer = nltk.tokenize.TweetTokenizer(preserve_case=False)
        tokens = tokenizer.tokenize(text)

        pos = 0
        neg = 0
        for tok in tokens:
            if tok in self.positives:
                pos += 1
            if tok in self.negatives:
                neg -= 1

        return pos + neg

This is the error.

Traceback (most recent call last):
      File "./tweets", line 39, in <module>
        main()
      File "./tweets", line 30, in main
        score = analyzer.analyze(tweets)
      File "/home/ubuntu/workspace/pset6/sentiments/analyzer.py", line 29, in analyze
        tokens = tokenizer.tokenize(text)
      File "/home/ubuntu/.local/lib/python3.4/site-packages/nltk/tokenize/casual.py", line 294, in tokenize
        text = _replace_html_entities(text)
      File "/home/ubuntu/.local/lib/python3.4/site-packages/nltk/tokenize/casual.py", line 258, in _replace_html_entities
        return ENT_RE.sub(_convert_entity, _str_to_unicode(text, encoding))
    TypeError: expected string or buffer
2
  • Looks like this has an answer over on reddit Commented Jan 16, 2017 at 23:02
  • Which line of code is the TypeError generated on?
    – John Quinn
    Commented Nov 22, 2017 at 16:20

1 Answer 1

1

The problem is get_user_timeline because it returns a list of strings i.e you can't just pass it to the tokenize function. Instead you have to iterate over the strings and tokenize them seperatly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .