首页 > 其他 > 详细

通过依存关系生成邻接矩阵

时间:2021-05-10 22:50:11      阅读:34      评论:0      收藏:0      [点我收藏+]
import spacy
from tqdm import  tqdm
import numpy as np

# In[8]
def adj_dependcy_tree(argments, max_length=80):
    nlp = spacy.load(‘en‘)
    depend = []
    depend1 = []
    doc = nlp(str(argments))
    d = {}
    i = 0
    for (_, token) in enumerate(doc):
        if str(token) in d.keys():
            continue
        d[str(token)] = i
        i = i+1
    for token in doc:
        depend.append((token.text, token.head.text))
        depend1.append((d[str(token)], d[str(token.head)]))

    ze = np.identity(80)
    for (i, j) in depend1:
        if i>=80 or j>=80:
            continue
        ze[i][j] = 1
    D = np.array(np.sum(ze, axis=1))
    D = np.matrix(np.diag(D))
    DSN = np.sqrt(D**-1)
    DN = D**-1
    return ze,DN,DSN


# In[8]
# adj1, d1, dsn1 = adj_dependcy_tree(‘I love eating apple‘)
# d1 

通过依存关系生成邻接矩阵

原文:https://www.cnblogs.com/douzujun/p/14752920.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!