Skip to content

Commit

Permalink
Merge pull request #77 from tvmani/master
Browse files Browse the repository at this point in the history
Tamil Language support
  • Loading branch information
MihaiValentin authored Aug 17, 2021
2 parents 732bd3d + 8ad3a48 commit dd1f25e
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
build:
working_directory: ~/lunr-languages
docker:
- image: circleci/node:10.3.0
- image: circleci/node:10.24.1
steps:
- checkout
- run:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Lunr Languages is a [Lunr](http://lunrjs.com/) addon that helps you search in do
* ![](https://raw.githubusercontent.com/madebybowtie/FlagKit/master/Assets/PNG/IQ.png) Arabic
* ![](https://raw.githubusercontent.com/madebybowtie/FlagKit/master/Assets/PNG/IN.png) Hindi
* ![](https://raw.githubusercontent.com/madebybowtie/FlagKit/master/Assets/PNG/CN.png) Chinese
* ![](https://raw.githubusercontent.com/madebybowtie/FlagKit/master/Assets/PNG/IN.png) Tamil
* [Contribute with a new language](CONTRIBUTING.md)

Lunr Languages is compatible with Lunr version `0.6`, `0.7`, `1.0` and `2.X`.
Expand Down
2 changes: 2 additions & 0 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ var list = [
file: 'SwedishStemmer.js',
stopwords: stopwordsCustomFolder + 'sv.csv',
wordCharacters: wordCharacters('Latin')
}, {
locale: 'ta',
}, {
locale: 'tr',
file: 'TurkishStemmer.js',
Expand Down
123 changes: 123 additions & 0 deletions lunr.ta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*!
* Lunr languages, `Tamil` language
* https://github.com/tvmani/lunr-languages
*
* Copyright 2021, Manikandan Venkatasubban
* http://www.mozilla.org/MPL/
*/
/*!
* based on
* Snowball JavaScript Library v0.3
* http://code.google.com/p/urim/
* http://snowball.tartarus.org/
*
* Copyright 2010, Oleg Mazko
* http://www.mozilla.org/MPL/
*/

/**
* export the module via AMD, CommonJS or as a browser global
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
*/
;
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory)
} else if (typeof exports === 'object') {
/**
* Node. Does not work with strict CommonJS, but
* only CommonJS-like environments that support module.exports,
* like Node.
*/
module.exports = factory()
} else {
// Browser globals (root is window)
factory()(root.lunr);
}
}(this, function() {
/**
* Just return a value to define the module export.
* This example returns an object, but the module
* can return a function as the exported value.
*/
return function(lunr) {
/* throw error if lunr is not yet included */
if ('undefined' === typeof lunr) {
throw new Error('Lunr is not present. Please include / require Lunr before this script.');
}

/* throw error if lunr stemmer support is not yet included */
if ('undefined' === typeof lunr.stemmerSupport) {
throw new Error('Lunr stemmer support is not present. Please include / require Lunr stemmer support before this script.');
}

/* register specific locale function */
lunr.ta = function() {
this.pipeline.reset();
this.pipeline.add(
lunr.ta.trimmer,
lunr.ta.stopWordFilter,
lunr.ta.stemmer
);

// change the tokenizer for japanese one
// if (isLunr2) { // for lunr version 2.0.0
// this.tokenizer = lunr.ta.tokenizer;
// } else {
// if (lunr.tokenizer) { // for lunr version 0.6.0
// lunr.tokenizer = lunr.ta.tokenizer;
// }
// if (this.tokenizerFn) { // for lunr version 0.7.0 -> 1.0.0
// this.tokenizerFn = lunr.ta.tokenizer;
// }
// }

if (this.searchPipeline) {
this.searchPipeline.reset();
this.searchPipeline.add(lunr.ta.stemmer)
}
};

/* lunr trimmer function */
lunr.ta.wordCharacters = "\u0b80-\u0b89\u0b8a-\u0b8f\u0b90-\u0b99\u0b9a-\u0b9f\u0ba0-\u0ba9\u0baa-\u0baf\u0bb0-\u0bb9\u0bba-\u0bbf\u0bc0-\u0bc9\u0bca-\u0bcf\u0bd0-\u0bd9\u0bda-\u0bdf\u0be0-\u0be9\u0bea-\u0bef\u0bf0-\u0bf9\u0bfa-\u0bffa-zA-Za-zA-Z0-90-9";

lunr.ta.trimmer = lunr.trimmerSupport.generateTrimmer(lunr.ta.wordCharacters);

lunr.Pipeline.registerFunction(lunr.ta.trimmer, 'trimmer-ta');
/* lunr stop word filter */
lunr.ta.stopWordFilter = lunr.generateStopWordFilter(
'அங்கு அங்கே அது அதை அந்த அவர் அவர்கள் அவள் அவன் அவை ஆக ஆகவே ஆகையால் ஆதலால் ஆதலினால் ஆனாலும் ஆனால் இங்கு இங்கே இது இதை இந்த இப்படி இவர் இவர்கள் இவள் இவன் இவை இவ்வளவு உனக்கு உனது உன் உன்னால் எங்கு எங்கே எது எதை எந்த எப்படி எவர் எவர்கள் எவள் எவன் எவை எவ்வளவு எனக்கு எனது எனவே என் என்ன என்னால் ஏது ஏன் தனது தன்னால் தானே தான் நாங்கள் நாம் நான் நீ நீங்கள்'.split(' '));
/* lunr stemmer function */
lunr.ta.stemmer = (function() {

return function(word) {
// for lunr version 2
if (typeof word.update === "function") {
return word.update(function(word) {
return word;
})
} else { // for lunr version <= 1
return word;
}

}
})();

var segmenter = lunr.wordcut;
segmenter.init();
lunr.ta.tokenizer = function(obj) {
if (!arguments.length || obj == null || obj == undefined) return []
if (Array.isArray(obj)) return obj.map(function(t) {
return isLunr2 ? new lunr.Token(t.toLowerCase()) : t.toLowerCase()
});

var str = obj.toString().toLowerCase().replace(/^\s+/, '');
return segmenter.cut(str).split('|');
}

lunr.Pipeline.registerFunction(lunr.ta.stemmer, 'stemmer-ta');
lunr.Pipeline.registerFunction(lunr.ta.stopWordFilter, 'stopWordFilter-ta');

};
}))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"mocha": "^9.0.0",
"uglify-js": "^2.4.15",
"unicode-8.0.0": "^0.1.5",
"nodejieba": "^2.5.0"
"nodejieba": "^2.5.2"
}
}
3 changes: 2 additions & 1 deletion test/VersionsAndLanguagesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var testDocuments = {
ro: require('./testdata/ro'),
ru: require('./testdata/ru'),
sv: require('./testdata/sv'),
ta: require('./testdata/ta'),
tr: require('./testdata/tr'),
th: require('./testdata/th'),
vi: require('./testdata/vi'),
Expand Down Expand Up @@ -109,7 +110,7 @@ lunrVersions.forEach(function(lunrVersion) {
if (language === 'ja' || language === 'jp') { // for japanese, we must also load the tinyseg tokenizer
require('../tinyseg')(lunr);
}
if (language === 'th' || language === 'hi') { // for thai, we must also load the wordcut tokenizer
if (language === 'th' || language === 'hi' || language === 'ta') { // for thai, we must also load the wordcut tokenizer
lunr.wordcut = require('../wordcut');
}
require('../lunr.' + language + '.js')(lunr);
Expand Down
61 changes: 61 additions & 0 deletions test/testdata/ta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = {
fields: [
{
name: 'title',
config: { boost: 10 }
}, {
name: 'body'
}
],
documents: [
{
"title": "கடவுள் வாழ்த்து",
"body": "அகர முதல எழுத்தெல்லாம் ஆதி\nபகவன் முதற்றே உலகு thank you",
"id": 1
},
{
"title": "நீத்தார் பெருமை",
"body": "சுவைஒளி ஊறுஓசை நாற்றமென ஐந்தின்\nவகைதெரிவான் கட்டே உலகு ",
"id": 2
}


],
tests: [
{
what: "find the word %w",
search: "அகர",
found: 1
},
{
what: "find the word %w",
search: "thank you",
found: 1
},
{
what: "find the word, like %w",
search: "கடவுள்",
found: 1
},
{
what: "find the word, like %w",
search: "அகர முதல",
found: 1
},
{
what: "find the word, like %w",
search: "அகர முதல பகவன் முதற்றே",
found: 1
},
{
what: "find the word, like %w",
search: "உலகு",
found: 2
},
{
what: "never find a word that does not exist, like %w",
search: "நற்றாள்",
found: 0
}
]
}

0 comments on commit dd1f25e

Please sign in to comment.