Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于python中的正则表达式对tweet类型进行分类_Python_Regex_Twitter - Fatal编程技术网

基于python中的正则表达式对tweet类型进行分类

基于python中的正则表达式对tweet类型进行分类,python,regex,twitter,Python,Regex,Twitter,延长这一问题: 我想将数据集中的每条tweet分类为以下内容之一,并将tweet类型附加到数据集中的每条记录中。目前,当我运行下面所示的脚本时,我得到了返回的[None],所以很明显我在这里遗漏了一些东西 当前格式: 所需格式: 推特分类: 1转发->在tweet文本列的某处有一个RT@anyusername 2提及->tweet列中有@anyusername,但没有RT@anyusername 3 Tweet->Tweet列中既没有RT@anyusername,也没有任何@anyusernam

延长这一问题:

我想将数据集中的每条tweet分类为以下内容之一,并将tweet类型附加到数据集中的每条记录中。目前,当我运行下面所示的脚本时,我得到了返回的[None],所以很明显我在这里遗漏了一些东西

当前格式:

所需格式:

推特分类:

1转发->在tweet文本列的某处有一个RT@anyusername

2提及->tweet列中有@anyusername,但没有RT@anyusername

3 Tweet->Tweet列中既没有RT@anyusername,也没有任何@anyusername

代码:


有两个问题:

tweet_type.appendi.append['tweet']-调用list.append不会返回任何内容,这意味着它们隐式地不返回任何内容。您正在向tweet_类型追加“无”。这是主要问题

return tweet_type向右缩进太多-循环在第一次迭代时退出函数


@柯蒂斯,你需要替换我。。。使用要放置到tweet_type的值。因为您没有docstring,而且每个参数都是parameter1,所以很难判断您打算在其中发生什么我的最终目标是获取一个tweets['CREATED_AT']['text']列表,根据tweet的类型,我想将tweet的类型附加到每一行,这样我最终会得到['CREATED_AT']['text']['tweet_type'],也许你可以编辑原始帖子中的代码,使其更具可读性?
 ['CREATED_AT']['text']
['CREATED_AT']['text']['tweet_type']
import json
import time
import re

# load Twitter Streaming JSON data into a dict
def import_tweets(parameter1):
    data = []
    for line in open(parameter1):
      try: 
        data.append(json.loads(line))
      except:
        pass

    for i in data:
        i['CREATED_AT'] = time.strftime('%Y-%m-%d %H:%M:%S',time.strptime(i['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))
    return data

# Extract timestamp and tweet text into a list
def extract_tweets(parameter2):
    tweets = []
    for i in parameter2:
        tweets.append(
        [i['CREATED_AT'],
         i['text']]        
        )
    return tweets

# Classify each tweet as retweet/mention/tweet
def tweet_type(parameter3):
    tweet_type = []
    for i in parameter3:
        match = re.match(r'RT\s@....+', i[1])   
        if match:
            tweet_type.append([i, 'retweet'])
        else:
            match = re.match(r'@....+', i[1])
            if match:
               tweet_type.append([i, 'reply'])
            else:
                match = re.match(r'....+@', i[1])
                if match:
                   tweet_type.append([i, 'mention'])
                else:
                   tweet_type.append([i, 'tweet'])                
    return tweet_type    

data = import_tweets('tweets.json')
tweets = extract_tweets(data)
tweet_type = tweet_type(tweets)

# Print sample to make sure tweet text was classified properly
print tweet_type[:5]
import json
import time
import re

def import_tweets(parameter1):
    """
    Loads data from Twitter Streaming API for analysis.

    Args:
        parameter1:  input file
    Returns:
        List of nested dictionaries
    """

    # load JSON data into a dict    
    data = []
    for line in open(parameter1):
        try: 
            data.append(json.loads(line))
        except:
            pass

    # Transform Twitter tweet date/time format into standard date/time format
    for i in data:
        i['CREATED_AT'] = time.strftime('%Y-%m-%d %H:%M:%S',time.strptime(i['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))

    # Classify each tweet as a retweet/reply/mention/tweet
    for i in data:
        match = re.match(r'RT\s@....+', i['text'])   
        if match:
            i['TYPE'] = 'retweet'
        else:
            match = re.match(r'@....+', i['text'])
            if match:
                i['TYPE'] = 'reply'
            else:
                match = re.match(r'....+@', i['text'])
                if match:
                    i['TYPE'] = 'mention'
                else:
                    i['TYPE'] = 'tweet'

    return data