-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.h
More file actions
99 lines (88 loc) · 3.46 KB
/
dictionary.h
File metadata and controls
99 lines (88 loc) · 3.46 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
// Copyright 2012 Florian Petran
#ifndef DICTIONARY_H_
#define DICTIONARY_H_
#include<string>
#include<map>
#include<utility>
#include<list>
#include<algorithm>
#include"text.h"
#include"params.h"
namespace Align {
class Dictionary;
/// Construct Dictionary objects.
/** Reads the dictionary index file and the Dictionary objects
* from their files.
* Holds the index to Dictionary objects already created, and
* will create a new one if it is requested. Constructs the
* Text objects as well. Owns all Text pointers.
**/
class DictionaryFactory {
public:
static DictionaryFactory& get_instance();
/// read a dictionary from the file if its first requested,
/// otherwise just return the pointer to the dictionary entry
const Dictionary* get_dictionary(const std::string&,
const std::string&);
/// create a new text with the given file name on first request,
/// or just return the text pointer otherwise
Text* get_text(const std::string&);
~DictionaryFactory();
private:
std::string locate_dictionary_file(const std::string&,
const std::string&);
std::string index_filename;
typedef std::pair<std::string, std::string> textpair;
/// an index mapping the text pair to their Dictionary objects
std::map<textpair, Dictionary*> dictionaries;
/// an index mapping the filenames to the Text objects
std::map<std::string, Text*> texts;
DictionaryFactory();
};
/// A directional bi-lingual/-textual dictionary
/** Represents a Dictionary, that is specific to two texts. Note
* that the Dictionary is directional - Dictionary( e, f ) is not
* the same as Dictionary( f, e )
*
* lookup word translations
* build the Text class
*
* The filename for the dictionary is first determined using an
* index file, then that file is opened and a word list constructed
* from it for each f and e texts. Also note that the Text objects
* need to be pointers since they are mutually dependent on Dictionary.
*
* The actual translation dictionary is stored as a map of vectors. Keys
* of that map are the e Words, and the vector value stores all f Words.
**/
class Dictionary : private std::map<WordType, std::list<WordType> > {
friend class DictionaryFactory;
public:
const Dictionary& operator=(const Dictionary&) = delete;
Dictionary(const Dictionary&) = delete;
/// look up a WordToken, and get a list to its translations
/// translations may be multiple types which might in turn
/// have multiple locations in the target file
const std::list<WordType>& lookup(const WordToken&) const;
/// check if the dictionary has an entry for word
bool has(const WordToken&) const;
/// return the source text for this dictionary
inline const Text* get_e() const { return _e; }
/// return the target text for this dictionary
inline const Text* get_f() const { return _f; }
protected:
explicit Dictionary(const std::string&);
Dictionary();
inline void set_texts(Text* e, Text* f) {
_e = e;
_f = f;
};
void open(const std::string&);
void read(std::ifstream*);
private:
Text *_e, *_f;
/// an empty dictionary entry
const std::list<WordType> empty_entry;
};
} // namespace Align
#endif // DICTIONARY_H_