-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vocab.cpp
68 lines (62 loc) · 1 KB
/
Vocab.cpp
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
#include "Vocab.h"
Vocab::Vocab()
{
_dic.clear();
/*add(unk);
add(sent_begin);
add(sent_end);*/
}
int
Vocab::
add(const string& wrd){
if(_dic.find(wrd)!=_dic.end())
return _dic[wrd];
int ind=(int)_dic.size();
_dic[wrd]=(int)ind;
_int2str.push_back(wrd);
return ind;
}
int
Vocab::
lookup(const string& wrd, bool forced)
{
if(forced)
return add(wrd);
else if(_dic.find(wrd)==_dic.end())
return _dic[unk];
else return _dic[wrd];
}
void
Vocab::
remove(int id)
{
if(id>=_int2str.size())
return;
string str=_int2str[id];
//_int2str.erase(_int2str.begin()+id);
_dic.erase(_dic.find(str));
}
int
Vocab::
lookup(string* pWrd, vector<int>& context, int clen, bool forced)
{
for(;clen>0;clen--,pWrd++)
{
context.push_back(lookup(*pWrd,forced));
}
return (int)context.size();
}
void
Vocab::
reverse(vector<int>& context)
{
reverse(&context[0],(int)context.size());
}
void
Vocab::
reverse(int* context, int len)
{
int hlen=len/2;
for(int i=0;i<hlen;i++)
swap(context[i],context[len-1-i]);
}