-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_cytoscape_gephi_final.py
147 lines (117 loc) · 5.08 KB
/
matrix_cytoscape_gephi_final.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#Python 3##############################################################################################
# #NEED UPDATE
# apre un file -- .lemmi_str -- che contiene frasi con 2 o piu' lemmi modali gia' separati,
# compila la matrice delle relazioni e la scrive su file csv
#
############################################################
#
# importa i parametri script (nome file da elaborare)
# e separa nome_file e percorso file_output.lemmi.str
#
############################################################
import sys
if len(sys.argv)!=2:
print ('Devi passare allo script il nome del file da elaborare')
print ('Devi dare il percorso completo')
print ('Sto uscendo...')
sys.exit()
nome_script,percorso_e_nome_file=sys.argv
import os
percorso_e_nome_file_senza_estensione = percorso_e_nome_file[0:-10]
#print(percorso_e_nome_file_senza_estensione)
###################################
#
# importa la libreria csv per
# scrittura
#
###################################
import csv
##################################
#
# lista dei lemmi modali
#
##################################
lemmi_modali=['debeo','possum','queo','nequeo','decet','licet','oportet','valet','aequum sum','meum sum','ius sum','necesse sum','opus sum','usus sum','aequus','iniquus',\
'aptus','ineptus','certus','incertus','dubius','licitus','illicitus','necessarius','certo','dubium','dubio','necessarium','necessario','dubium','facultas','possibilitas',\
'potestas','necessitas','necessitudo','probabilitas','aeque','certe','dubie','forsitan','fortasse', 'forte','indubitate','indubitanter','necessarie','possibiliter','probabiliter'] #lista aggiornata:eliminato certo e rivisti i composti
######################################################
#
# dimensionamento matrice relazione lemmi modali
#
######################################################
matrix = [[0 for col in range(len(lemmi_modali))] for row in range(len(lemmi_modali))]
########################################################
#
# apre file .lemmi_str
# formato file: #frase\n lemma1,lemma2,lemman\n
# scarta la frase e divide i lemmi modali
#
#######################################################
with open (percorso_e_nome_file) as f:
for line in f:
if line[0] == '#':
#print (line)
pass #scarta la frase, preceduta da #
else:
line = line[:-1] #elimina \n (newline) finale
lemmi = line.split(',', -1) #divide in lemmi (virgola=separatore)
print lemmi
######################################################################
#
# cerca la posizione del primo lemma nella lista lemmi_modali (x) e quella del successivo (y)
# e incrementa il contatore della relazione
#
######################################################################
for i in range(len(lemmi)-1):
for j in range((i + 1), len(lemmi)):
x = lemmi_modali.index(lemmi[i])
y = lemmi_modali.index(lemmi[j])
matrix [x][y] = matrix [x][y] + 1
#print (i,j,lemmi[i], lemmi[j],x,y)
#print (lemmi_modali[x], matrix [x])
###########################################################################
#
# scrive la matrice in csv nel formato per cytoscape
#
#
###########################################################################
with open(percorso_e_nome_file_senza_estensione + '_cytoscape.csv', 'w') as csvfile1:
#intestazione colonne
matrix_writer = csv.writer(csvfile1, delimiter = ',')
matrix_writer.writerow(['Source', 'Target', 'Type', 'Id', 'Label', 'timeset', 'Weight'])
id_count = 0
for i in range (len(lemmi_modali) - 1):
for j in range (len(lemmi_modali) -1):
if matrix [i][j] != 0:
riga_n = [lemmi_modali[i], lemmi_modali[j], 'Directed', id_count, '', '', matrix[i][j]]
id_count = id_count + 1
#print (i,j,riga_n)
matrix_writer.writerow(riga_n)
###########################################################################
#
# scrive la matrice in csv con lista lemmi= intestazione colonne
# e lemma(i)=intestazione riga per lettura su gephi
#
###########################################################################
with open(percorso_e_nome_file_senza_estensione + '_matrix_gephi.csv', 'w') as csvfile:
#intestazione colonne
matrix_writer = csv.writer(csvfile, delimiter = ',')
matrix_writer.writerow(([''] + lemmi_modali))
#righe della matrice con inserimento intestazione riga
for i in range (len(lemmi_modali)):
riga_n = matrix[i]
riga_n.insert(0, lemmi_modali[i])
#print (riga_n)
matrix_writer.writerow(riga_n)
######################################################################
#
# Output a video
#
######################################################################
print('#####################################################################')
print('#')
print('# File csv per gephi scritto in ' + percorso_e_nome_file_senza_estensione + '_matrix_gephi.csv')
print('#')
print('# File csv per cytoscape scritto in ' + percorso_e_nome_file_senza_estensione + '_cytoscape.csv')
print('#')
print('#####################################################################')