LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
viterbiUtils.h
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/***************************************************************************
7 * Copyright (C) 2004-2012 by CEA LIST *
8 * *
9 ***************************************************************************/
10
11#ifndef LIMA_LINGUISTICPROCESSING_POSTAGGING_VITERBIUTILS_H
12#define LIMA_LINGUISTICPROCESSING_POSTAGGING_VITERBIUTILS_H
13
16
17#include "frequencyCost.h"
18
19#include <fstream>
20#include <sstream>
21#include <stdexcept>
22#include <algorithm>
23
24namespace Lima
25{
26
27namespace LinguisticProcessing
28{
29
30namespace PosTagger
31{
32
33
34template<typename Cost, typename CostFactory>
36{
37public:
38
40 MediaId language,
41 const Common::PropertyCode::PropertyManager* microManager,
42 const CostFactory& costFactory,
43 const std::string& trigramsFile,
44 const std::string& bigramsFile);
45
47
48 template<typename PredData>
49 void apply(LinguisticCode micro,PredData& predData,const std::vector<PredData>& predPredData) const;
50
51 inline Cost getMinimumCost() const { return m_minimumCost; }
52
53 inline Cost getMaximumCost() const { return m_maximumCost; }
54
55private:
56
57 void readTrigramMatrixFile(const std::string& fileName,const CostFactory& costFactory);
58 void readBigramMatrixFile(const std::string& fileName,const CostFactory& costFactory);
59 void sortData();
60
61 MediaId m_language;
62 const Common::PropertyCode::PropertyManager* m_microManager;
63 Cost m_minimumCost;
64 Cost m_maximumCost;
65 Cost m_unigramCost;
66
67 struct CategCostPair
68 {
69 LinguisticCode m_cat;
70 Cost m_cost;
71 // lets us insert the struct in ordered containers
72 inline bool operator<(const CategCostPair& ccp) const { return m_cat<ccp.m_cat;}
73 };
74
75 struct GramsData
76 {
77 Cost m_default; // cost of the bigram, or unigram cost if not
78 std::vector<CategCostPair> m_grams; // this is the last part of the n-gram.
79 };
80
81 std::map<LinguisticCode, std::map<LinguisticCode, GramsData> > m_data;
82
83 // this is used to store the bigram frequencies. code + code -> bigram
84 std::map<LinguisticCode, std::map<LinguisticCode, float> > m_biFreq;
85
86};
87
89{
90public:
91 FrequencyCost getCost(bool isTrigram,bool isBigram,bool,float triFreq) const
92 {
93 if (isTrigram) { return FrequencyCost(1,0,0,triFreq,0); }
94 if (isBigram) { return FrequencyCost(0,1,0,0,triFreq); }
95 else { return FrequencyCost(0,0,1,0,0);}
96 }
97
99 { return FrequencyCost(0,0,0,0,0);}
100
102 { return FrequencyCost(0,0,999999,0,0); }
103
104};
105
106template <class CostType>
108{
109public:
110
112 uint64_t trigramCost,
113 uint64_t bigramCost,
114 uint64_t unigramCost) :
115 m_trigramcost(trigramCost),
116 m_bigramcost(bigramCost),
117 m_unigramcost(unigramCost)
118 {}
119
120 CostType getCost(bool isTrigram,bool isBigram,bool,float) const
121 {
122 if (isTrigram) { return m_trigramcost; }
123 if (isBigram) { return m_bigramcost; }
124 else { return m_unigramcost;}
125 }
126
127 CostType getMinimumCost() const
128 { return CostType(0);}
129
130 CostType getMaximumCost() const
131 { return CostType(std::numeric_limits<uint64_t>::max()); }
132
133private:
134 CostType m_trigramcost;
135 CostType m_bigramcost;
136 CostType m_unigramcost;
137
138};
139
140
141} // PosTagger
142} // LinguisticProcessing
143} // Lima
144
145#include "viterbiUtils.tcc"
146
147#endif
Provide tools to manage a specific property.
FrequencyCost getCost(bool isTrigram, bool isBigram, bool, float triFreq) const
CostType getCost(bool isTrigram, bool isBigram, bool, float) const
SimpleIntegerCostFactory(uint64_t trigramCost, uint64_t bigramCost, uint64_t unigramCost)
ViterbiCostFunction(MediaId language, const Common::PropertyCode::PropertyManager *microManager, const CostFactory &costFactory, const std::string &trigramsFile, const std::string &bigramsFile)
void apply(LinguisticCode micro, PredData &predData, const std::vector< PredData > &predPredData) const
NAUTITIA.
bool operator<(const LinguisticCode &lhs, const LinguisticCode &rhs)
Definition StdBitset.h:289