1 // 2 // Copyright RIME Developers 3 // Distributed under the BSD License 4 // 5 // 2011-7-12 Zou xu <zouivex@gmail.com> 6 // 7 8 #ifndef RIME_SYLLABIFIER_H_ 9 #define RIME_SYLLABIFIER_H_ 10 11 #include <stdint.h> 12 #include <rime_api.h> 13 #include "spelling.h" 14 15 namespace rime { 16 17 class Prism; 18 class Corrector; 19 20 using SyllableId = int32_t; 21 22 struct EdgeProperties : SpellingProperties { EdgePropertiesEdgeProperties23 EdgeProperties(SpellingProperties sup): SpellingProperties(sup) {}; 24 EdgeProperties() = default; 25 bool is_correction = false; 26 }; 27 28 using SpellingMap = map<SyllableId, EdgeProperties>; 29 using VertexMap = map<size_t, SpellingType>; 30 using EndVertexMap = map<size_t, SpellingMap>; 31 using EdgeMap = map<size_t, EndVertexMap>; 32 33 using SpellingPropertiesList = vector<const EdgeProperties*>; 34 using SpellingIndex = map<SyllableId, SpellingPropertiesList>; 35 using SpellingIndices = map<size_t, SpellingIndex>; 36 37 struct SyllableGraph { 38 size_t input_length = 0; 39 size_t interpreted_length = 0; 40 VertexMap vertices; 41 EdgeMap edges; 42 SpellingIndices indices; 43 }; 44 45 class Syllabifier { 46 public: 47 Syllabifier() = default; 48 explicit Syllabifier(const string &delimiters, 49 bool enable_completion = false, 50 bool strict_spelling = false) delimiters_(delimiters)51 : delimiters_(delimiters), 52 enable_completion_(enable_completion), 53 strict_spelling_(strict_spelling) { 54 } 55 56 RIME_API int BuildSyllableGraph(const string &input, 57 Prism &prism, 58 SyllableGraph *graph); 59 RIME_API void EnableCorrection(Corrector* corrector); 60 61 protected: 62 void CheckOverlappedSpellings(SyllableGraph *graph, 63 size_t start, size_t end); 64 void Transpose(SyllableGraph* graph); 65 66 string delimiters_; 67 bool enable_completion_ = false; 68 bool strict_spelling_ = false; 69 Corrector* corrector_ = nullptr; 70 }; 71 72 } // namespace rime 73 74 #endif // RIME_SYLLABIFIER_H_ 75