Exemple #1
0
def read_tweets(text: TextIO) -> Dict[str, List[tuple]]:
    """ Reads text and interprets it in a form of a tweet. 
    A tweet tuple should have the form (tweet text, date, source, 
    favourite count, retweet count)
    >>> text = open('tweets_small.txt', 'r')
    >>> read_tweets(text)
    """

    d = text.read().split("<<<EOT")
    dic = {}
    # d is now a list of different tweets
    for tweet in d:  # make a tuple
        text = tweet.split(',')[-1]
        text = text.split()[
            2:-1]  # eg. date is located between the first colon and comma
        source = tweet.split(' ')[-1]
        fav_count = tweet.split(',')[-2]
        rtwt_count = tweet.split(',')[-1]
        rtwt_count = rtwt_count.split(' ')[0]
        date = tweet.split(',')[
            0]  #(this gives me everything before first comma)
        date = date.split(':')[-1]  # this is my date
        # source = ...
        tuple = (text, date, source, fav_count, rtwt_count)
        username = tweet.split(',')[0]
        username = username.split(':')[-1]
        if username in dic:
            dic[username].append(tuple)
        else:
            dic[username] = [tuple]
    return dic