C + + OPENCC easy to use record

Source: Internet
Author: User
Tags json

In addition to a library of functions, OPENCC can be used to provide command-prompt character forms, as well as the graphics interface's executable file. And his precompiled program is on Google Code, and the latest version of Windows is 0.4.2.

Because the author is writing the program, so the first is to use the function library version, but in the 0.4.2 of the original code ... There are some problems with the compatibility of Visual Studio and I never succeeded in compiling it, so I gave up. Later, it takes the form of calling an external program to call the OPENCC command prompt character in the program to complete the conversion.

And recently, because the program to be revised, so began to try to see, can integrate OPENCC better. As a result, it is found that the latest version on the GITHUB can be used! This is a simple record of the use of the experience ~

If you want to use the OPENCC library in Visual Studio 2013, the first step is to go to the GitHub to download the latest source code. After downloading, the next step is to use the CMake tool to create a Visualsudio project file. The instructions to execute are:
Cmake-g "Visual Studio"-dcmake_install_prefix= ""
-dcmake_build_type=release-denable_gettext:bool=off

After the successful implementation, you can see the directory will appear in Visual Studio corresponding to the project file and program file, this time to open the establishment of the ' Opencc.sln ' This file, you can open the OPENCC scheme ~
No surprise, the plan should be fully built. However, if you want to use OPENCC as a function library, it is only possible to build a ' LIBOPENCC ' project.

After the successful establishment of the LIBOPENCC project, the files created will be in the ' srcdebug ' or ' srcrelease ' directory, and only the two files of ' Opencc.dll ' and ' opencc.lib ' are needed here. (Personal advice can be built release version of the Good)

and other projects in the use of OPENCC, the need for the header file, basically only two: ' Srcopencc.h ' and ' srcexport.hpp '; So after the build, in fact, as long as the following four files out on it ~
Opencc.h
Export.hpp
Opencc.lib
Opencc.dll
Then when to use, in the project setting is the same as the normal library, set the corresponding include path, library path, and set to join Opencc.lib on it.
And the part of the program, is also quite simple ~ as long as include opencc.h this file, you can start to use! And the way he programmed it:

 
 
    1. std::string sinput = "...";
    2. Opencc::simpleconverter MOPENCC ("S2t.json");
    3. std::string sresult = Mopencc.convert (sinput);

Basically, it is to create a Simpleconvert object, MOPENCC it out, and then call his convert () function to convert it. The CONVERT () function accepts, should be UTF-8 encoded std::string, so if the source string does not meet the requirements, you can consider the first through the Boost.locale to do the conversion (reference).

At the time of establishment, specify a configuration setting file for OPENCC to let him know what settings to use for subsequent conversions. Heresy this side is to use ' S2t.json ' This set file, should be regarded as the simplest simplified to the traditional settings.

These settings can be found in the ' dataconfig ' directory under OPENCC. If you open these settings with a text editor, you can see that they are actually very simple JSON-formatted data that defines the corresponding table to use those transformations, like the content of ' S2t.json ', which is basically the following form:
{
"Name": "Simplified Chinese to Traditional Chinese",
"Conversion_chain": [{
"Type": "Mmseg",
"Dict": {
' Type ': ' Group ',
"Dicts": [{
"Type": "OCD",
"File": "Stphrases.ocd"
}, {
"Type": "OCD",
"File": "Stcharacters.ocd"
}]
}
}]
}

Here you can see that he specifies to use the STPHRASES.OCD and STCHARACTERS.OCD two OPENCC customized OCD conversion files. In these files, you can find the corresponding text file in ' Datadictionary ', if you want to use it in OCD format, you need to convert the plain text file that you need into OCD file.
However, because OPENCC can also directly use a plain text file ~ So this side heresy choose not to convert, but to modify the JSON configuration file, so that he directly using the text file ~ and the modified S2t.json content, it becomes:

 
   
   
  1.    ' name ':  ' simplified chinese to traditional  Chinese ",  
  2.   " Conversion_chain ":  [{ 
  3.     " type ": & nbsp; " Mmseg ",  
  4.     " dict ":  { 
  5.         "type":  "group",  
  6.        "dicts":  [{ 
  7. & nbsp;        "type":  "text",  
  8.           "File":  "STPhrases.txt"  
  9.       },  { 
  10.          "type":  "text",  
  11.          "File":  "STCharacters.txt"  
  12.       &NBSP}]  
  13.     }  
  14.  &NBSP}]  

, just place the S2t.json, STPHRASES.OCD, and Stcharacters.ocd three files in the execution directory, Let the program in the initialization of the OPENCC, you can read the good!
And if you want to reverse convert, you can simply use the ' T2s.json ' type of setup file to initialize the OPENCC.
Note:
Theoretically, OPENCC provides the TXT file should be able to use opencc_dict this program to convert to OCD file, but do not know why, heresy converted, OPENCC will not be able to use (the program will stop), so had to give up.
OPENCC If you do not read the settings file or the corresponding dictionary files, there will be no information, directly dead, and can not be in Visual Studio trace code ... This is really annoying.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.