代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
import numpy as np
import spacy
import pickle
nlp = spacy.load('en_core_web_sm')
# https://spacy.io/docs/usage/processing-text
def dependency_adj_matrix(text):
document = nlp(text)
seq_len = len(text.split())
matrix = np.zeros((seq_len, seq_len)).astype('float32')
# https://spacy.io/docs/api/token
for token in document:
if token.i < seq_len:
matrix[token.i][token.i] = 1
for child in token.children:
if child.i < seq_len:
matrix[token.i][child.i] = 1
matrix[child.i][token.i] = 1
return matrix
def process(filename):
fin = open(filename, 'r', encoding='utf-8', newline='\n', errors='ignore')
lines = fin.readlines()
fin.close()
idx2graph = {}
fout = open(filename+'.graph', 'wb')
for i in range(0, len(lines), 3):
text_left, _, text_right = [s.lower().strip() for s in lines[i].partition("$T$")]
aspect = lines[i + 1].lower().strip()
adj_matrix = dependency_adj_matrix(text_left+' '+aspect+' '+text_right)
idx2graph[i] = adj_matrix
pickle.dump(idx2graph, fout)
fout.close()
if __name__ == '__main__':
process('./datasets/acl-14-short-data/train.raw')
process('./datasets/acl-14-short-data/test.raw')
process('./datasets/semeval14/restaurant_train.raw')
process('./datasets/semeval14/restaurant_test.raw')
process('./datasets/semeval14/laptop_train.raw')
process('./datasets/semeval14/laptop_test.raw')
process('./datasets/semeval15/restaurant_train.raw')
process('./datasets/semeval15/restaurant_test.raw')
process('./datasets/semeval16/restaurant_train.raw')
process('./datasets/semeval16/restaurant_test.raw')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。