首页 > 解决方案 > 读取特定 JSON 列以进行标记化

问题描述

我打算用 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)

标签: pythonnlpnltk

解决方案


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

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

推荐阅读