PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/hotspot/test/compiler/7070134/Stemmer.java

#
Java | 433 lines | 244 code | 48 blank | 141 comment | 138 complexity | c9cc52d095fa416c68aea1ebeda45ff8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /**
  2. * @test
  3. * @bug 7070134
  4. * @summary Hotspot crashes with sigsegv from PorterStemmer
  5. *
  6. * @run shell Test7070134.sh
  7. */
  8. /*
  9. Porter stemmer in Java. The original paper is in
  10. Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
  11. no. 3, pp 130-137,
  12. See also http://www.tartarus.org/~martin/PorterStemmer
  13. History:
  14. Release 1
  15. Bug 1 (reported by Gonzalo Parra 16/10/99) fixed as marked below.
  16. The words 'aed', 'eed', 'oed' leave k at 'a' for step 3, and b[k-1]
  17. is then out outside the bounds of b.
  18. Release 2
  19. Similarly,
  20. Bug 2 (reported by Steve Dyrdahl 22/2/00) fixed as marked below.
  21. 'ion' by itself leaves j = -1 in the test for 'ion' in step 5, and
  22. b[j] is then outside the bounds of b.
  23. Release 3
  24. Considerably revised 4/9/00 in the light of many helpful suggestions
  25. from Brian Goetz of Quiotix Corporation (brian@quiotix.com).
  26. Release 4
  27. */
  28. import java.io.*;
  29. /**
  30. * Stemmer, implementing the Porter Stemming Algorithm
  31. *
  32. * The Stemmer class transforms a word into its root form. The input
  33. * word can be provided a character at time (by calling add()), or at once
  34. * by calling one of the various stem(something) methods.
  35. */
  36. class Stemmer
  37. { private char[] b;
  38. private int i, /* offset into b */
  39. i_end, /* offset to end of stemmed word */
  40. j, k;
  41. private static final int INC = 50;
  42. /* unit of size whereby b is increased */
  43. public Stemmer()
  44. { b = new char[INC];
  45. i = 0;
  46. i_end = 0;
  47. }
  48. /**
  49. * Add a character to the word being stemmed. When you are finished
  50. * adding characters, you can call stem(void) to stem the word.
  51. */
  52. public void add(char ch)
  53. { if (i == b.length)
  54. { char[] new_b = new char[i+INC];
  55. for (int c = 0; c < i; c++) new_b[c] = b[c];
  56. b = new_b;
  57. }
  58. b[i++] = ch;
  59. }
  60. /** Adds wLen characters to the word being stemmed contained in a portion
  61. * of a char[] array. This is like repeated calls of add(char ch), but
  62. * faster.
  63. */
  64. public void add(char[] w, int wLen)
  65. { if (i+wLen >= b.length)
  66. { char[] new_b = new char[i+wLen+INC];
  67. for (int c = 0; c < i; c++) new_b[c] = b[c];
  68. b = new_b;
  69. }
  70. for (int c = 0; c < wLen; c++) b[i++] = w[c];
  71. }
  72. /**
  73. * After a word has been stemmed, it can be retrieved by toString(),
  74. * or a reference to the internal buffer can be retrieved by getResultBuffer
  75. * and getResultLength (which is generally more efficient.)
  76. */
  77. public String toString() { return new String(b,0,i_end); }
  78. /**
  79. * Returns the length of the word resulting from the stemming process.
  80. */
  81. public int getResultLength() { return i_end; }
  82. /**
  83. * Returns a reference to a character buffer containing the results of
  84. * the stemming process. You also need to consult getResultLength()
  85. * to determine the length of the result.
  86. */
  87. public char[] getResultBuffer() { return b; }
  88. /* cons(i) is true <=> b[i] is a consonant. */
  89. private final boolean cons(int i)
  90. { switch (b[i])
  91. { case 'a': case 'e': case 'i': case 'o': case 'u': return false;
  92. case 'y': return (i==0) ? true : !cons(i-1);
  93. default: return true;
  94. }
  95. }
  96. /* m() measures the number of consonant sequences between 0 and j. if c is
  97. a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
  98. presence,
  99. <c><v> gives 0
  100. <c>vc<v> gives 1
  101. <c>vcvc<v> gives 2
  102. <c>vcvcvc<v> gives 3
  103. ....
  104. */
  105. private final int m()
  106. { int n = 0;
  107. int i = 0;
  108. while(true)
  109. { if (i > j) return n;
  110. if (! cons(i)) break; i++;
  111. }
  112. i++;
  113. while(true)
  114. { while(true)
  115. { if (i > j) return n;
  116. if (cons(i)) break;
  117. i++;
  118. }
  119. i++;
  120. n++;
  121. while(true)
  122. { if (i > j) return n;
  123. if (! cons(i)) break;
  124. i++;
  125. }
  126. i++;
  127. }
  128. }
  129. /* vowelinstem() is true <=> 0,...j contains a vowel */
  130. private final boolean vowelinstem()
  131. { int i; for (i = 0; i <= j; i++) if (! cons(i)) return true;
  132. return false;
  133. }
  134. /* doublec(j) is true <=> j,(j-1) contain a double consonant. */
  135. private final boolean doublec(int j)
  136. { if (j < 1) return false;
  137. if (b[j] != b[j-1]) return false;
  138. return cons(j);
  139. }
  140. /* cvc(i) is true <=> i-2,i-1,i has the form consonant - vowel - consonant
  141. and also if the second c is not w,x or y. this is used when trying to
  142. restore an e at the end of a short word. e.g.
  143. cav(e), lov(e), hop(e), crim(e), but
  144. snow, box, tray.
  145. */
  146. private final boolean cvc(int i)
  147. { if (i < 2 || !cons(i) || cons(i-1) || !cons(i-2)) return false;
  148. { int ch = b[i];
  149. if (ch == 'w' || ch == 'x' || ch == 'y') return false;
  150. }
  151. return true;
  152. }
  153. private final boolean ends(String s)
  154. { int l = s.length();
  155. int o = k-l+1;
  156. if (o < 0) return false;
  157. for (int i = 0; i < l; i++) if (b[o+i] != s.charAt(i)) return false;
  158. j = k-l;
  159. return true;
  160. }
  161. /* setto(s) sets (j+1),...k to the characters in the string s, readjusting
  162. k. */
  163. private final void setto(String s)
  164. { int l = s.length();
  165. int o = j+1;
  166. for (int i = 0; i < l; i++) b[o+i] = s.charAt(i);
  167. k = j+l;
  168. }
  169. /* r(s) is used further down. */
  170. private final void r(String s) { if (m() > 0) setto(s); }
  171. /* step1() gets rid of plurals and -ed or -ing. e.g.
  172. caresses -> caress
  173. ponies -> poni
  174. ties -> ti
  175. caress -> caress
  176. cats -> cat
  177. feed -> feed
  178. agreed -> agree
  179. disabled -> disable
  180. matting -> mat
  181. mating -> mate
  182. meeting -> meet
  183. milling -> mill
  184. messing -> mess
  185. meetings -> meet
  186. */
  187. private final void step1()
  188. { if (b[k] == 's')
  189. { if (ends("sses")) k -= 2; else
  190. if (ends("ies")) setto("i"); else
  191. if (b[k-1] != 's') k--;
  192. }
  193. if (ends("eed")) { if (m() > 0) k--; } else
  194. if ((ends("ed") || ends("ing")) && vowelinstem())
  195. { k = j;
  196. if (ends("at")) setto("ate"); else
  197. if (ends("bl")) setto("ble"); else
  198. if (ends("iz")) setto("ize"); else
  199. if (doublec(k))
  200. { k--;
  201. { int ch = b[k];
  202. if (ch == 'l' || ch == 's' || ch == 'z') k++;
  203. }
  204. }
  205. else if (m() == 1 && cvc(k)) setto("e");
  206. }
  207. }
  208. /* step2() turns terminal y to i when there is another vowel in the stem. */
  209. private final void step2() { if (ends("y") && vowelinstem()) b[k] = 'i'; }
  210. /* step3() maps double suffices to single ones. so -ization ( = -ize plus
  211. -ation) maps to -ize etc. note that the string before the suffix must give
  212. m() > 0. */
  213. private final void step3() { if (k == 0) return; /* For Bug 1 */ switch (b[k-1])
  214. {
  215. case 'a': if (ends("ational")) { r("ate"); break; }
  216. if (ends("tional")) { r("tion"); break; }
  217. break;
  218. case 'c': if (ends("enci")) { r("ence"); break; }
  219. if (ends("anci")) { r("ance"); break; }
  220. break;
  221. case 'e': if (ends("izer")) { r("ize"); break; }
  222. break;
  223. case 'l': if (ends("bli")) { r("ble"); break; }
  224. if (ends("alli")) { r("al"); break; }
  225. if (ends("entli")) { r("ent"); break; }
  226. if (ends("eli")) { r("e"); break; }
  227. if (ends("ousli")) { r("ous"); break; }
  228. break;
  229. case 'o': if (ends("ization")) { r("ize"); break; }
  230. if (ends("ation")) { r("ate"); break; }
  231. if (ends("ator")) { r("ate"); break; }
  232. break;
  233. case 's': if (ends("alism")) { r("al"); break; }
  234. if (ends("iveness")) { r("ive"); break; }
  235. if (ends("fulness")) { r("ful"); break; }
  236. if (ends("ousness")) { r("ous"); break; }
  237. break;
  238. case 't': if (ends("aliti")) { r("al"); break; }
  239. if (ends("iviti")) { r("ive"); break; }
  240. if (ends("biliti")) { r("ble"); break; }
  241. break;
  242. case 'g': if (ends("logi")) { r("log"); break; }
  243. } }
  244. /* step4() deals with -ic-, -full, -ness etc. similar strategy to step3. */
  245. private final void step4() { switch (b[k])
  246. {
  247. case 'e': if (ends("icate")) { r("ic"); break; }
  248. if (ends("ative")) { r(""); break; }
  249. if (ends("alize")) { r("al"); break; }
  250. break;
  251. case 'i': if (ends("iciti")) { r("ic"); break; }
  252. break;
  253. case 'l': if (ends("ical")) { r("ic"); break; }
  254. if (ends("ful")) { r(""); break; }
  255. break;
  256. case 's': if (ends("ness")) { r(""); break; }
  257. break;
  258. } }
  259. /* step5() takes off -ant, -ence etc., in context <c>vcvc<v>. */
  260. private final void step5()
  261. { if (k == 0) return; /* for Bug 1 */ switch (b[k-1])
  262. { case 'a': if (ends("al")) break; return;
  263. case 'c': if (ends("ance")) break;
  264. if (ends("ence")) break; return;
  265. case 'e': if (ends("er")) break; return;
  266. case 'i': if (ends("ic")) break; return;
  267. case 'l': if (ends("able")) break;
  268. if (ends("ible")) break; return;
  269. case 'n': if (ends("ant")) break;
  270. if (ends("ement")) break;
  271. if (ends("ment")) break;
  272. /* element etc. not stripped before the m */
  273. if (ends("ent")) break; return;
  274. case 'o': if (ends("ion") && j >= 0 && (b[j] == 's' || b[j] == 't')) break;
  275. /* j >= 0 fixes Bug 2 */
  276. if (ends("ou")) break; return;
  277. /* takes care of -ous */
  278. case 's': if (ends("ism")) break; return;
  279. case 't': if (ends("ate")) break;
  280. if (ends("iti")) break; return;
  281. case 'u': if (ends("ous")) break; return;
  282. case 'v': if (ends("ive")) break; return;
  283. case 'z': if (ends("ize")) break; return;
  284. default: return;
  285. }
  286. if (m() > 1) k = j;
  287. }
  288. /* step6() removes a final -e if m() > 1. */
  289. private final void step6()
  290. { j = k;
  291. if (b[k] == 'e')
  292. { int a = m();
  293. if (a > 1 || a == 1 && !cvc(k-1)) k--;
  294. }
  295. if (b[k] == 'l' && doublec(k) && m() > 1) k--;
  296. }
  297. /** Stem the word placed into the Stemmer buffer through calls to add().
  298. * Returns true if the stemming process resulted in a word different
  299. * from the input. You can retrieve the result with
  300. * getResultLength()/getResultBuffer() or toString().
  301. */
  302. public void stem()
  303. { k = i - 1;
  304. if (k > 1) { step1(); step2(); step3(); step4(); step5(); step6(); }
  305. i_end = k+1; i = 0;
  306. }
  307. /** Test program for demonstrating the Stemmer. It reads text from a
  308. * a list of files, stems each word, and writes the result to standard
  309. * output. Note that the word stemmed is expected to be in lower case:
  310. * forcing lower case must be done outside the Stemmer class.
  311. * Usage: Stemmer file-name file-name ...
  312. */
  313. public static void main(String[] args)
  314. {
  315. char[] w = new char[501];
  316. Stemmer s = new Stemmer();
  317. for (int i = 0; i < args.length; i++)
  318. try
  319. {
  320. FileInputStream in = new FileInputStream(args[i]);
  321. try
  322. { while(true)
  323. { int ch = in.read();
  324. if (Character.isLetter((char) ch))
  325. {
  326. int j = 0;
  327. while(true)
  328. { ch = Character.toLowerCase((char) ch);
  329. w[j] = (char) ch;
  330. if (j < 500) j++;
  331. ch = in.read();
  332. if (!Character.isLetter((char) ch))
  333. {
  334. /* to test add(char ch) */
  335. for (int c = 0; c < j; c++) s.add(w[c]);
  336. /* or, to test add(char[] w, int j) */
  337. /* s.add(w, j); */
  338. s.stem();
  339. { String u;
  340. /* and now, to test toString() : */
  341. u = s.toString();
  342. /* to test getResultBuffer(), getResultLength() : */
  343. /* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */
  344. System.out.print(u);
  345. }
  346. break;
  347. }
  348. }
  349. }
  350. if (ch < 0) break;
  351. System.out.print((char)ch);
  352. }
  353. }
  354. catch (IOException e)
  355. { System.out.println("error reading " + args[i]);
  356. break;
  357. }
  358. }
  359. catch (FileNotFoundException e)
  360. { System.out.println("file " + args[i] + " not found");
  361. break;
  362. }
  363. }
  364. }