fr_stemmer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Author: Kasun Gajasinghe
  3. * E-Mail: kasunbg AT gmail DOT com
  4. * Date: 09.08.2010
  5. *
  6. * usage: stemmer(word);
  7. * ex: var stem = stemmer(foobar);
  8. * Implementation of the stemming algorithm from http://snowball.tartarus.org/algorithms/french/stemmer.html
  9. *
  10. * LICENSE:
  11. *
  12. * Copyright (c) 2010, Kasun Gajasinghe. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. *
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY KASUN GAJASINGHE ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  26. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KASUN GAJASINGHE BE LIABLE FOR ANY DIRECT,
  28. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  31. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  32. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. var stemmer = function(word){
  36. // Letters in French include the following accented forms,
  37. // â à ç ë é ê è ï î ô û ù
  38. // The following letters are vowels:
  39. // a e i o u y â à ë é ê è ï î ô û ù
  40. word = word.toLowerCase();
  41. var oriWord = word;
  42. word = word.replace(/qu/g, 'qU'); //have to perform first, as after the operation, capital U is not treated as a vowel
  43. word = word.replace(/([aeiouyâàëéêèïîôûù])u([aeiouyâàëéêèïîôûù])/g, '$1U$2');
  44. word = word.replace(/([aeiouyâàëéêèïîôûù])i([aeiouyâàëéêèïîôûù])/g, '$1I$2');
  45. word = word.replace(/([aeiouyâàëéêèïîôûù])y/g, '$1Y');
  46. word = word.replace(/y([aeiouyâàëéêèïîôûù])/g, 'Y$1');
  47. var rv='';
  48. var rvIndex = -1;
  49. if(word.search(/^(par|col|tap)/) != -1 || word.search(/^[aeiouyâàëéêèïîôûù]{2}/) != -1){
  50. rv = word.substring(3);
  51. rvIndex = 3;
  52. } else {
  53. rvIndex = word.substring(1).search(/[aeiouyâàëéêèïîôûù]/);
  54. if(rvIndex != -1){
  55. rvIndex +=2; //+2 is to supplement the substring(1) used to find rvIndex
  56. rv = word.substring(rvIndex);
  57. } else {
  58. rvIndex = word.length;
  59. }
  60. }
  61. // R1 is the region after the first non-vowel following a vowel, or the end of the word if there is no such non-vowel.
  62. // R2 is the region after the first non-vowel following a vowel in R1, or the end of the word if there is no such non-vowel
  63. var r1Index = word.search(/[aeiouyâàëéêèïîôûù][^aeiouyâàëéêèïîôûù]/);
  64. var r1 = '';
  65. if (r1Index != -1) {
  66. r1Index += 2;
  67. r1 = word.substring(r1Index);
  68. } else {
  69. r1Index = word.length;
  70. }
  71. var r2Index = -1;
  72. var r2 = '';
  73. if (r1Index != -1) {
  74. r2Index = r1.search(/[aeiouyâàëéêèïîôûù][^aeiouyâàëéêèïîôûù]/);
  75. if (r2Index != -1) {
  76. r2Index += 2;
  77. r2 = r1.substring(r2Index);
  78. r2Index += r1Index;
  79. } else {
  80. r2 = '';
  81. r2Index = word.length;
  82. }
  83. }
  84. if (r1Index != -1 && r1Index < 3) {
  85. r1Index = 3;
  86. r1 = word.substring(r1Index);
  87. }
  88. /*
  89. Step 1: Standard suffix removal
  90. */
  91. var a1Index = word.search(/(ance|iqUe|isme|able|iste|eux|ances|iqUes|ismes|ables|istes)$/);
  92. var a2Index = word.search(/(atrice|ateur|ation|atrices|ateurs|ations)$/);
  93. var a3Index = word.search(/(logie|logies)$/);
  94. var a4Index = word.search(/(usion|ution|usions|utions)$/);
  95. var a5Index = word.search(/(ence|ences)$/);
  96. var a6Index = word.search(/(ement|ements)$/);
  97. var a7Index = word.search(/(ité|ités)$/);
  98. var a8Index = word.search(/(if|ive|ifs|ives)$/);
  99. var a9Index = word.search(/(eaux)$/);
  100. var a10Index = word.search(/(aux)$/);
  101. var a11Index = word.search(/(euse|euses)$/);
  102. var a12Index = word.search(/[^aeiouyâàëéêèïîôûù](issement|issements)$/);
  103. var a13Index = word.search(/(amment)$/);
  104. var a14Index = word.search(/(emment)$/);
  105. var a15Index = word.search(/[aeiouyâàëéêèïîôûù](ment|ments)$/);
  106. if(a1Index != -1 && a1Index >= r2Index){
  107. word = word.substring(0,a1Index);
  108. } else if(a2Index != -1 && a2Index >= r2Index){
  109. word = word.substring(0,a2Index);
  110. var a2Index2 = word.search(/(ic)$/);
  111. if(a2Index2 != -1 && a2Index2 >= r2Index){
  112. word = word.substring(0, a2Index2); //if preceded by ic, delete if in R2,
  113. } else { //else replace by iqU
  114. word = word.replace(/(ic)$/,'iqU');
  115. }
  116. } else if(a3Index != -1 && a3Index >= r2Index){
  117. word = word.replace(/(logie|logies)$/,'log'); //replace with log if in R2
  118. } else if(a4Index != -1 && a4Index >= r2Index){
  119. word = word.replace(/(usion|ution|usions|utions)$/,'u'); //replace with u if in R2
  120. } else if(a5Index != -1 && a5Index >= r2Index){
  121. word = word.replace(/(ence|ences)$/,'ent'); //replace with ent if in R2
  122. } else if(a6Index != -1 && a6Index >= rvIndex){
  123. word = word.substring(0,a6Index);
  124. if(word.search(/(iv)$/) >= r2Index){
  125. word = word.replace(/(iv)$/, '');
  126. if(word.search(/(at)$/) >= r2Index){
  127. word = word.replace(/(at)$/, '');
  128. }
  129. } else if(word.search(/(eus)$/) != -1){
  130. var a6Index2 = word.search(/(eus)$/);
  131. if(a6Index2 >=r2Index){
  132. word = word.substring(0, a6Index2);
  133. } else if(a6Index2 >= r1Index){
  134. word = word.substring(0,a6Index2)+"eux";
  135. }
  136. } else if(word.search(/(abl|iqU)$/) >= r2Index){
  137. word = word.replace(/(abl|iqU)$/,''); //if preceded by abl or iqU, delete if in R2,
  138. } else if(word.search(/(ièr|Ièr)$/) >= rvIndex){
  139. word = word.replace(/(ièr|Ièr)$/,'i'); //if preceded by abl or iqU, delete if in R2,
  140. }
  141. } else if(a7Index != -1 && a7Index >= r2Index){
  142. word = word.substring(0,a7Index); //delete if in R2
  143. if(word.search(/(abil)$/) != -1){ //if preceded by abil, delete if in R2, else replace by abl, otherwise,
  144. var a7Index2 = word.search(/(abil)$/);
  145. if(a7Index2 >=r2Index){
  146. word = word.substring(0, a7Index2);
  147. } else {
  148. word = word.substring(0,a7Index2)+"abl";
  149. }
  150. } else if(word.search(/(ic)$/) != -1){
  151. var a7Index3 = word.search(/(ic)$/);
  152. if(a7Index3 != -1 && a7Index3 >= r2Index){
  153. word = word.substring(0, a7Index3); //if preceded by ic, delete if in R2,
  154. } else { //else replace by iqU
  155. word = word.replace(/(ic)$/,'iqU');
  156. }
  157. } else if(word.search(/(iv)$/) != r2Index){
  158. word = word.replace(/(iv)$/,'');
  159. }
  160. } else if(a8Index != -1 && a8Index >= r2Index){
  161. word = word.substring(0,a8Index);
  162. if(word.search(/(at)$/) >= r2Index){
  163. word = word.replace(/(at)$/, '');
  164. if(word.search(/(ic)$/) >= r2Index){
  165. word = word.replace(/(ic)$/, '');
  166. } else { word = word.replace(/(ic)$/, 'iqU'); }
  167. }
  168. } else if(a9Index != -1){ word = word.replace(/(eaux)/,'eau')
  169. } else if(a10Index >= r1Index){ word = word.replace(/(aux)/,'al')
  170. } else if(a11Index != -1 ){
  171. var a11Index2 = word.search(/(euse|euses)$/);
  172. if(a11Index2 >=r2Index){
  173. word = word.substring(0, a11Index2);
  174. } else if(a11Index2 >= r1Index){
  175. word = word.substring(0, a11Index2)+"eux";
  176. }
  177. } else if(a12Index!=-1 && a12Index>=r1Index){
  178. word = word.substring(0,a12Index+1); //+1- amendment to non-vowel
  179. } else if(a13Index!=-1 && a13Index>=rvIndex){
  180. word = word.replace(/(amment)$/,'ant');
  181. } else if(a14Index!=-1 && a14Index>=rvIndex){
  182. word = word.replace(/(emment)$/,'ent');
  183. } else if(a15Index!=-1 && a15Index>=rvIndex){
  184. word = word.substring(0,a15Index+1);
  185. }
  186. /* Step 2a: Verb suffixes beginning i*/
  187. var wordStep1 = word;
  188. var step2aDone = false;
  189. if(oriWord == word.toLowerCase() || oriWord.search(/(amment|emment|ment|ments)$/) != -1){
  190. step2aDone = true;
  191. var b1Regex = /([^aeiouyâàëéêèïîôûù])(îmes|ît|îtes|i|ie|ies|ir|ira|irai|iraIent|irais|irait|iras|irent|irez|iriez|irions|irons|iront|is|issaIent|issais|issait|issant|issante|issantes|issants|isse|issent|isses|issez|issiez|issions|issons|it)$/i;
  192. if(word.search(b1Regex) >= rvIndex){
  193. word = word.replace(b1Regex,'$1');
  194. }
  195. }
  196. /* Step 2b: Other verb suffixes*/
  197. if (step2aDone && wordStep1 == word) {
  198. if (word.search(/(ions)$/) >= r2Index) {
  199. word = word.replace(/(ions)$/, '');
  200. } else {
  201. var b2Regex = /(é|ée|ées|és|èrent|er|era|erai|eraIent|erais|erait|eras|erez|eriez|erions|erons|eront|ez|iez)$/i;
  202. if (word.search(b2Regex) >= rvIndex) {
  203. word = word.replace(b2Regex, '');
  204. } else {
  205. var b3Regex = /e(âmes|ât|âtes|a|ai|aIent|ais|ait|ant|ante|antes|ants|as|asse|assent|asses|assiez|assions)$/i;
  206. if (word.search(b3Regex) >= rvIndex) {
  207. word = word.replace(b3Regex, '');
  208. } else {
  209. var b3Regex2 = /(âmes|ât|âtes|a|ai|aIent|ais|ait|ant|ante|antes|ants|as|asse|assent|asses|assiez|assions)$/i;
  210. if (word.search(b3Regex2) >= rvIndex) {
  211. word = word.replace(b3Regex2, '');
  212. }
  213. }
  214. }
  215. }
  216. }
  217. if(oriWord != word.toLowerCase()){
  218. /* Step 3 */
  219. var rep = '';
  220. if(word.search(/Y$/) != -1) {
  221. word = word.replace(/Y$/, 'i');
  222. } else if(word.search(/ç$/) != -1){
  223. word = word.replace(/ç$/, 'c');
  224. }
  225. } else {
  226. /* Step 4 */
  227. //If the word ends s, not preceded by a, i, o, u, è or s, delete it.
  228. if (word.search(/([^aiouès])s$/) >= rvIndex) {
  229. word = word.replace(/([^aiouès])s$/, '$1');
  230. }
  231. var e1Index = word.search(/ion$/);
  232. if (e1Index >= r2Index && word.search(/[st]ion$/) >= rvIndex) {
  233. word = word.substring(0, e1Index);
  234. } else {
  235. var e2Index = word.search(/(ier|ière|Ier|Ière)$/);
  236. if (e2Index != -1 && e2Index >= rvIndex) {
  237. word = word.substring(0, e2Index) + "i";
  238. } else {
  239. if (word.search(/e$/) >= rvIndex) {
  240. word = word.replace(/e$/, ''); //delete last e
  241. } else if (word.search(/guë$/) >= rvIndex) {
  242. word = word.replace(/guë$/, 'gu');
  243. }
  244. }
  245. }
  246. }
  247. /* Step 5: Undouble */
  248. //word = word.replace(/(en|on|et|el|eil)(n|t|l)$/,'$1');
  249. word = word.replace(/(en|on)(n)$/,'$1');
  250. word = word.replace(/(ett)$/,'et');
  251. word = word.replace(/(el|eil)(l)$/,'$1');
  252. /* Step 6: Un-accent */
  253. word = word.replace(/[éè]([^aeiouyâàëéêèïîôûù]+)$/,'e$1');
  254. word = word.toLowerCase();
  255. return word;
  256. };
  257. var eqOut = new Array();
  258. var noteqOut = new Array();
  259. var eqCount = 0;
  260. /*
  261. To test the stemming, create two arrays named "voc" and "COut" which are for vocabualary and the stemmed output.
  262. Then add the vocabulary strings and output strings. This method will generate the stemmed output for "voc" and will
  263. compare the output with COut.
  264. (I used porter's voc and out files and did a regex to convert them to js objects. regex: /");\nvoc.push("/g . This
  265. will add strings to voc array such that output would look like: voc.push("foobar"); ) drop me an email for any help.
  266. */
  267. function testFr(){
  268. var start = new Date().getTime(); //execution time
  269. eqCount = 0;
  270. eqOut = new Array();
  271. noteqOut = new Array();
  272. for(var k=0;k<voc.length;k++){
  273. if(COut[k]==stemmer(voc[k])){
  274. eqCount++;
  275. eqOut.push("v: "+voc[k]+" c: "+COut[k]);
  276. } else {
  277. noteqOut.push(voc[k]+", c: "+COut[k]+" s:"+stemmer(voc[k]));
  278. }
  279. }
  280. var end = new Date().getTime(); //execution time
  281. var time = end-start;
  282. alert("equal count= "+eqCount+" out of "+voc.length+" words. time= "+time+" ms");
  283. //console.log("equal count= "+eqCount+" out of "+voc.length+" words. time= "+time+" ms");
  284. }