DictionariesRepository.cpp
5.22 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
/*
* File: DictionariesRepository.cpp
* Author: lennyn
*
* Created on August 8, 2014, 3:49 PM
*/
#include <fstream>
#include "DictionariesRepository.hpp"
#include "data/default_fsa.hpp"
#include "const.hpp"
#include "utils.hpp"
namespace morfeusz {
using namespace std;
DictionariesRepository DictionariesRepository::instance;
list<string>& DictionariesRepository::dictionarySearchPaths = Morfeusz::dictionarySearchPaths;
string DictionariesRepository::getDictionaryFilename(const std::string& name, MorfeuszProcessorType processorType) {
string processorTypeSuffix;
switch (processorType) {
case ANALYZER:
processorTypeSuffix = "-a";
break;
case GENERATOR:
processorTypeSuffix = "-s";
break;
default:
throw MorfeuszException("Internal error: invalid Morfeusz processor type");
}
string extensionSuffix = ".dict";
return name + processorTypeSuffix + extensionSuffix;
}
map<std::string, DictionariesRepository::RepositoryEntry> DictionariesRepository::getDefaultEntriesMap() {
map<std::string, DictionariesRepository::RepositoryEntry> res;
DictionariesRepository::RepositoryEntry defaultEntry;
defaultEntry.analyzerDictionary = new Dictionary(DEFAULT_FSA, ANALYZER);
defaultEntry.generatorDictionary = new Dictionary(DEFAULT_SYNTH_FSA, GENERATOR);
res[string()] = defaultEntry;
return res;
}
DictionariesRepository::DictionariesRepository()
: entriesMap(getDefaultEntriesMap()) {
}
const Dictionary* DictionariesRepository::getDefaultDictionary(MorfeuszProcessorType processorType) {
static string emptyString;
return this->getDictionary(emptyString, processorType);
}
Dictionary* DictionariesRepository::RepositoryEntry::getDictionary(MorfeuszProcessorType processorType) const {
switch (processorType) {
case ANALYZER:
return analyzerDictionary;
case GENERATOR:
return generatorDictionary;
default:
throw MorfeuszException("Internal error: invalid Morfeusz processor type");
}
}
static void raiseException(const string& name, MorfeuszProcessorType processorType) {
string processorTypeStr;
switch (processorType) {
case ANALYZER:
processorTypeStr = "analyzer";
break;
case GENERATOR:
processorTypeStr = "generator";
break;
default:
throw MorfeuszException("Internal error: invalid Morfeusz processor type");
}
throw MorfeuszException("Failed to load "+processorTypeStr+" dictionary \""+name+"\"");
}
const Dictionary* DictionariesRepository::getDictionary(const string& name, MorfeuszProcessorType processorType) {
if (!hasLoadedDictionary(name, processorType) && !tryToLoadDictionary(name, processorType)) {
raiseException(name, processorType);
}
return entriesMap.find(name)->second.getDictionary(processorType);
}
bool DictionariesRepository::hasLoadedDictionary(const string& name, MorfeuszProcessorType processorType) const {
if (entriesMap.find(name) == entriesMap.end()) {
return false;
} else {
const DictionariesRepository::RepositoryEntry& entry = entriesMap.find(name)->second;
return entry.getDictionary(processorType) != NULL;
}
}
static bool fileExists(const string& fileName) {
ifstream infile(fileName.c_str());
return infile.good();
}
bool DictionariesRepository::tryToLoadDictionary(const string& name, MorfeuszProcessorType processorType) {
list<string>::const_iterator it = dictionarySearchPaths.begin();
while (it != dictionarySearchPaths.end()) {
string dirpath = *it;
string filepath = dirpath + FILESYSTEM_PATH_SEPARATOR + getDictionaryFilename(name, processorType);
if (fileExists(filepath)) {
doLoadDictionary(name, filepath, processorType);
return true;
}
++it;
}
return false;
}
void DictionariesRepository::doLoadDictionary(const std::string& dictName, const std::string& filepath, MorfeuszProcessorType processorType) {
const unsigned char* ptr(readFile<unsigned char>(filepath.c_str()));
if (entriesMap.find(dictName) == entriesMap.end()) {
DictionariesRepository::RepositoryEntry entry;
entry.analyzerDictionary = NULL;
entry.generatorDictionary = NULL;
entriesMap[dictName] = entry;
}
DictionariesRepository::RepositoryEntry& entry = entriesMap.find(dictName)->second;
Dictionary* dict = new Dictionary(ptr, processorType);
switch (processorType) {
case ANALYZER:
entry.analyzerDictionary = dict;
break;
case GENERATOR:
entry.generatorDictionary = dict;
break;
default:
throw MorfeuszException("Internal error: invalid Morfeusz processor type");
}
}
}