【问题标题】:Reading a Specific JSON Column for Tokenization读取特定 JSON 列以进行标记化
【发布时间】:2020-05-25 03:43:29
【问题描述】:

我打算用 NLTK 标记 JSON 文件中的列。下面的代码根据不同的时间间隔读取 JSON 文件并将其切片。

然而,我正在努力让'Main Text' 列(在 JSON 文件中)在下面代码的最后部分中读取/标记。是否有任何巧妙的调整来实现这一点?

# Loading and reading dataset
file = open("Glassdoor_A.json", "r")
data = json.load(file)
df = pd.json_normalize(data)
df['Date'] = pd.to_datetime(df['Date'])


# Create an empty dictionary
d = dict()


# Filtering by date
start_date = pd.to_datetime('2009-01-01')
end_date = pd.to_datetime('2009-03-31')
last_end_date = pd.to_datetime('2017-12-31')
mnthBeg = pd.offsets.MonthBegin(3)
mnthEnd = pd.offsets.MonthEnd(3)
while end_date <= last_end_date:
    filtered_dates = df[df.Date.between(start_date, end_date)]
    n = len(filtered_dates.index)
    print(f'Date range: {start_date.strftime("%Y-%m-%d")} - {end_date.strftime("%Y-%m-%d")},  {n} rows.')
    if n > 0:
        print(filtered_dates)
    start_date += mnthBeg
    end_date += mnthEnd


# NLTK tokenizing
file_content = open('Main Text').read()
tokens = nltk.word_tokenize(file_content)
print(tokens)

【问题讨论】:

  • “正文”栏指的是什么?
  • 是一个多行的字符串文本。 JSON 文件本身具有以下结构:[ {"No":"121","Stock Symbol":"A","Date":"05/11/2017","Text Main":"Sample text"} ]
  • 嘿,不知道是什么问题。你得到的输出是什么,你期望什么?
  • 我得到了输出file_content = open('Main Text').read(), FileNotFoundError: [Errno 2] No such file or directory: 'Main Text' - 很抱歉没有在问题中指定这一点。问题是我试图标记 JSON 文件中的列,而不是整个文件...

标签: python nlp nltk


【解决方案1】:

我用下面的代码解决了这个问题,运行流畅。再次感谢大家的意见。

    for index, row in filtered_dates.iterrows():
        line = row['Text Main']
        tokens = nltk.word_tokenize(line)
        print(tokens)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2018-11-30
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多