librime: corrector.h Source File

| librime 1.2 Rime Input Method Engine, the core library

corrector.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// Created by nameoverflow on 2018/11/14.
6//
7
8#ifndef RIME_CORRECTOR_H
9#define RIME_CORRECTOR_H
10
11#include <rime/common.h>
12#include <rime/component.h>
13#include <rime/algo/algebra.h>
14#include <rime/algo/spelling.h>
15#include <rime/dict/prism.h>
16#include <rime/dict/vocabulary.h>
17
18namespace rime {
19struct Ticket;
20
21 class SymDeleteCollector {
22 public:
23 explicit SymDeleteCollector(const Syllabary& syllabary)
24 : syllabary_(syllabary) {}
25
26 Script Collect(size_t edit_distance);
27
28 private:
29 const Syllabary& syllabary_;
30};
31
32 namespace corrector {
33 using Distance = size_t;
34 struct Correction {
35 size_t distance;
36 SyllableId syllable;
37 size_t length;
38};
39 class Corrections : public hash_map<SyllableId, Correction> {
40 public:
44 inline void Alter(SyllableId syllable, Correction correction) {
45 if (find(syllable) == end() ||
46 correction.distance < (*this)[syllable].distance) {
47 (*this)[syllable] = correction;
48 }
49 };
50};
51} // namespace corrector
52
56 class Corrector : public Class<Corrector, const Ticket&> {
57 public:
58 virtual ~Corrector() = default;
59 RIME_DLL virtual void ToleranceSearch(const Prism& prism,
60 const string& key,
61 corrector::Corrections* results,
62 size_t tolerance) = 0;
63};
64
65 class CorrectorComponent : public Corrector::Component {
66 public:
67 CorrectorComponent();
68 ~CorrectorComponent() override = default;
69 Corrector* Create(const Ticket& ticket) noexcept override;
70
71 private:
72 template <class... Cs>
73 static Corrector* Combine(Cs... args);
74
75 the<ResourceResolver> resolver_;
76
77 class Unified : public Corrector {
78 public:
79 Unified() = default;
80 RIME_DLL void ToleranceSearch(const Prism& prism,
81 const string& key,
82 corrector::Corrections* results,
83 size_t tolerance) override;
84 template <class... Cs>
85 void Add(Cs... args) {
86 contents = {args...};
87 }
88
89 private:
90 vector<of<Corrector>> contents = {};
91 };
92};
93
94 class EditDistanceCorrector : public Corrector, public Prism {
95 public:
96 ~EditDistanceCorrector() override = default;
97 RIME_DLL explicit EditDistanceCorrector(const path& file_path);
98
99 RIME_DLL bool Build(const Syllabary& syllabary,
100 const Script* script = nullptr,
101 uint32_t dict_file_checksum = 0,
102 uint32_t schema_file_checksum = 0);
103
104 RIME_DLL void ToleranceSearch(const Prism& prism,
105 const string& key,
106 corrector::Corrections* results,
107 size_t tolerance) override;
108 corrector::Distance LevenshteinDistance(const std::string& s1,
109 const std::string& s2);
110 corrector::Distance RestrictedDistance(const std::string& s1,
111 const std::string& s2,
112 corrector::Distance threshold);
113};
114
115 class RIME_DLL NearSearchCorrector : public Corrector {
116 public:
117 NearSearchCorrector() = default;
118 ~NearSearchCorrector() override = default;
119 void ToleranceSearch(const Prism& prism,
120 const string& key,
121 corrector::Corrections* results,
122 size_t tolerance) override;
123};
124
125template <class... Cs>
126 Corrector* CorrectorComponent::Combine(Cs... args) {
127 auto u = new Unified();
128 u->Add(args...);
129 return u;
130}
131
132} // namespace rime
133
134#endif // RIME_CORRECTOR_H
algebra.h
spelling.h
common.h
component.h
prism.h
vocabulary.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::Syllabary
set< string > Syllabary
Definition vocabulary.h:17
rime::hash_map
boost::unordered_map< Key, T > hash_map
Definition common.h:53
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::SyllableId
int32_t SyllableId
Definition syllabifier.h:21
rime::corrector
Definition corrector.h:32
rime::corrector::Distance
size_t Distance
Definition corrector.h:33
rime::prism
Definition prism.h:20
rime::Script
Definition algebra.h:20
rime::path
Definition common.h:84
rime::Class
Definition component.h:21
rime::SymDeleteCollector::SymDeleteCollector
SymDeleteCollector(const Syllabary &syllabary)
Definition corrector.h:23
rime::SymDeleteCollector::Collect
Script Collect(size_t edit_distance)
Definition corrector.cc:73
rime::corrector::Correction
Definition corrector.h:34
rime::corrector::Correction::length
size_t length
Definition corrector.h:37
rime::corrector::Correction::distance
size_t distance
Definition corrector.h:35
rime::corrector::Correction::syllable
SyllableId syllable
Definition corrector.h:36
rime::corrector::Corrections
Definition corrector.h:39
rime::corrector::Corrections::Alter
void Alter(SyllableId syllable, Correction correction)
Definition corrector.h:44
rime::Corrector
Definition corrector.h:56
rime::Corrector::~Corrector
virtual ~Corrector()=default
rime::Corrector::ToleranceSearch
virtual RIME_DLL void ToleranceSearch(const Prism &prism, const string &key, corrector::Corrections *results, size_t tolerance)=0
rime::CorrectorComponent::~CorrectorComponent
~CorrectorComponent() override=default
rime::CorrectorComponent::Create
Corrector * Create(const Ticket &ticket) noexcept override
Definition corrector.cc:301
rime::CorrectorComponent::CorrectorComponent
CorrectorComponent()
Definition corrector.cc:297
rime::EditDistanceCorrector::EditDistanceCorrector
RIME_DLL EditDistanceCorrector(const path &file_path)
Definition corrector.cc:243
rime::EditDistanceCorrector::RestrictedDistance
corrector::Distance RestrictedDistance(const std::string &s1, const std::string &s2, corrector::Distance threshold)
Definition corrector.cc:193
rime::EditDistanceCorrector::ToleranceSearch
RIME_DLL void ToleranceSearch(const Prism &prism, const string &key, corrector::Corrections *results, size_t tolerance) override
Definition corrector.cc:100
rime::EditDistanceCorrector::LevenshteinDistance
corrector::Distance LevenshteinDistance(const std::string &s1, const std::string &s2)
Definition corrector.cc:163
rime::EditDistanceCorrector::Build
RIME_DLL bool Build(const Syllabary &syllabary, const Script *script=nullptr, uint32_t dict_file_checksum=0, uint32_t schema_file_checksum=0)
Definition corrector.cc:224
rime::EditDistanceCorrector::~EditDistanceCorrector
~EditDistanceCorrector() override=default
rime::NearSearchCorrector::NearSearchCorrector
NearSearchCorrector()=default
rime::NearSearchCorrector::~NearSearchCorrector
~NearSearchCorrector() override=default
rime::NearSearchCorrector::ToleranceSearch
void ToleranceSearch(const Prism &prism, const string &key, corrector::Corrections *results, size_t tolerance) override
Definition corrector.cc:246
rime::MappedFile::file_path
const path & file_path() const
Definition mapped_file.h:121
rime::Prism
Definition prism.h:67
rime::Prism::dict_file_checksum
uint32_t dict_file_checksum() const
Definition prism.cc:334
rime::Prism::schema_file_checksum
uint32_t schema_file_checksum() const
Definition prism.cc:338
rime::Prism::Prism
RIME_DLL Prism(const path &file_path)
Definition prism.cc:79
rime::Ticket
Definition ticket.h:17