python - 我正在尝试标记 text/json 文件。不知道为什么,但只有第一条推文被标记化。代码如下

标签 python twitter tokenize

from nltk.tokenize import word_tokenize
import json
import re

emoticons_str = r"""
    (?:
        [:=;] # Eyes
        [oO\-]? # Nose (optional)
        [D\)\]\(\]/\\OpP] # Mouth
    )"""

regex_str = [
    emoticons_str,
    r'<[^>]+>', # HTML tags
    r'(?:@[\w_]+)', # @-mentions
    r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)", # hash-tags
    r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-  9a-f]))+', # URLs
    r'(?:(?:\d+,?)+(?:\.?\d+)?)', # numbers
    r"(?:[a-z][a-z'\-_]+[a-z])", # words with - and '
    r'(?:[\w_]+)', # other words
    r'(?:\S)' # anything else
    ]

tokens_re = re.compile(r'('+'|'.join(regex_str)+')', re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r'^'+emoticons_str+'$', re.VERBOSE | re.IGNORECASE)


def tokenize(s):
    return tokens_re.findall(s)


def preprocess(s, lowercase=False):
    tokens = tokenize(s)
    if lowercase:
        tokens = [token if emoticon_re.search(token) else token.lower() for token in tokens]
    return tokens


#tweet = "RT @marcobonzanini: just an example! :D http://example.com #NLP"
#print(preprocess(tweet))
# ['RT', '@marcobonzanini', ':', 'just', 'an', 'example', '!', ':D', 'http://example.com', '#NLP']

with open('../script/iphone.txt', 'r') as f: 
   for line in f:
        tweet = json.loads(line)
        tokens = preprocess(tweet['text'])
        #do_something_else(tokens)
        print(json.dumps(tokens, indent=4)

this is how it looks

最佳答案

我遇到了同样的问题,这是因为你的 json 文件有一个空行占用了 Json。尝试添加:

newline='\r\n'

所以读取 json 文件的代码如下所示:

with open('data/stream_sample.json', 'r', newline='\r\n') as f:
for line in f:
    tweet = json.loads(line)
    tokens = preprocess(tweet['text'])
    print(tokens)

希望有帮助

关于python - 我正在尝试标记 text/json 文件。不知道为什么,但只有第一条推文被标记化。代码如下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36257964/

相关文章:

java - 如何在 Java 中逐字符读取输入?

Python:有没有办法强制父函数返回?

python - 如何在 python 中读取 .float 文件?

python - 如何使用Python查找图片中的几个关键像素点?

ios - 向 Twitter 发布状态更新返回 403 forbidden 错误

java - 没有这样的元素异常(exception)

python - 用于网络分析屏幕的Django设计模式需要很长时间才能计算出来

ios - 在 iOS 应用程序中创建共享按钮(twitter facebook ...)

c# - C# 中的 Twitter Oauth

java - 将单词标记为古吉拉特语的音节(古吉拉特语字符)