| 1 |
/*! |
| 2 |
* Lunr languages, `Japanese` language |
| 3 |
* https://github.com/MihaiValentin/lunr-languages |
| 4 |
* |
| 5 |
* Copyright 2014, Chad Liu |
| 6 |
* http://www.mozilla.org/MPL/ |
| 7 |
*/ |
| 8 |
/*! |
| 9 |
* based on |
| 10 |
* Snowball JavaScript Library v0.3 |
| 11 |
* http://code.google.com/p/urim/ |
| 12 |
* http://snowball.tartarus.org/ |
| 13 |
* |
| 14 |
* Copyright 2010, Oleg Mazko |
| 15 |
* http://www.mozilla.org/MPL/ |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* export the module via AMD, CommonJS or as a browser global |
| 20 |
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js |
| 21 |
*/ |
| 22 |
; |
| 23 |
(function(root, factory) { |
| 24 |
if (typeof define === 'function' && define.amd) { |
| 25 |
// AMD. Register as an anonymous module. |
| 26 |
define(factory) |
| 27 |
} else if (typeof exports === 'object') { |
| 28 |
/** |
| 29 |
* Node. Does not work with strict CommonJS, but |
| 30 |
* only CommonJS-like environments that support module.exports, |
| 31 |
* like Node. |
| 32 |
*/ |
| 33 |
module.exports = factory() |
| 34 |
} else { |
| 35 |
// Browser globals (root is window) |
| 36 |
factory()(root.lunr); |
| 37 |
} |
| 38 |
}(this, function() { |
| 39 |
/** |
| 40 |
* Just return a value to define the module export. |
| 41 |
* This example returns an object, but the module |
| 42 |
* can return a function as the exported value. |
| 43 |
*/ |
| 44 |
return function(lunr) { |
| 45 |
/* throw error if lunr is not yet included */ |
| 46 |
if ('undefined' === typeof lunr) { |
| 47 |
throw new Error('Lunr is not present. Please include / require Lunr before this script.'); |
| 48 |
} |
| 49 |
|
| 50 |
/* throw error if lunr stemmer support is not yet included */ |
| 51 |
if ('undefined' === typeof lunr.stemmerSupport) { |
| 52 |
throw new Error('Lunr stemmer support is not present. Please include / require Lunr stemmer support before this script.'); |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
Japanese tokenization is trickier, since it does not |
| 57 |
take into account spaces. |
| 58 |
Since the tokenization function is represented different |
| 59 |
internally for each of the Lunr versions, this had to be done |
| 60 |
in order to try to try to pick the best way of doing this based |
| 61 |
on the Lunr version |
| 62 |
*/ |
| 63 |
var isLunr2 = lunr.version[0] == "2"; |
| 64 |
|
| 65 |
/* register specific locale function */ |
| 66 |
lunr.ja = function() { |
| 67 |
this.pipeline.reset(); |
| 68 |
this.pipeline.add( |
| 69 |
lunr.ja.trimmer, |
| 70 |
lunr.ja.stopWordFilter, |
| 71 |
lunr.ja.stemmer |
| 72 |
); |
| 73 |
|
| 74 |
// change the tokenizer for japanese one |
| 75 |
if (isLunr2) { // for lunr version 2.0.0 |
| 76 |
this.tokenizer = lunr.ja.tokenizer; |
| 77 |
} else { |
| 78 |
if (lunr.tokenizer) { // for lunr version 0.6.0 |
| 79 |
lunr.tokenizer = lunr.ja.tokenizer; |
| 80 |
} |
| 81 |
if (this.tokenizerFn) { // for lunr version 0.7.0 -> 1.0.0 |
| 82 |
this.tokenizerFn = lunr.ja.tokenizer; |
| 83 |
} |
| 84 |
} |
| 85 |
}; |
| 86 |
var segmenter = new lunr.TinySegmenter(); // インスタンス生成 |
| 87 |
|
| 88 |
lunr.ja.tokenizer = function(obj) { |
| 89 |
var i; |
| 90 |
var str; |
| 91 |
var len; |
| 92 |
var segs; |
| 93 |
var tokens; |
| 94 |
var char; |
| 95 |
var sliceLength; |
| 96 |
var sliceStart; |
| 97 |
var sliceEnd; |
| 98 |
var segStart; |
| 99 |
|
| 100 |
if (!arguments.length || obj == null || obj == undefined) |
| 101 |
return []; |
| 102 |
|
| 103 |
if (Array.isArray(obj)) { |
| 104 |
return obj.map( |
| 105 |
function(t) { |
| 106 |
return isLunr2 ? new lunr.Token(t.toLowerCase()) : t.toLowerCase(); |
| 107 |
} |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
str = obj.toString().toLowerCase().replace(/^\s+/, ''); |
| 112 |
for (i = str.length - 1; i >= 0; i--) { |
| 113 |
if (/\S/.test(str.charAt(i))) { |
| 114 |
str = str.substring(0, i + 1); |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
tokens = []; |
| 120 |
len = str.length; |
| 121 |
for (sliceEnd = 0, sliceStart = 0; sliceEnd <= len; sliceEnd++) { |
| 122 |
char = str.charAt(sliceEnd); |
| 123 |
sliceLength = sliceEnd - sliceStart; |
| 124 |
|
| 125 |
if ((char.match(/\s/) || sliceEnd == len)) { |
| 126 |
if (sliceLength > 0) { |
| 127 |
segs = segmenter.segment(str.slice(sliceStart, sliceEnd)).filter( |
| 128 |
function(token) { |
| 129 |
return !!token; |
| 130 |
} |
| 131 |
); |
| 132 |
|
| 133 |
segStart = sliceStart; |
| 134 |
for (i = 0; i < segs.length; i++) { |
| 135 |
if (isLunr2) { |
| 136 |
tokens.push( |
| 137 |
new lunr.Token( |
| 138 |
segs[i], { |
| 139 |
position: [segStart, segs[i].length], |
| 140 |
index: tokens.length |
| 141 |
} |
| 142 |
) |
| 143 |
); |
| 144 |
} else { |
| 145 |
tokens.push(segs[i]); |
| 146 |
} |
| 147 |
segStart += segs[i].length; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
sliceStart = sliceEnd + 1; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return tokens; |
| 156 |
} |
| 157 |
|
| 158 |
/* lunr stemmer function */ |
| 159 |
lunr.ja.stemmer = (function() { |
| 160 |
|
| 161 |
/* TODO japanese stemmer */ |
| 162 |
return function(word) { |
| 163 |
return word; |
| 164 |
} |
| 165 |
})(); |
| 166 |
lunr.Pipeline.registerFunction(lunr.ja.stemmer, 'stemmer-ja'); |
| 167 |
|
| 168 |
/* lunr trimmer function */ |
| 169 |
lunr.ja.wordCharacters = "一二三四五六七八九十百千万億兆一-龠々〆ヵヶぁ-んァ-ヴーア-ン゙a-zA-Za-zA-Z0-90-9"; |
| 170 |
lunr.ja.trimmer = lunr.trimmerSupport.generateTrimmer(lunr.ja.wordCharacters); |
| 171 |
lunr.Pipeline.registerFunction(lunr.ja.trimmer, 'trimmer-ja'); |
| 172 |
|
| 173 |
/* lunr stop word filter. see http://www.ranks.nl/stopwords/japanese */ |
| 174 |
lunr.ja.stopWordFilter = lunr.generateStopWordFilter( |
| 175 |
'これ それ あれ この その あの ここ そこ あそこ こちら どこ だれ なに なん 何 私 貴方 貴方方 我々 私達 あの人 あのかた 彼女 彼 です あります おります います は が の に を で え から まで より も どの と し それで しかし'.split(' ')); |
| 176 |
lunr.Pipeline.registerFunction(lunr.ja.stopWordFilter, 'stopWordFilter-ja'); |
| 177 |
|
| 178 |
// alias ja => jp for backward-compatibility. |
| 179 |
// jp is the country code, while ja is the language code |
| 180 |
// a new lunr.ja.js has been created, but in order to |
| 181 |
// keep the backward compatibility, we'll leave the lunr.jp.js |
| 182 |
// here for a while, and just make it use the new lunr.ja.js |
| 183 |
lunr.jp = lunr.ja; |
| 184 |
lunr.Pipeline.registerFunction(lunr.jp.stemmer, 'stemmer-jp'); |
| 185 |
lunr.Pipeline.registerFunction(lunr.jp.trimmer, 'trimmer-jp'); |
| 186 |
lunr.Pipeline.registerFunction(lunr.jp.stopWordFilter, 'stopWordFilter-jp'); |
| 187 |
}; |
| 188 |
})) |