de_stemmer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Author: Joder Illi
  3. *
  4. * Copyright (c) 2010, FormBlitz AG
  5. * All rights reserved.
  6. * Implementation of the stemming algorithm from http://snowball.tartarus.org/algorithms/german/stemmer.html
  7. * Copyright of the algorithm is: Copyright (c) 2001, Dr Martin Porter and can be found at http://snowball.tartarus.org/license.php
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification, is covered by the standard BSD license.
  10. *
  11. */
  12. //var stemmer = function Stemmer() {
  13. /*
  14. German includes the following accented forms,
  15. ä ö ü
  16. and a special letter, ß, equivalent to double s.
  17. The following letters are vowels:
  18. a e i o u y ä ö ü
  19. */
  20. var stemmer = function(word) {
  21. /*
  22. Put u and y between vowels into upper case
  23. */
  24. word = word.replace(/([aeiouyäöü])u([aeiouyäöü])/g, '$1U$2');
  25. word = word.replace(/([aeiouyäöü])y([aeiouyäöü])/g, '$1Y$2');
  26. /*
  27. and then do the following mappings,
  28. (a) replace ß with ss,
  29. (a) replace ae with ä, Not doing these, have trouble with diphtongs
  30. (a) replace oe with ö, Not doing these, have trouble with diphtongs
  31. (a) replace ue with ü unless preceded by q. Not doing these, have trouble with diphtongs
  32. So in quelle, ue is not mapped to ü because it follows q, and in feuer it is not mapped because the first part of the rule changes it to feUer, so the u is not found.
  33. */
  34. word = word.replace(/ß/g, 'ss');
  35. //word = word.replace(/ae/g, 'ä');
  36. //word = word.replace(/oe/g, 'ö');
  37. //word = word.replace(/([^q])ue/g, '$1ü');
  38. /*
  39. R1 and R2 are first set up in the standard way (see the note on R1 and R2), but then R1 is adjusted so that the region before it contains at least 3 letters.
  40. R1 is the region after the first non-vowel following a vowel, or is the null region at the end of the word if there is no such non-vowel.
  41. R2 is the region after the first non-vowel following a vowel in R1, or is the null region at the end of the word if there is no such non-vowel.
  42. */
  43. var r1Index = word.search(/[aeiouyäöü][^aeiouyäöü]/);
  44. var r1 = '';
  45. if (r1Index != -1) {
  46. r1Index += 2;
  47. r1 = word.substring(r1Index);
  48. }
  49. var r2Index = -1;
  50. var r2 = '';
  51. if (r1Index != -1) {
  52. var r2Index = r1.search(/[aeiouyäöü][^aeiouyäöü]/);
  53. if (r2Index != -1) {
  54. r2Index += 2;
  55. r2 = r1.substring(r2Index);
  56. r2Index += r1Index;
  57. } else {
  58. r2 = '';
  59. }
  60. }
  61. if (r1Index != -1 && r1Index < 3) {
  62. r1Index = 3;
  63. r1 = word.substring(r1Index);
  64. }
  65. /*
  66. Define a valid s-ending as one of b, d, f, g, h, k, l, m, n, r or t.
  67. Define a valid st-ending as the same list, excluding letter r.
  68. */
  69. /*
  70. Do each of steps 1, 2 and 3.
  71. */
  72. /*
  73. Step 1:
  74. Search for the longest among the following suffixes,
  75. (a) em ern er
  76. (b) e en es
  77. (c) s (preceded by a valid s-ending)
  78. */
  79. var a1Index = word.search(/(em|ern|er)$/g);
  80. var b1Index = word.search(/(e|en|es)$/g);
  81. var c1Index = word.search(/([bdfghklmnrt]s)$/g);
  82. if (c1Index != -1) {
  83. c1Index++;
  84. }
  85. var index1 = 10000;
  86. var optionUsed1 = '';
  87. if (a1Index != -1 && a1Index < index1) {
  88. optionUsed1 = 'a';
  89. index1 = a1Index;
  90. }
  91. if (b1Index != -1 && b1Index < index1) {
  92. optionUsed1 = 'b';
  93. index1 = b1Index;
  94. }
  95. if (c1Index != -1 && c1Index < index1) {
  96. optionUsed1 = 'c';
  97. index1 = c1Index;
  98. }
  99. /*
  100. and delete if in R1. (Of course the letter of the valid s-ending is not necessarily in R1.) If an ending of group (b) is deleted, and the ending is preceded by niss, delete the final s.
  101. (For example, äckern -> äck, ackers -> acker, armes -> arm, bedürfnissen -> bedürfnis)
  102. */
  103. if (index1 != 10000 && r1Index != -1) {
  104. if (index1 >= r1Index) {
  105. word = word.substring(0, index1);
  106. if (optionUsed1 == 'b') {
  107. if (word.search(/niss$/) != -1) {
  108. word = word.substring(0, word.length -1);
  109. }
  110. }
  111. }
  112. }
  113. /*
  114. Step 2:
  115. Search for the longest among the following suffixes,
  116. (a) en er est
  117. (b) st (preceded by a valid st-ending, itself preceded by at least 3 letters)
  118. */
  119. var a2Index = word.search(/(en|er|est)$/g);
  120. var b2Index = word.search(/(.{3}[bdfghklmnt]st)$/g);
  121. if (b2Index != -1) {
  122. b2Index += 4;
  123. }
  124. var index2 = 10000;
  125. var optionUsed2 = '';
  126. if (a2Index != -1 && a2Index < index2) {
  127. optionUsed2 = 'a';
  128. index2 = a2Index;
  129. }
  130. if (b2Index != -1 && b2Index < index2) {
  131. optionUsed2 = 'b';
  132. index2 = b2Index;
  133. }
  134. /*
  135. and delete if in R1.
  136. (For example, derbsten -> derbst by step 1, and derbst -> derb by step 2, since b is a valid st-ending, and is preceded by just 3 letters)
  137. */
  138. if (index2 != 10000 && r1Index != -1) {
  139. if (index2 >= r1Index) {
  140. word = word.substring(0, index2);
  141. }
  142. }
  143. /*
  144. Step 3: d-suffixes (*)
  145. Search for the longest among the following suffixes, and perform the action indicated.
  146. end ung
  147. delete if in R2
  148. if preceded by ig, delete if in R2 and not preceded by e
  149. ig ik isch
  150. delete if in R2 and not preceded by e
  151. lich heit
  152. delete if in R2
  153. if preceded by er or en, delete if in R1
  154. keit
  155. delete if in R2
  156. if preceded by lich or ig, delete if in R2
  157. */
  158. var a3Index = word.search(/(end|ung)$/g);
  159. var b3Index = word.search(/[^e](ig|ik|isch)$/g);
  160. var c3Index = word.search(/(lich|heit)$/g);
  161. var d3Index = word.search(/(keit)$/g);
  162. if (b3Index != -1) {
  163. b3Index ++;
  164. }
  165. var index3 = 10000;
  166. var optionUsed3 = '';
  167. if (a3Index != -1 && a3Index < index3) {
  168. optionUsed3 = 'a';
  169. index3 = a3Index;
  170. }
  171. if (b3Index != -1 && b3Index < index3) {
  172. optionUsed3 = 'b';
  173. index3 = b3Index;
  174. }
  175. if (c3Index != -1 && c3Index < index3) {
  176. optionUsed3 = 'c';
  177. index3 = c3Index;
  178. }
  179. if (d3Index != -1 && d3Index < index3) {
  180. optionUsed3 = 'd';
  181. index3 = d3Index;
  182. }
  183. if (index3 != 10000 && r2Index != -1) {
  184. if (index3 >= r2Index) {
  185. word = word.substring(0, index3);
  186. var optionIndex = -1;
  187. var optionSubsrt = '';
  188. if (optionUsed3 == 'a') {
  189. optionIndex = word.search(/[^e](ig)$/);
  190. if (optionIndex != -1) {
  191. optionIndex++;
  192. if (optionIndex >= r2Index) {
  193. word = word.substring(0, optionIndex);
  194. }
  195. }
  196. } else if (optionUsed3 == 'c') {
  197. optionIndex = word.search(/(er|en)$/);
  198. if (optionIndex != -1) {
  199. if (optionIndex >= r1Index) {
  200. word = word.substring(0, optionIndex);
  201. }
  202. }
  203. } else if (optionUsed3 == 'd') {
  204. optionIndex = word.search(/(lich|ig)$/);
  205. if (optionIndex != -1) {
  206. if (optionIndex >= r2Index) {
  207. word = word.substring(0, optionIndex);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. /*
  214. Finally,
  215. turn U and Y back into lower case, and remove the umlaut accent from a, o and u.
  216. */
  217. word = word.replace(/U/g, 'u');
  218. word = word.replace(/Y/g, 'y');
  219. word = word.replace(/ä/g, 'a');
  220. word = word.replace(/ö/g, 'o');
  221. word = word.replace(/ü/g, 'u');
  222. return word;
  223. };
  224. //}