-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grammar): compare homophones/homographs in sentence
add inteface to grammar plugin; fall back to naive formula if missing "grammar" module
- Loading branch information
Showing
10 changed files
with
156 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef RIME_GRAMMAR_H_ | ||
#define RIME_GRAMMAR_H_ | ||
|
||
#include <rime/common.h> | ||
#include <rime/component.h> | ||
#include <rime/dict/vocabulary.h> | ||
|
||
namespace rime { | ||
|
||
class Config; | ||
|
||
class Grammar : public Class<Grammar, Config*> { | ||
public: | ||
virtual ~Grammar() {} | ||
virtual double Query(const string& context, | ||
const string& word, | ||
bool is_rear) = 0; | ||
|
||
inline static double Evaluate(const string& context, | ||
const DictEntry& entry, | ||
bool is_rear, | ||
Grammar* grammar) { | ||
const double kPenalty = -18.420680743952367; // log(1e-8) | ||
return entry.weight + | ||
(grammar ? grammar->Query(context, entry.text, is_rear) : kPenalty); | ||
} | ||
}; | ||
|
||
} // namespace rime | ||
|
||
#endif // RIME_GRAMMAR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,30 @@ | |
// | ||
// 2011-10-06 GONG Chen <[email protected]> | ||
// | ||
#include <rime/common.h> | ||
#include <rime/candidate.h> | ||
#include <rime/config.h> | ||
#include <rime/dict/vocabulary.h> | ||
#include <rime/gear/grammar.h> | ||
#include <rime/gear/poet.h> | ||
|
||
namespace rime { | ||
|
||
inline static Grammar* create_grammar(Config* config) { | ||
if (auto* grammar = Grammar::Require("grammar")) { | ||
return grammar->Create(config); | ||
} | ||
return nullptr; | ||
} | ||
|
||
Poet::Poet(const Language* language, Config* config) | ||
: language_(language), | ||
grammar_(create_grammar(config)) {} | ||
|
||
Poet::~Poet() {} | ||
|
||
an<Sentence> Poet::MakeSentence(const WordGraph& graph, | ||
size_t total_length) { | ||
const int kMaxHomophonesInMind = 1; | ||
size_t total_length) { | ||
// TODO: save more intermediate sentence candidates | ||
map<int, an<Sentence>> sentences; | ||
sentences[0] = New<Sentence>(language_); | ||
// dynamic programming | ||
|
@@ -30,15 +44,16 @@ an<Sentence> Poet::MakeSentence(const WordGraph& graph, | |
continue; // exclude single words from the result | ||
DLOG(INFO) << "end pos: " << end_pos; | ||
const DictEntryList& entries(x.second); | ||
for (size_t i = 0; i < kMaxHomophonesInMind && i < entries.size(); ++i) { | ||
for (size_t i = 0; i < entries.size(); ++i) { | ||
const auto& entry(entries[i]); | ||
auto new_sentence = New<Sentence>(*sentences[start_pos]); | ||
new_sentence->Extend(*entry, end_pos); | ||
bool is_rear = end_pos == total_length; | ||
new_sentence->Extend(*entry, end_pos, is_rear, grammar_.get()); | ||
if (sentences.find(end_pos) == sentences.end() || | ||
sentences[end_pos]->weight() < new_sentence->weight()) { | ||
DLOG(INFO) << "updated sentences " << end_pos << ") with '" | ||
<< new_sentence->text() << "', " << new_sentence->weight(); | ||
sentences[end_pos] = new_sentence; | ||
sentences[end_pos] = std::move(new_sentence); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.