-
Notifications
You must be signed in to change notification settings - Fork 862
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from gmarty/transliterator-ja
Japanese transliterator (Hiragana and Katakana) to Latin
- Loading branch information
Showing
11 changed files
with
1,494 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
Copyright (c) 2012, Guillaume Marty | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* A very basic stemmer that performs the following steps: | ||
* * Stem katakana. | ||
* Inspired by: | ||
* http://svn.apache.org/repos/asf/lucene/dev/trunk/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/JapaneseKatakanaStemFilter.java | ||
* | ||
* \@todo Use .bind() in StemmerJa.prototype.attach(). | ||
*/ | ||
|
||
var Tokenizer = require('../tokenizers/tokenizer_ja'); | ||
var stopwords = require('../util/stopwords_ja'); | ||
|
||
|
||
|
||
/** | ||
* @constructor | ||
*/ | ||
var StemmerJa = function() { | ||
}; | ||
|
||
|
||
/** | ||
* Tokenize and stem a text. | ||
* Stop words are excluded except if the second argument is true. | ||
* | ||
* @param {string} text | ||
* @param {boolean} keepStops Whether to keep stop words from the output or not. | ||
* @return {Array.<string>} | ||
*/ | ||
StemmerJa.prototype.tokenizeAndStem = function(text, keepStops) { | ||
var self = this; | ||
var stemmedTokens = []; | ||
var tokens = new Tokenizer().tokenize(text); | ||
|
||
// This is probably faster than an if at each iteration. | ||
if (keepStops) { | ||
tokens.forEach(function(token) { | ||
var resultToken = token.toLowerCase(); | ||
resultToken = self.stem(resultToken); | ||
stemmedTokens.push(resultToken); | ||
}); | ||
} else { | ||
tokens.forEach(function(token) { | ||
if (stopwords.indexOf(token) == -1) { | ||
var resultToken = token.toLowerCase(); | ||
resultToken = self.stem(resultToken); | ||
stemmedTokens.push(resultToken); | ||
} | ||
}); | ||
} | ||
|
||
return stemmedTokens; | ||
}; | ||
|
||
|
||
/** | ||
* Stem a term. | ||
* | ||
* @param {string} token | ||
* @return {string} | ||
*/ | ||
StemmerJa.prototype.stem = function(token) { | ||
token = this.stemKatakana(token); | ||
|
||
return token; | ||
}; | ||
|
||
|
||
/** | ||
* Remove the final prolonged sound mark on katakana if length is superior to | ||
* a threshold. | ||
* | ||
* @param {string} token A katakana string to stem. | ||
* @return {string} A katakana string stemmed. | ||
*/ | ||
StemmerJa.prototype.stemKatakana = function(token) { | ||
var HIRAGANA_KATAKANA_PROLONGED_SOUND_MARK = 'ー'; | ||
var DEFAULT_MINIMUM_LENGTH = 4; | ||
|
||
if (token.length >= DEFAULT_MINIMUM_LENGTH | ||
&& token.slice(-1) === HIRAGANA_KATAKANA_PROLONGED_SOUND_MARK | ||
&& this.isKatakana(token)) { | ||
token = token.slice(0, token.length - 1); | ||
} | ||
return token; | ||
}; | ||
|
||
|
||
/** | ||
* Is a string made of fullwidth katakana only? | ||
* This implementation is the fastest I know: | ||
* http://jsperf.com/string-contain-katakana-only/2 | ||
* | ||
* @param {string} str A string. | ||
* @return {boolean} True if the string has katakana only. | ||
*/ | ||
StemmerJa.prototype.isKatakana = function(str) { | ||
return !!str.match(/^[ァ-ヴー]+$/); | ||
}; | ||
|
||
// Expose an attach function that will patch String with new methods. | ||
StemmerJa.prototype.attach = function() { | ||
var self = this; | ||
|
||
String.prototype.stem = function() { | ||
return self.stem(this); | ||
}; | ||
|
||
String.prototype.tokenizeAndStem = function(keepStops) { | ||
return self.tokenizeAndStem(this, keepStops); | ||
}; | ||
}; | ||
|
||
module.exports = StemmerJa; |
Oops, something went wrong.