-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagNode.cpp
More file actions
145 lines (115 loc) · 4.2 KB
/
TagNode.cpp
File metadata and controls
145 lines (115 loc) · 4.2 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
#include "TagNode.h"
#include <list>
#include <iostream>
using namespace std;
const string DUMMY_WORD = "<DUMMY_WORD>";
//const string DUMMY_CONTEXT = "<DUMMY_CONTEXT>";
const string DUMMY_TAG = "<DUMMY_TAG>"; // used for unknown words in baseline method
extern double g_addLogProbabilities(double value1, double value2);
TAGNODE::TAGNODE(string tagName,vector<string> sentence,int wordPos,int tagSpan,int tagID)
:
tagName(tagName),
tagID(tagID),
wordPos(wordPos),
tagSpan(tagSpan),
deleted(0),
best(0),
l_reach(0),
forwardBackwardProbability(0)
{
// store observedWords for this tag in this tagNode
for(int i=wordPos ; i<wordPos+tagSpan ; ++i){
if(i > -1){
observedWords.push_back(sentence[i]);
}else{
observedWords.push_back(DUMMY_WORD);
}
}
}
TAGNODE::~TAGNODE()
{}
void TAGNODE::setAllStatesTo(double value, int direction)
// initialize all states' forward or backward probabilities to value
{
for(list<STATENODE>::iterator li=associatedStates.begin() ; li!=associatedStates.end() ; ++li){
li->setProbability(value,direction);
}
}
void TAGNODE::setAllStatesReach()
{
for(list<STATENODE>::iterator li=associatedStates.begin() ; li!=associatedStates.end() ; ++li){
li->mark_r_reachable();
}
}
list<STATENODE>::iterator TAGNODE::addNewState(list<STATENODE>::iterator sourceState)
// add a state to the current tagNode, using source state for history data
{
// tag history:
vector<string> newHistory(sourceState->getHistory());
// add current tag to end
newHistory.push_back(tagName);
// remove oldest tag
newHistory.erase(newHistory.begin());
vector<string> newContextHistory(sourceState->getContextHistory());
// add current context to end
newContextHistory.push_back(contextName);
// remove oldest tag
newContextHistory.erase(newContextHistory.begin());
for(list<STATENODE>::iterator li=associatedStates.begin() ; li!=associatedStates.end() ; ++li){
// if state with same histories already exists
if((newHistory == li->getHistory()) ){
// return iterator to existing state
return(li);
}
}
STATENODE state(newHistory,newContextHistory,observedWords,tagName,contextName);
// otherwise, add new state and return iterator
associatedStates.push_front(state);
return(associatedStates.begin());
}
/*void TAGNODE::addDummyState(vector<string> history)
// add a statenode without doing the computations of the addState function
{
STATENODE newState(history,observedWords,tagName);
associatedStates.push_back(newState);
}*/
void TAGNODE::addDummyState(vector<string> history,vector<string> contextHistory)
// add a statenode without doing the computations of the addState function
{
STATENODE newState(history,contextHistory,observedWords,tagName,contextName);
associatedStates.push_back(newState);
}
void TAGNODE::computeForwardBackwardValue()
// Compute and combine fw/bw values of all states belonging to this tagNode.
// Since regular probabilities would have to be added (thus representing
// probability of reaching one of these states) the log probabilities have
// to be added using the function addLogProbabilities.
{
int r_reachable=1;
for(list<STATENODE>::iterator s=associatedStates.begin() ; s!=associatedStates.end() ; ++s){
if(s == associatedStates.begin()){
forwardBackwardProbability = s->getForwardBackwardValue();
} else {
forwardBackwardProbability = g_addLogProbabilities(forwardBackwardProbability, s->getForwardBackwardValue());
}
r_reachable=(r_reachable && s->r_reachable());
}
if (!r_reachable) {
markAsDeleted();
}
}
void TAGNODE::print()
{
string observedWord = *observedWords.begin();
for(vector<string>::iterator vi=observedWords.begin()+1 ; vi!=observedWords.end() ; ++vi){
observedWord += (" " + *vi);
}
cout << observedWord << " " << tagName << " "
<< wordPos << "-"
<< wordPos+tagSpan << " " << forwardBackwardProbability
<< " " << (deleted ? "deleted" : "")
<< " " << (l_reach ? "" : "not-reachable") << endl;
for(list<STATENODE>::iterator li=associatedStates.begin() ; li!=associatedStates.end() ; ++li){
li->print();
}
}