Skip to content

Commit

Permalink
Merge branch 'universal-dictionary' from PR #156
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Oct 21, 2017
2 parents b51dda8 + 52ae306 commit d536c3f
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 293 deletions.
17 changes: 10 additions & 7 deletions src/rime/dict/db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <rime/common.h>
#include <rime/resource.h>
#include <rime/service.h>
#include <rime/dict/db.h>

Expand All @@ -20,15 +21,17 @@ bool DbAccessor::MatchesPrefix(const string& key) {

// Db members

static const ResourceType kDbResourceType = {
"db", "", ""
};

Db::Db(const string& name) : name_(name) {
boost::filesystem::path path(name);
if (path.has_parent_path()) {
file_name_ = name;
}
else {
boost::filesystem::path dir(Service::instance().deployer().user_data_dir);
file_name_ = (dir / path).string();
static ResourceResolver db_resource_resolver(kDbResourceType);
if (db_resource_resolver.root_path().empty()) {
db_resource_resolver.set_root_path(
Service::instance().deployer().user_data_dir);
}
file_name_ = db_resource_resolver.ResolvePath(name).string();
}

bool Db::Exists() const {
Expand Down
46 changes: 31 additions & 15 deletions src/rime/dict/dict_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
#include <fstream>
#include <boost/filesystem.hpp>
#include <rime/resource.h>
#include <rime/service.h>
#include <rime/algo/algebra.h>
#include <rime/algo/utilities.h>
#include <rime/dict/dictionary.h>
Expand All @@ -19,19 +21,25 @@

namespace rime {

DictCompiler::DictCompiler(Dictionary *dictionary, DictFileFinder finder)
DictCompiler::DictCompiler(Dictionary *dictionary)
: dict_name_(dictionary->name()),
prism_(dictionary->prism()),
table_(dictionary->table()),
dict_file_finder_(finder) {
table_(dictionary->table()) {
}

static string LocateFile(const string& file_name) {
the<ResourceResolver> resolver(
Service::instance().CreateResourceResolver({"", "", ""}));
return resolver->ResolvePath(file_name).string();
}

bool DictCompiler::Compile(const string &schema_file) {
LOG(INFO) << "compiling:";
bool build_table_from_source = true;
DictSettings settings;
string dict_file(FindDictFile(dict_name_));
if (dict_file.empty()) {
string dict_file = LocateFile(dict_name_ + ".dict.yaml");
if (!boost::filesystem::exists(dict_file)) {
LOG(ERROR) << "source file '" << dict_file << "' does not exist.";
build_table_from_source = false;
}
else {
Expand All @@ -49,9 +57,12 @@ bool DictCompiler::Compile(const string &schema_file) {
for(auto it = tables->begin(); it != tables->end(); ++it) {
if (!Is<ConfigValue>(*it))
continue;
string dict_file(FindDictFile(As<ConfigValue>(*it)->str()));
if (dict_file.empty())
string dict_name = As<ConfigValue>(*it)->str();
string dict_file = LocateFile(dict_name + ".dict.yaml");
if (!boost::filesystem::exists(dict_file)) {
LOG(ERROR) << "source file '" << dict_file << "' does not exist.";
return false;
}
dict_files.push_back(dict_file);
}
uint32_t dict_file_checksum = 0;
Expand Down Expand Up @@ -95,7 +106,9 @@ bool DictCompiler::Compile(const string &schema_file) {
<< " (" << dict_file_checksum << ")";
LOG(INFO) << schema_file << " (" << schema_file_checksum << ")";
{
ReverseDb reverse_db(dict_name_);
the<ResourceResolver> resolver(
Service::instance().CreateResourceResolver({"", "", ".reverse.bin"}));
ReverseDb reverse_db(resolver->ResolvePath(dict_name_).string());
if (!reverse_db.Exists() ||
!reverse_db.Load() ||
reverse_db.dict_file_checksum() != dict_file_checksum) {
Expand All @@ -117,18 +130,19 @@ bool DictCompiler::Compile(const string &schema_file) {
return true;
}

string DictCompiler::FindDictFile(const string& dict_name) {
string dict_file(dict_name + ".dict.yaml");
if (dict_file_finder_) {
dict_file = dict_file_finder_(dict_file);
}
return dict_file;
static string RelocateToUserDirectory(const string& file_name) {
ResourceResolver resolver(ResourceType{"", "", ""});
resolver.set_root_path(Service::instance().deployer().user_data_dir);
auto resource_id = boost::filesystem::path(file_name).filename().string();
return resolver.ResolvePath(resource_id).string();
}

bool DictCompiler::BuildTable(DictSettings* settings,
const vector<string>& dict_files,
uint32_t dict_file_checksum) {
LOG(INFO) << "building table...";
table_ = New<Table>(RelocateToUserDirectory(table_->file_name()));

EntryCollector collector;
collector.Configure(settings);
collector.Collect(dict_files);
Expand Down Expand Up @@ -172,7 +186,7 @@ bool DictCompiler::BuildTable(DictSettings* settings,
}
}
// build .reverse.bin
ReverseDb reverse_db(dict_name_);
ReverseDb reverse_db(RelocateToUserDirectory(dict_name_ + ".reverse.bin"));
if (!reverse_db.Build(settings,
collector.syllabary,
vocabulary,
Expand All @@ -187,6 +201,8 @@ bool DictCompiler::BuildTable(DictSettings* settings,
bool DictCompiler::BuildPrism(const string &schema_file,
uint32_t dict_file_checksum, uint32_t schema_file_checksum) {
LOG(INFO) << "building prism...";
prism_ = New<Prism>(RelocateToUserDirectory(prism_->file_name()));

// get syllabary from table
Syllabary syllabary;
if (!table_->Load() || !table_->GetSyllabary(&syllabary) || syllabary.empty())
Expand Down
9 changes: 1 addition & 8 deletions src/rime/dict/dict_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class Table;
class ReverseDb;
class DictSettings;

// return found dict file path, otherwize return empty string
using DictFileFinder =
function<const string (const string& file_name)>;

class DictCompiler {
public:
enum Options {
Expand All @@ -31,14 +27,12 @@ class DictCompiler {
kDump = 4,
};

RIME_API DictCompiler(Dictionary *dictionary,
DictFileFinder finder = NULL);
RIME_API DictCompiler(Dictionary *dictionary);

RIME_API bool Compile(const string &schema_file);
void set_options(int options) { options_ = options; }

private:
string FindDictFile(const string& dict_name);
bool BuildTable(DictSettings* settings,
const vector<string>& dict_files,
uint32_t dict_file_checksum);
Expand All @@ -50,7 +44,6 @@ class DictCompiler {
an<Prism> prism_;
an<Table> table_;
int options_ = 0;
DictFileFinder dict_file_finder_;
};

} // namespace rime
Expand Down
26 changes: 21 additions & 5 deletions src/rime/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <utility>
#include <boost/filesystem.hpp>
#include <rime/common.h>
#include <rime/resource.h>
#include <rime/schema.h>
#include <rime/service.h>
#include <rime/ticket.h>
Expand Down Expand Up @@ -279,7 +280,22 @@ bool Dictionary::loaded() const {

// DictionaryComponent members

DictionaryComponent::DictionaryComponent() {
static const ResourceType kPrismResourceType = {
"prism", "", ".prism.bin"
};

static const ResourceType kTableResourceType = {
"table", "", ".table.bin"
};

DictionaryComponent::DictionaryComponent()
: prism_resource_resolver_(
Service::instance().CreateResourceResolver(kPrismResourceType)),
table_resource_resolver_(
Service::instance().CreateResourceResolver(kTableResourceType)) {
}

DictionaryComponent::~DictionaryComponent() {
}

Dictionary* DictionaryComponent::Create(const Ticket& ticket) {
Expand Down Expand Up @@ -308,13 +324,13 @@ DictionaryComponent::CreateDictionaryWithName(const string& dict_name,
boost::filesystem::path path(Service::instance().deployer().user_data_dir);
auto table = table_map_[dict_name].lock();
if (!table) {
table = New<Table>((path / dict_name).string() + ".table.bin");
table_map_[dict_name] = table;
auto file_path = table_resource_resolver_->ResolvePath(dict_name).string();
table_map_[dict_name] = table = New<Table>(file_path);
}
auto prism = prism_map_[prism_name].lock();
if (!prism) {
prism = New<Prism>((path / prism_name).string() + ".prism.bin");
prism_map_[prism_name] = prism;
auto file_path = prism_resource_resolver_->ResolvePath(prism_name).string();
prism_map_[prism_name] = prism = New<Prism>(file_path);
}
return new Dictionary(dict_name, table, prism);
}
Expand Down
5 changes: 5 additions & 0 deletions src/rime/dict/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,21 @@ class Dictionary : public Class<Dictionary, const Ticket&> {
an<Prism> prism_;
};

class ResourceResolver;

class DictionaryComponent : public Dictionary::Component {
public:
DictionaryComponent();
~DictionaryComponent();
Dictionary* Create(const Ticket& ticket);
Dictionary* CreateDictionaryWithName(const string& dict_name,
const string& prism_name);

private:
map<string, weak<Prism>> prism_map_;
map<string, weak<Table>> table_map_;
the<ResourceResolver> prism_resource_resolver_;
the<ResourceResolver> table_resource_resolver_;
};

} // namespace rime
Expand Down
4 changes: 0 additions & 4 deletions src/rime/dict/mapped_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ class MappedFile : boost::noncopyable {

// member function definitions

# if defined(__arm__)
# define RIME_ALIGNED(size, T) ((size + alignof(T) - 1) & ~(alignof(T) - 1))
# else
# define RIME_ALIGNED(size, T) (size)
# endif

template <class T>
T* MappedFile::Allocate(size_t count) {
Expand Down
20 changes: 11 additions & 9 deletions src/rime/dict/preset_vocabulary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <utf8.h>
#include <rime/resource.h>
#include <rime/service.h>
#include <rime/dict/preset_vocabulary.h>
#include <rime/dict/text_db.h>

namespace rime {

static const ResourceType kVocabularyResourceType = {
"vocabulary", "", ".txt"
};

static const string kDefaultVocabulary = "essay";

struct VocabularyDb : public TextDb {
explicit VocabularyDb(const string& path);
an<DbAccessor> cursor;
static const TextFormat format;
};

VocabularyDb::VocabularyDb(const string& path)
: TextDb(path, "vocabulary", VocabularyDb::format) {
: TextDb(path, kVocabularyResourceType.name, VocabularyDb::format) {
}

static bool rime_vocabulary_entry_parser(const Tsv& row,
Expand Down Expand Up @@ -50,14 +57,9 @@ const TextFormat VocabularyDb::format = {
};

string PresetVocabulary::DictFilePath() {
auto& deployer(Service::instance().deployer());
boost::filesystem::path path(deployer.user_data_dir);
path /= "essay.txt";
if (!boost::filesystem::exists(path)) {
path = deployer.shared_data_dir;
path /= "essay.txt";
}
return path.string();
the<ResourceResolver> resource_resolver(
Service::instance().CreateResourceResolver(kVocabularyResourceType));
return resource_resolver->ResolvePath(kDefaultVocabulary).string();
}

PresetVocabulary::PresetVocabulary() {
Expand Down
2 changes: 1 addition & 1 deletion src/rime/dict/prism.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct node_t {

} // namespace

const char kPrismFormat[] = "Rime::Prism/1.0";
const char kPrismFormat[] = "Rime::Prism/2.0";

const char kPrismFormatPrefix[] = "Rime::Prism/";
const size_t kPrismFormatPrefixLen = sizeof(kPrismFormatPrefix) - 1;
Expand Down
4 changes: 3 additions & 1 deletion src/rime/dict/prism.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ namespace rime {

namespace prism {

using Credibility = float;

struct SpellingDescriptor {
SyllableId syllable_id;
int32_t type;
double credibility;
Credibility credibility;
String tips;
};

Expand Down
27 changes: 13 additions & 14 deletions src/rime/dict/reverse_lookup_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/resource.h>
#include <rime/schema.h>
#include <rime/service.h>
#include <rime/ticket.h>
Expand All @@ -17,20 +18,15 @@

namespace rime {

const char kReverseFormat[] = "Rime::Reverse/1.0";
const char kReverseFormat[] = "Rime::Reverse/2.0";

const char kReverseFormatPrefix[] = "Rime::Reverse/";
const size_t kReverseFormatPrefixLen = sizeof(kReverseFormatPrefix) - 1;

static const char* kStemKeySuffix = "\x1fstem";

static string reverse_db_file_name(const string& dict_name) {
boost::filesystem::path dir(Service::instance().deployer().user_data_dir);
return (dir / dict_name).string() + ".reverse.bin";
}

ReverseDb::ReverseDb(const string& dict_name)
: MappedFile(reverse_db_file_name(dict_name)) {
ReverseDb::ReverseDb(const string& file_name)
: MappedFile(file_name) {
}

bool ReverseDb::Load() {
Expand Down Expand Up @@ -200,10 +196,6 @@ ReverseLookupDictionary::ReverseLookupDictionary(an<ReverseDb> db)
: db_(db) {
}

ReverseLookupDictionary::ReverseLookupDictionary(const string& dict_name)
: db_(new ReverseDb(dict_name)) {
}

bool ReverseLookupDictionary::Load() {
return db_ && (db_->IsOpen() || db_->Load());
}
Expand Down Expand Up @@ -233,7 +225,13 @@ an<DictSettings> ReverseLookupDictionary::GetDictSettings() {
return settings;
}

ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent() {
static const ResourceType kReverseDbResourceType = {
"reverse_db", "", ".reverse.bin"
};

ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent()
: resource_resolver_(
Service::instance().CreateResourceResolver(kReverseDbResourceType)) {
}

ReverseLookupDictionary*
Expand All @@ -248,7 +246,8 @@ ReverseLookupDictionaryComponent::Create(const Ticket& ticket) {
}
auto db = db_pool_[dict_name].lock();
if (!db) {
db = New<ReverseDb>(dict_name);
auto file_path = resource_resolver_->ResolvePath(dict_name).string();
db = New<ReverseDb>(file_path);
db_pool_[dict_name] = db;
}
return new ReverseLookupDictionary(db);
Expand Down
Loading

0 comments on commit d536c3f

Please sign in to comment.