LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
frequencyCost.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include "frequencyCost.h"
7
8namespace Lima
9{
10namespace LinguisticProcessing
11{
12namespace PosTagger
13{
14
16 m_nbtrigrams(0),
17 m_nbbigrams(0),
18 m_nbunigrams(0),
19 m_trigramproba(0),
20 m_bigramproba(0)
21{}
22
24 uint64_t nbtrigrams,
25 uint64_t nbbigrams,
26 uint64_t nbunigrams,
27 float trigramproba,
28 float bigramproba) :
29 m_nbtrigrams(nbtrigrams),
30 m_nbbigrams(nbbigrams),
31 m_nbunigrams(nbunigrams),
32 m_trigramproba(trigramproba),
33 m_bigramproba(bigramproba)
34{}
35
36/*
37FrequencyCost::FrequencyCost(const FrequencyCost& fc) :
38 m_nbtrigrams(fc.m_nbtrigrams),
39 m_nbbigrams(fc.m_nbbigrams),
40 m_nbunigrams(fc.m_nbunigrams),
41 m_trigramproba(fc.m_trigramproba),
42 m_bigramproba(fc.m_bigramproba)
43{}
44*/
45
47
49{
50 if (m_nbunigrams!=fc.m_nbunigrams) return m_nbunigrams<fc.m_nbunigrams;
51 if (m_nbbigrams!=fc.m_nbbigrams) return m_nbbigrams<fc.m_nbbigrams;
52 if (m_bigramproba!=fc.m_bigramproba) return m_bigramproba>fc.m_bigramproba;
53 if (fc.m_nbtrigrams==0) return false;
54 if (m_nbtrigrams==0) return true;
55 return ((m_trigramproba/m_nbtrigrams)>(fc.m_trigramproba/fc.m_nbtrigrams));
56}
57
59{
60 return ((m_trigramproba==fc.m_trigramproba) &&
61 (m_bigramproba==fc.m_bigramproba) &&
62 (m_nbtrigrams==fc.m_nbtrigrams) &&
63 (m_nbbigrams==fc.m_nbbigrams) &&
64 (m_nbunigrams==fc.m_nbunigrams));
65}
66
67std::ostream& operator<<(std::ostream& out,const FrequencyCost& cost)
68{
69 out << "freqcost{" << cost.m_nbtrigrams << "," << cost.m_nbbigrams << "," << cost.m_nbunigrams << ";" << cost.m_trigramproba << ";" << cost.m_bigramproba << "}";
70 return out;
71}
72
73
74FrequencyCost FrequencyCostFunction::m_minimumCost(0,0,0,0,0);
75FrequencyCost FrequencyCostFunction::m_maximumCost(0,0,999999,0,0);
76
77
79{
80 return FrequencyCost(
81 m_nbtrigrams+fc.m_nbtrigrams,
82 m_nbbigrams+fc.m_nbbigrams,
83 m_nbunigrams+fc.m_nbunigrams,
84 m_trigramproba+fc.m_trigramproba,
85 m_bigramproba+fc.m_bigramproba);
86}
87
89{
90 m_nbtrigrams+=fc.m_nbtrigrams;
91 m_nbbigrams+=fc.m_nbbigrams;
92 m_nbunigrams+=fc.m_nbunigrams;
93 m_trigramproba+=fc.m_trigramproba;
94 m_bigramproba+=fc.m_bigramproba;
95 return *this;
96}
97
98
99
101 const TrigramMatrix* trigrams,
102 const BigramMatrix* bigrams) :
103 m_trigrams(trigrams),
104 m_bigrams(bigrams)
105{}
106
108 const FrequencyCostFunction& cf) :
109 m_trigrams(cf.m_trigrams),
110 m_bigrams(cf.m_bigrams)
111{}
112
114
116{
117 float f=m_trigrams->freq(cat1,cat2,cat3);
118 if (f>0)
119 {
120 float b=m_bigrams->freq(cat1,cat2);
121 if (b>0)
122 {
123 return FrequencyCost(1,0,0,f/b,0);
124 }
125 else
126 {
127 // should not happen
128 return FrequencyCost(1,0,0,f,0);
129 }
130 }
131 else
132 {
133 f=m_bigrams->freq(cat2,cat3);
134 if (f>0)
135 {
136 return FrequencyCost(0,1,0,0,f);
137 }
138 else
139 {
140 return FrequencyCost(0,0,1,0,0);
141 }
142 }
143}
144
145} // PosTagger
146} // LinguisticProcessing
147}// Lima
float freq(LinguisticCode cat1, LinguisticCode cat2) const
FrequencyCost operator()(LinguisticCode cat1, LinguisticCode cat2, LinguisticCode cat3) const
FrequencyCostFunction(const TrigramMatrix *trigrams, const BigramMatrix *bigrams)
bool operator==(const FrequencyCost &fc) const
bool operator<(const FrequencyCost &fc) const
FrequencyCost & operator+=(const FrequencyCost &fc)
FrequencyCost operator+(const FrequencyCost &fc) const
float freq(LinguisticCode cat1, LinguisticCode cat2, LinguisticCode cat3) const
std::ostream & operator<<(std::ostream &out, const FrequencyCost &cost)
NAUTITIA.