/
CompareVietNetOxford.py
148 lines (104 loc) · 4.55 KB
/
CompareVietNetOxford.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
148
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ReadVietNet
import OxfordParser
import FileProcess
import csv
import Util
from nltk.corpus import wordnet as wn
from nltk.metrics import jaccard_distance
def compareVietNetAndOxford(dict_VietNet, dict_Oxford):
for WORD in dict_Oxford:
if len(dict_Oxford[WORD]) == 0:
continue
# if WORD == "BA":
# print "holyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyyholyyy"
wn_words = wn.synsets(WORD, pos = 'n');
if wn_words == None:
continue
if WORD == 'baby':
a = 1;
if dict_VietNet.has_key(WORD):
arr_VietNet = dict_VietNet[WORD];
arr_Oxford = dict_Oxford[WORD]
matrix_similarity = [[0 for x in range(len(arr_Oxford))] for x in range(len(wn_words))];
for iWn in range(len(wn_words)):
definitionWn = wn.synset(wn_words[iWn].name()).definition();
vietNet = {};
for iVietNet in arr_VietNet:
levenshtein_vn_wn = Util.levenshtein(arr_VietNet[iVietNet]["d"],definitionWn)
if levenshtein_vn_wn < len(definitionWn)/2.:
vietNet = arr_VietNet[iVietNet];
break
if not vietNet.has_key("tv"):
vietNet["tv"] = "";
viet_net_tv = vietNet["tv"]
for iOxford in range(len(arr_Oxford)):
oxford = arr_Oxford[str(iOxford)];
vietNet_tv = viet_net_tv
if not oxford.has_key("tv"):
continue
oxford_tv = oxford["tv"].encode("utf-8")
vietNet_tv.replace(";","")
oxford_tv = oxford_tv.replace(";","")
oxford_tv = oxford_tv.replace(",","")
oxford_tv = oxford_tv.replace("/"," ")
arr_tv_oxford = set(oxford_tv.split(" "))
arr_tv_vietnet = set(vietNet_tv.split(" "))
jaccard = jaccard_distance(arr_tv_oxford,arr_tv_vietnet);
print arr_tv_vietnet
print arr_tv_oxford
print jaccard
matrix_similarity[iWn][iOxford] = 0;
if jaccard < 0.95:
matrix_similarity[iWn][iOxford] = 1;
matrix_similarity[iWn].insert(0,viet_net_tv + "<>" + definitionWn.encode("utf-8"));
print matrix_similarity
# - - - - - - - - - - - - - - - - - - - - - - - - -
# col
# for i in range(len(dict_VietNet[WORD])):
# matrix_similarity[i].insert(0,dict_VietNet[WORD][i]["tv"] + "<>" + dict_VietNet[WORD][i]["d"]);
# - - - - - - - - - - - - - - - - - - - - - - - - -
# row
arrRowDict = [];
arrRowDict.append(WORD);
for i in range(len(dict_Oxford[WORD])):
if not dict_Oxford[WORD][str(i)].has_key("tv"):
dict_Oxford[WORD][str(i)]["tv"] = "-";
if not dict_Oxford[WORD][str(i)].has_key("d"):
dict_Oxford[WORD][str(i)]["d"] = "-";
if dict_Oxford[WORD][str(i)]["d"] == None:
dict_Oxford[WORD][str(i)]["d"] = "-";
arrRowDict.append(dict_Oxford[WORD][str(i)]["tv"].encode("utf-8") + "<>" + dict_Oxford[WORD][str(i)]["d"].encode("utf-8"));
FileProcess.append_to_excel_file("Results/parameters/VN_Ox/"+"compare_VN_Ox_2_2.1.csv",arrRowDict,matrix_similarity)
def readResultFile(fileName):
dict = {};
firstline = 1;
f = open(fileName,'r');
reader = csv.reader(f);
word = "";
length_of_col = 0;
for row in reader:
if firstline == 2 and len(row)!=0:
row.pop(0)
arr_temp = row;
dict[word].append(arr_temp);
# get word for key in dict
if firstline == 1 and len(row)!=0:
word = row[0];
dict[word] = [];
# print "\n"
firstline = 2;
# end matrix of a word
if len(row)==0:
firstline = 1;
# print word
# print dict[word]
########################################
return dict
########################################
# readResultFile("Results/parameters/VN_Ox/"+"compare_VN_Ox_v2.csv");
#
# dict_VN = ReadVietNet.readVietNetFile()
# dict_Ox = OxfordParser.readOxfordNouns();
# compareVietNetAndOxford(dict_VN,dict_Ox)