Skip to content

Commit

Permalink
add rime_table_decompiler.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Sep 4, 2023
1 parent 2c5a0a3 commit 7cd8566
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/rime/dict/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const int kTableFormatLowestCompatible = 4.0;
const char kTableFormatPrefix[] = "Rime::Table/";
const size_t kTableFormatPrefixLen = sizeof(kTableFormatPrefix) - 1;


TableAccessor::TableAccessor(const Code& index_code,
const List<table::Entry>* list,
double credibility)
Expand Down
13 changes: 13 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ target_link_libraries(rime_deployer
${rime_dict_library}
${rime_levers_library})

set(rime_table_decompiler_src
"rime_table_decompiler.cc"
${CMAKE_SOURCE_DIR}/src/rime/dict/table.cc
${CMAKE_SOURCE_DIR}/src/rime/dict/mapped_file.cc
${CMAKE_SOURCE_DIR}/src/rime/dict/string_table.cc
${CMAKE_SOURCE_DIR}/src/rime/dict/vocabulary.cc
)
add_executable(rime_table_decompiler ${rime_table_decompiler_src})
target_link_libraries(rime_table_decompiler
${rime_library}
${rime_dict_library})

install(TARGETS rime_deployer DESTINATION ${BIN_INSTALL_DIR})
install(TARGETS rime_dict_manager DESTINATION ${BIN_INSTALL_DIR})
install(TARGETS rime_table_decompiler DESTINATION ${BIN_INSTALL_DIR})

install(TARGETS rime_patch DESTINATION ${BIN_INSTALL_DIR})

Expand Down
105 changes: 105 additions & 0 deletions tools/rime_table_decompiler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// rime_table_decompiler.cc
// nopdan <[email protected]>
//
#include <cmath>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <rime/dict/table.h>

using namespace std;
ofstream fout;

void outCode(rime::Table* table, const rime::Code code, ofstream& fout) {
if (code.empty()) {
return;
}
auto item = code.begin();
fout << table->GetSyllableById(*item);
item++;
for (; item != code.end(); ++item) {
fout << " ";
fout << table->GetSyllableById(*item);
}
return;
}

void access(rime::Table* table, rime::TableAccessor accessor) {
while (!accessor.exhausted()) {
auto word = table->GetEntryText(*accessor.entry());
fout << word << "\t";
outCode(table, accessor.code(), fout);

auto weight = accessor.entry()->weight;
if (weight >= 0) {
fout << "\t" << exp(weight);
}
fout << endl;
accessor.Next();
}
}

// 递归遍历
void recursion(rime::Table* table, ofstream& fout, rime::TableQuery* query) {
for (int i = 0; i < table->metadata()->num_syllables; i++) {
auto accessor = query->Access(i);
access(table, accessor);
if (query->Advance(i)) {
if (query->level() < 3) {
recursion(table, fout, query);
} else {
auto accessor = query->Access(0);
access(table, accessor);
}
query->Backdate();
}
}
}

void traversal(rime::Table* table, ofstream& fout) {
auto metadata = table->metadata();
cout << "num_syllables: " << metadata->num_syllables << endl;
cout << "num_entries: " << metadata->num_entries << endl;

fout << fixed;
fout << setprecision(0);
rime::TableQuery query(table->metadata()->index.get());
recursion(table, fout, &query);
}

int main(int argc, char* argv[]) {
string fileName(argv[1]);

cout << "Read File: " << fileName << endl;
rime::Table table(fileName);
table.Load();

// Remove directory if present.
// Do this before extension removal incase directory has a period character.
const size_t last_slash_idx = fileName.find_last_of("\\/");
if (std::string::npos != last_slash_idx) {
fileName.erase(0, last_slash_idx + 1);
}

// Remove extension if present.
const size_t period_idx = fileName.find('.');
if (std::string::npos != period_idx) {
fileName.erase(period_idx);
}

string outputName = fileName + ".txt";
fout.open(outputName);
// clang-format off
fout << "# Rime dictionary\n\n";
fout << "---\n"
"name: " << fileName << "\n"
"version: \"1.0\"\n"
"...\n\n";
// clang-format on
traversal(&table, fout);
cout << "Save To: " << outputName << endl
<< endl;
fout.close();
return 0;
}

0 comments on commit 7cd8566

Please sign in to comment.