我正在浏览一个JSON文件,并创建一个由单个单词和其他信息组成的数组。例如,我可能有单词I,像,苹果,词性代词,动词,名词,和词条I,(to)像,苹果。目前,我的代码删除了所有标点符号,并将单个单词添加到数组中,然后我使用zip()来排列所有内容。然而,我当前的实现创建了一个off-by-one错误,因为引理数组最终是四个元素,而其他数组只有三个元素。我尝试删除除括号之外的所有标点符号,但是引理数组总共是6个元素。如何将"( to )“添加到数组中与"like”相同的位置?
我当前的代码是:
with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
data = json.load(f)
#this loop assumes the json is formatted: first-->items-->body-->value
for line in data:
#inputLabels are all the user-specified tiers they want in output
if line['label'] in inputLabels:
position = inputLabels.index(line['label'])
items = line['first']['items']
for item in items:
tokens = word_tokenize(item['body']['value'])
for t in tokens:
t = t.strip('\n')
if not (t in string.punctuation):
newLabels[position].append(t)发布于 2021-06-17 04:44:04
您的数据已经过预处理。例如,如果SPK_WORD中有三个标记,那么SPK_LEMMA、SPK_POS等中也会有三个标记。
data = list()
with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
data = json.load(f)
for line in data:
if line['label'] in inputLabels:
elements = [e['body']['value'] for e in line['first']['items']]
data.append(elements)这将导致包含同步列表的data:
# data
[['I', 'like', 'apples'],
['pron', 'verb', 'noun'],
['I', '(to) like', 'apple']
]在那里,您可以使用zip来获取所有项目并同步处理它们,例如,
for tok in zip(*data):
print(tok)给出
('I', 'pron', 'I')
('like', 'verb', '(to) like')
('apples', 'noun', 'apple')https://stackoverflow.com/questions/68008792
复制相似问题