1 // Implementation of CPrefixCollection methods
2 // Copyright © 2009 The University of Chicago
3 #include "PrefixCollection.h"
5 #include <Q3TextStream>
10 #include "MiniLexicon.h"
13 CPrefixCollection::CPrefixCollection (CMiniLexicon
* Lex
)
16 if( m_pMiniLex
) m_pLexicon
= m_pMiniLex
->GetLexicon();
21 CPrefixCollection::CPrefixCollection ( )
28 CPrefixCollection::~CPrefixCollection()
34 for( int i
= 0; i
< GetCount(); i
++ )
38 m_pLexicon
->RemovePrefix( pPrefix
);
47 CPrefix
* CPrefixCollection::operator<< ( CStringSurrogate Prefix
)
51 pPrefix
= AddToCollection( Prefix
);
57 CPrefix
* CPrefixCollection::operator<< ( CParse
* Prefix
)
61 pPrefix
= AddToCollection (*Prefix
);
67 CPrefix
* CPrefixCollection::operator<< ( QString Prefix
)
71 CStringSurrogate
ssPrefix( Prefix
);
72 pPrefix
= AddToCollection( ssPrefix
);
78 void CPrefixCollection::AddPointer( CPrefix
* pPrefix
)
80 TCollection
<CPrefix
>::AddPointer( pPrefix
);
82 if( m_pLexicon
) m_pLexicon
->InsertPrefix( pPrefix
);
86 CPrefix
* CPrefixCollection::AddToCollection( CParse
& Prefix
)
88 CPrefix
* pPrefix
= TCollection
<CPrefix
>::AddToCollection( Prefix
);
90 if( m_pLexicon
) m_pLexicon
->InsertPrefix( pPrefix
);
96 CPrefix
* CPrefixCollection::AddToCollection( CStringSurrogate
& Prefix
)
98 CPrefix
* pPrefix
= TCollection
<CPrefix
>::AddToCollection( Prefix
);
100 if( m_pLexicon
) m_pLexicon
->InsertPrefix( pPrefix
);
106 void CPrefixCollection::Empty()
112 for( int i
= 0; i
< GetCount(); i
++ )
116 m_pLexicon
->RemovePrefix( pPrefix
);
120 TCollection
<CPrefix
>::Empty();
124 void CPrefixCollection::RemoveAll()
130 for( int i
= 0; i
< GetCount(); i
++ )
134 m_pLexicon
->RemovePrefix( pPrefix
);
138 TCollection
<CPrefix
>::RemoveAll();
142 bool CPrefixCollection::Remove( CPrefix
* pPrefix
)
144 if( m_pLexicon
) m_pLexicon
->RemovePrefix( pPrefix
);
146 return TCollection
<CPrefix
>::Remove( pPrefix
);
150 bool CPrefixCollection::RemoveMember( CPrefix
* pPrefix
)
152 if( m_pLexicon
) m_pLexicon
->RemovePrefix( pPrefix
);
154 return TCollection
<CPrefix
>::RemoveMember( pPrefix
);
158 bool CPrefixCollection::RemoveMember( CStringSurrogate
& Prefix
)
160 CPrefix
* pPrefix
= (CPrefix
*)Find1( Prefix
)->Get_T_Pointer();
162 if( m_pLexicon
) m_pLexicon
->RemovePrefix( pPrefix
);
164 return TCollection
<CPrefix
>::RemoveMember( Prefix
);
168 bool CPrefixCollection::RemoveMember( CStringSurrogate
& Prefix
, bool b
)
170 CPrefix
* pPrefix
= (CPrefix
*)Find1( Prefix
)->Get_T_Pointer();
172 if( m_pLexicon
) m_pLexicon
->RemovePrefix( pPrefix
);
174 return TCollection
<CPrefix
>::RemoveMember( Prefix
, b
);
178 void CPrefixCollection::DeleteMarkedMembers()
180 if ( m_DeletionArray
== NULL
) { return; }
182 int count
= GetCount();
183 for (int i
= 0; i
< count
; i
++)
185 if ( m_DeletionArray
[i
] == 1 )
187 if( m_pLexicon
) m_pLexicon
->RemovePrefix( m_PointerArray
[i
] );
191 TCollection
<CPrefix
>::DeleteMarkedMembers();
194 void CPrefixCollection::ListDisplay(
195 Q3ListView
* pView
, QMap
<QString
, QString
>* filter
,
196 linguistica::ui::status_user_agent
& status
)
198 pView
->setRootIsDecorated(false);
199 pView
->setSorting(1);
201 // Clean it out first.
202 while (pView
->columns() != 0)
203 pView
->removeColumn(0);
205 // Add Column headers
206 pView
->addColumn("Prefixes");
207 pView
->addColumn("Descr. Length");
208 // XXX. suffixes has "Length Of Ptr" column
209 pView
->addColumn("Corpus Count");
210 pView
->addColumn("Use Count");
211 pView
->addColumn("Stems");
213 // XXX. suffixes sets column alignments and widths
215 status
.major_operation
= "Creating prefix list for display";
216 status
.progress
.clear();
218 if (m_SortValidFlag
== false)
222 status
.progress
.set_denominator(GetCount());
223 for (int i
= 0; i
< (int) GetCount(); i
++) {
224 GetAtSort(i
)->ListDisplay(pView
, filter
, m_pLexicon
->GetNumberOfCharacterTypes());
227 status
.progress
.clear();
228 status
.major_operation
.clear();
231 void CPrefixCollection::OutputPrefixes( QString FileName
)
233 Q_ASSERT(!FileName
.isEmpty());
234 QFile
file( FileName
);
236 if( file
.open( QIODevice::WriteOnly
) )
238 Q3TextStream
outf( &file
);
239 outf
.setEncoding( Q3TextStream::Unicode
);
241 outf
<< "# Prefix Count" << endl
;
242 outf
<< "# ------------" << endl
;
243 outf
<< " " << GetCount() << endl
<< endl
;
245 outf
<< "# Prefix | Use Count | Corpus Count | Index | " << endl
;
246 outf
<< "# ------------------------------------------------- " << endl
;
249 for (int i
= 0; i
< (int)GetCount(); i
++)
251 GetAtSort(i
)->OutputPrefix( outf
);
259 void CPrefixCollection::ReadPrefixFile (QString FileName
)
261 Q_ASSERT(!FileName
.isEmpty());
263 QFile
file(FileName
);
271 if( file
.exists() && file
.open( QIODevice::ReadOnly
) )
273 Q3TextStream
inf(&file
);
274 inf
.setEncoding ( Q3TextStream::Locale
);
276 Buffer
= inf
.readLine();
277 Q_ASSERT( Buffer
[0] == '#' );
279 Buffer
= inf
.readLine();
280 Q_ASSERT( Buffer
[0] == '#' );
284 Buffer
= inf
.readLine(); // end of size line
285 Q_ASSERT( Buffer
.length() == 0 );
287 Buffer
= inf
.readLine(); // blank line
288 Q_ASSERT( Buffer
.length() == 0 );
290 Buffer
= inf
.readLine();
291 Q_ASSERT( Buffer
[0] == '#' );
293 Buffer
= inf
.readLine();
294 Q_ASSERT( Buffer
[0] == '#' );
296 while( !inf
.atEnd() && LineCount
< size
)
305 // Filter all sequences that should be
306 // analyzed as one character
307 Key
= Filter( m_pLexicon
->GetInFilter(), Key
);
309 pPrefix
= *this << CStringSurrogate( Key
);
310 pPrefix
->IncrementUseCount( UseCount
);
311 pPrefix
->IncrementCorpusCount( CorpusCount
);
319 double CPrefixCollection::GetDL_PhonologicalContent()
321 double affix_total_dl
= 0;
324 for( i
= 0; i
< (int)GetCount(); i
++ )
327 affix_total_dl
+= pAffix
->ComputeDL( m_pMiniLex
->GetNumberOfCharacterTypes() );
329 return affix_total_dl
;