| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2015-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <fstream> |
| 20 | |
| 21 | #include "src/CmdLineOutput.hpp" |
| 22 | #include "src/PhraseExtract.hpp" |
| 23 | |
| 24 | using opencc::Exception; |
| 25 | using opencc::PhraseExtract; |
| 26 | using opencc::UTF8StringSlice; |
| 27 | |
| 28 | void Extract(const std::vector<std::string>& inputFiles, |
| 29 | const std::string& outputFile) { |
| 30 | std::ostringstream buffer; |
| 31 | for (const auto& inputFile : inputFiles) { |
| 32 | std::ifstream ifs(inputFile); |
| 33 | const std::string contents((std::istreambuf_iterator<char>(ifs)), |
| 34 | (std::istreambuf_iterator<char>())); |
| 35 | buffer << contents; |
| 36 | } |
| 37 | const std::string& text = buffer.str(); |
| 38 | PhraseExtract extractor; |
| 39 | extractor.SetWordMaxLength(2); |
| 40 | extractor.SetPrefixSetLength(1); |
| 41 | extractor.SetSuffixSetLength(1); |
| 42 | extractor.Extract(text); |
| 43 | std::ofstream ofs(outputFile); |
| 44 | for (const auto& word : extractor.Words()) { |
| 45 | const PhraseExtract::Signals& signals = extractor.Signal(word); |
| 46 | const double entropy = signals.prefixEntropy + signals.suffixEntropy; |
| 47 | const double logProbablity = extractor.LogProbability(word); |
| 48 | ofs << word << " " << signals.frequency << " " << logProbablity << " " |
| 49 | << signals.cohesion << " " << entropy << " " << signals.prefixEntropy |
| 50 | << " " << signals.suffixEntropy << std::endl; |
| 51 | } |
| 52 | ofs.close(); |
| 53 | } |
| 54 | |
| 55 | int main(int argc, const char* argv[]) { |
| 56 | try { |
| 57 | TCLAP::CmdLine cmd("Open Chinese Convert (OpenCC) Phrase Extractor", ' ', |
| 58 | OPENCC_VERSION); |
| 59 | CmdLineOutput cmdLineOutput; |
| 60 | cmd.setOutput(&cmdLineOutput); |
| 61 | TCLAP::UnlabeledMultiArg<std::string> fileNames( |
| 62 | "fileName", "Input files", true /* required */, "files"); |
| 63 | cmd.add(fileNames); |
| 64 | TCLAP::ValueArg<std::string> outputArg( |
| 65 | "o", "output", "Output file", true /* required */, "" /* default */, |
| 66 | "file" /* type */, cmd); |
| 67 | cmd.parse(argc, argv); |
| 68 | Extract(fileNames.getValue(), outputArg.getValue()); |
| 69 | } catch (TCLAP::ArgException& e) { |
| 70 | std::cerr << "error: " << e.error() << " for arg " << e.argId() |
| 71 | << std::endl; |
| 72 | } catch (Exception& e) { |
| 73 | std::cerr << e.what() << std::endl; |
| 74 | } |
| 75 | return 0; |
| 76 | } |