kuromoji
Version:
JavaScript implementation of Japanese morphological analyzer
69 lines (61 loc) • 2.7 kB
JavaScript
/*
* Copyright 2014 Takuya Asano
* Copyright 2010-2014 Atilika Inc. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
;
var CharacterDefinition = require("../CharacterDefinition");
var InvokeDefinitionMap = require("../InvokeDefinitionMap");
var CATEGORY_DEF_PATTERN = /^(\w+)\s+(\d)\s+(\d)\s+(\d)/;
var CATEGORY_MAPPING_PATTERN = /^(0x[0-9A-F]{4})(?:\s+([^#\s]+))(?:\s+([^#\s]+))*/;
var RANGE_CATEGORY_MAPPING_PATTERN = /^(0x[0-9A-F]{4})\.\.(0x[0-9A-F]{4})(?:\s+([^#\s]+))(?:\s+([^#\s]+))*/;
/**
* CharacterDefinitionBuilder
* @constructor
*/
function CharacterDefinitionBuilder() {
this.char_def = new CharacterDefinition();
this.char_def.invoke_definition_map = new InvokeDefinitionMap();
this.character_category_definition = [];
this.category_mapping = [];
}
CharacterDefinitionBuilder.prototype.putLine = function (line) {
var parsed_category_def = CATEGORY_DEF_PATTERN.exec(line);
if (parsed_category_def != null) {
var class_id = this.character_category_definition.length;
var char_class = CharacterDefinition.parseCharCategory(class_id, parsed_category_def);
if (char_class == null) {
return;
}
this.character_category_definition.push(char_class);
return;
}
var parsed_category_mapping = CATEGORY_MAPPING_PATTERN.exec(line);
if (parsed_category_mapping != null) {
var mapping = CharacterDefinition.parseCategoryMapping(parsed_category_mapping);
this.category_mapping.push(mapping);
}
var parsed_range_category_mapping = RANGE_CATEGORY_MAPPING_PATTERN.exec(line);
if (parsed_range_category_mapping != null) {
var range_mapping = CharacterDefinition.parseRangeCategoryMapping(parsed_range_category_mapping);
this.category_mapping.push(range_mapping);
}
};
CharacterDefinitionBuilder.prototype.build = function () {
// TODO If DEFAULT category does not exist, throw error
this.char_def.invoke_definition_map.init(this.character_category_definition);
this.char_def.initCategoryMappings(this.category_mapping);
return this.char_def;
};
module.exports = CharacterDefinitionBuilder;