Open Chinese Convert  0.4.3
A project for conversion between Traditional and Simplified Chinese
opencc.js
1 /**
2  * @file
3  * Node.js API.
4  *
5  * @license
6  * Open Chinese Convert
7  *
8  * Copyright 2010-2013 BYVoid <byvoid@byvoid.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 /**
24  * @defgroup node_api Node.js API
25  *
26  * Node.js language binding
27  */
28 
29 var path = require('path');
30 var binding = require('../build/Release/binding');
31 
32 var assetsPath = path.resolve(__dirname, '../build/Release');
33 var getConfigPath = function (config) {
34  var configPath = config;
35  if (config[0] !== '/' && config[1] !== ':') {
36  // Resolve relative path
37  configPath = path.join(assetsPath, config);
38  }
39  return configPath;
40 };
41 
42 /**
43  * OpenCC Node.js API
44  *
45  * @class OpenCC
46  * @constructor
47  * @ingroup node_api
48  */
49 var OpenCC = module.exports = function (config) {
50  if (!config) {
51  config = 'zhs2zht.ini';
52  }
53  config = getConfigPath(config);
54  this.handler = new binding.Opencc(config);
55 };
56 
57 
58 /**
59  * Default conversion mode.
60  *
61  * @ingroup node_api
62  */
63 OpenCC.CONVERSION_FAST = 0;
64 
65 /**
66  * Only converts text into segments.
67  *
68  * @ingroup node_api
69  */
70 OpenCC.CONVERSION_SEGMENT_ONLY = 1;
71 
72 /**
73  * List all candidates of every segment.
74  *
75  * @ingroup node_api
76  */
77 OpenCC.CONVERSION_LIST_CANDIDATES = 2;
78 
79 /**
80  * Converts input text.
81  *
82  * @fn void convert(string input, function callback)
83  * @memberof OpenCC
84  * @param input Input text.
85  * @param callback Callback function(err, convertedText).
86  * @ingroup node_api
87  */
88 OpenCC.prototype.convert = function (input, callback) {
89  return this.handler.convert(input.toString(), callback);
90 };
91 
92 /**
93  * Converts input text.
94  *
95  * @fn string convertSync(string input)
96  * @memberof OpenCC
97  * @param input Input text.
98  * @return Converted text.
99  * @ingroup node_api
100  */
101 OpenCC.prototype.convertSync = function (input) {
102  return this.handler.convertSync(input.toString());
103 };
104 
105 /**
106  * Sets conversion mode.
107  *
108  * @fn void setConversionMode(int conversionMode)
109  * @memberof OpenCC
110  * @param conversionMode Conversion mode.
111  * @ingroup node_api
112  */
113 OpenCC.prototype.setConversionMode = function (conversionMode) {
114  return this.handler.setConversionMode(conversionMode);
115 };