Last active
December 19, 2019 06:58
-
-
Save puraminy/a70459cadb08517c9ad790f5d5bd6e31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import networkx as nx | |
df = pd.DataFrame(np.zeros((5,14)), columns = ['علی','مدرسه', 'را', 'دوست','دار', | |
'برای','او','زندان', 'است', 'کمی','در','درس','خوان', 'بازیگوش']) | |
s1 = 'علی مدرسه را دوست دار' | |
s2 = 'مدرسه برای او زندان است' | |
s3 = 'علی دوست کمی در مدرسه دار' | |
s4 = 'دوست علی در مدرسه درس خوان' | |
s5 = 'علی دوست بازیگوش دار' | |
labels = ['s1','s2','s3','s4','s5'] | |
labels = dict(zip(range(len(labels)), labels)) | |
sents = [s1,s2,s3,s4,s5] | |
for i,s in enumerate(sents): | |
df.loc[i,s.split()]=1 | |
rows, cols = df.shape | |
W = np.zeros((rows, rows)) | |
for i in range(rows): | |
for j in range(i+1, rows): | |
rowi = df.iloc[i,:].values.tolist() | |
rowj = df.iloc[j,:].values.tolist() | |
W[i,j] = sum([x*y for x, y in zip(rowi, rowj)]) | |
# print(W) | |
average = W[W!=0].mean() | |
W[W<average] =0 | |
W[W>=average] =1 | |
W = np.maximum( W, W.transpose() ) | |
# print(W) | |
G = nx.DiGraph(W) | |
pos = {0: (40, 20), 1: (30, 30), 2: (40, 30), 3: (30, 10), 4: (50, 20)} | |
# G.add_nodes_from(pos.keys()) | |
nx.draw(G,labels=labels, pos=pos, with_labels = True, font_size=16, node_color='y', | |
node_size=800) | |
plt.savefig('g.png') | |
print(nx.pagerank(G)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment