CMiniLexicon::FindMajorSignatures(): use log file routines
[linguistica.git] / MonteCarlo.cpp
blob89205ad55d1ee3ded8507daf7f273d1fa9ec1db9
1 // Monte Carlo algorithm for computing the denominator Z in a Boltzmann model
2 // Copyright © 2009 The University of Chicago
3 #include "MonteCarlo.h"
5 #include <cstdlib>
6 #include <Q3TextStream>
8 MonteCarlo::MonteCarlo(int size)
9 {
10 m_Size = size;
11 m_Values.resize(size);
12 m_ReadyForAction = false;
15 MonteCarlo::MonteCarlo(int size, QString MyFirstPhone)
17 m_Size = size;
18 m_Values.resize(size);
19 m_ReadyForAction = false;
20 m_MyFirstPhone = MyFirstPhone;
23 void MonteCarlo::Dump( Q3TextStream* out )
25 QString FirstPhone,
26 SecondPhone;
28 int Size;
30 MonteCarlo* pConditionalMonteCarlo;
33 *out << endl <<
34 "MonteCarlo dump. Size: " << m_Size;
35 if (m_ReadyForAction )
37 *out << " Ready for action (normalized). ";
39 else
41 *out << " Not ready for action (normalized). ";
43 if (m_ModelType == UNIGRAM) { *out << endl <<"Model type is Unigram.\n"; };
44 if (m_ModelType == BIGRAM) { *out << endl <<"Model type is Bigram.\n" ;};
47 *out << endl << "Top level links: ";
48 for (int i = 0 ; i < m_Size; i++)
50 *out << i << "\t" << m_Keys[i] << "\t" << m_Values[i];
54 if (m_ModelType == BIGRAM)
56 for (int i = 0; i < GetSize(); i++)
58 Size = GetSize();
60 pConditionalMonteCarlo = m_MyBigrams.find( m_MyFirstPhone );
61 *out << endl << "<<" << FirstPhone << ">>";
63 for (int j = 0; j < pConditionalMonteCarlo->GetSize();j++)
65 *out << "\n\t" << j << "\t" << m_Keys[j] << "\t" << m_Values[j];
78 QString MonteCarlo::ReturnCharacter( )
80 using std::rand;
82 int i= 0;
83 double random;
86 if (m_ReadyForAction == false) Normalize();
88 random = rand()/(double)0x7fff;
90 for ( i = 0; i < m_Size; i++)
92 //unused variable:
93 // double temp ( m_Values[i]) ;
94 if ( m_Values[i] > random ) break;
97 return m_Keys[i];
100 QString MonteCarlo::ReturnCharacter( QString Char )
102 using std::rand;
104 int i= 0;
105 double random;
107 MonteCarlo* pConditionalMonteCarlo;
108 pConditionalMonteCarlo = m_MyBigrams.find( Char );
110 random = rand()/(double)0x7fff;
112 for ( i = 0; i < m_Size; i++)
114 //unused variable:
115 // double temp ( pConditionalMonteCarlo->m_Values[i]) ;
116 if ( pConditionalMonteCarlo->m_Values[i] > random ) break;
119 return pConditionalMonteCarlo->m_Keys[i];
122 void MonteCarlo::Normalize()
124 double total = 0;
125 int i= 0;
126 for (i = 0; i <m_Size; i++)
128 total += m_Values[i];
131 if (total != 1.0)
133 for (i = 0; i <m_Size; i++)
135 m_Values[i] /= total;
137 for (i = 1; i <m_Size; i++)
139 m_Values[i] += m_Values[i-1];
142 //check to see if collection includes #; if not, do not say it's ready for action.
144 m_ReadyForAction = true;
148 CParse MonteCarlo::ReturnString(CParse& String)
150 QString Char, Char2;
151 QString temp;
152 String.ClearParse();
153 String.Append(QChar('#'));
155 Char = "#";
156 while (TRUE)
158 Char2 = ReturnCharacter ( Char );
159 if (Char2.length() == 0) continue;
160 String.Append( Char2);
161 temp = String.Display();
162 if (Char2 == "#") break;
163 Char = Char2;
166 QString temp2 = String.GetPiece(2).Display();
168 return String;