Repository navigation
Expand file tree
/
Copy pathDecoder.cs
More file actions
211 lines (175 loc) · 5.91 KB
/
Copy pathDecoder.cs
File metadata and controls
211 lines (175 loc) · 5.91 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Txt2Vec
{
public enum TermOperation { ADD, SUB };
public class TermOP
{
public string strTerm;
public TermOperation operation;
}
public class Result : IComparable<Result>
{
public string strTerm;
public double score;
public Result()
{
strTerm = null;
score = -1;
}
int IComparable<Result>.CompareTo(Result other)
{
return other.score.CompareTo(score);
}
}
public class Decoder
{
int BLOCK_N = 16;
Model model;
object locker = new object();
ParallelOptions parallelOption;
public Decoder(Model m)
{
parallelOption = new ParallelOptions();
model = m;
}
public double[] ToVector(string[] termList, int N = 40)
{
List<TermOP> termOPList = new List<TermOP>();
foreach (string term in termList)
{
TermOP termOP = new TermOP();
termOP.strTerm = term;
termOP.operation = TermOperation.ADD;
termOPList.Add(termOP);
}
double[] vec = GetVector(termOPList);
return vec;
}
public double[] GetVector(List<TermOP> termList)
{
double[] vec = new double[model.VectorSize];
//Calculate input terms' vector
for (int b = 0; b < termList.Count; b++)
{
Term term = model.GetTerm(termList[b].strTerm);
if (term == null)
{
continue;
}
if (termList[b].operation == TermOperation.ADD)
{
for (int a = 0; a < model.VectorSize; a++)
{
vec[a] += term.vector[a];
}
}
else if (termList[b].operation == TermOperation.SUB)
{
for (int a = 0; a < model.VectorSize; a++)
{
vec[a] -= term.vector[a];
}
}
}
return vec;
}
private List<TermOP> GenerateTermOP(string[] termList)
{
List<TermOP> termOPList = new List<TermOP>();
foreach (string term in termList)
{
TermOP termOP = new TermOP();
termOP.strTerm = term;
termOP.operation = TermOperation.ADD;
termOPList.Add(termOP);
}
return termOPList;
}
public double Similarity(string[] tokens1, string[] tokens2)
{
double score = 0;
List<TermOP> termOPList1 = GenerateTermOP(tokens1);
List<TermOP> termOPList2 = GenerateTermOP(tokens2);
double[] vec1 = GetVector(termOPList1);
double[] vec2 = GetVector(termOPList2);
//Cosine distance
for (int i = 0; i < model.VectorSize; i++)
{
score += vec1[i] * vec2[i];
}
return score;
}
public List<Result> Distance(string strTerm, int N = 40)
{
string[] termList = new string[1];
termList[0] = strTerm;
return Distance(termList, N);
}
//N is the number of closest words that will be shown
public List<Result> Distance(string[] termList, int N = 40)
{
List<TermOP> termOPList = new List<TermOP>();
foreach (string term in termList)
{
TermOP termOP = new TermOP();
termOP.strTerm = term;
termOP.operation = TermOperation.ADD;
termOPList.Add(termOP);
}
return Distance(termOPList, N);
}
public List<Result> Distance(List<TermOP> termList, int N = 40)
{
long termCount = termList.Count;
for (int i = 0; i < termCount; i++)
{
if (model.GetTerm(termList[i].strTerm) == null)
{
//The term is OOV, no result
return null;
}
}
//Calculate input terms' vector
double[] vec = GetVector(termList);
int candidateWordCount = model.Vocabulary.Count;
//Calculate the distance betweens words in parallel
int size_per_block = candidateWordCount / BLOCK_N;
List<Result> rstList = new List<Result>();
Parallel.For<List<Result>>(0, BLOCK_N + 1, parallelOption, () => new List<Result>(), (k, loop, subtotal) =>
{
for (int c = (int)(k * size_per_block); c < (k + 1) * size_per_block && c < candidateWordCount; c++)
{
//Calculate the distance
double dist = 0;
for (int a = 0; a < model.VectorSize; a++)
{
dist += vec[a] * model.Vocabulary[c].vector[a];
}
//Save the result
Result rst = new Result();
rst.strTerm = model.Vocabulary[c].strTerm;
rst.score = dist;
subtotal.Add(rst);
}
return subtotal;
},
(subtotal) => // lock free accumulator
{
//Mereg the result from different threads
lock (locker)
{
rstList.AddRange(subtotal);
}
});
//Sort the result according the distance
rstList.Sort();
int maxN = Math.Min(N, rstList.Count);
return rstList.GetRange(0, maxN);
}
}
}