Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/lt/analyzer/MpdlMorphAnalyzer.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children | 5df60f24e997 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.lt.analyzer; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.io.Reader; | |
6 import java.io.StringReader; | |
7 import java.util.ArrayList; | |
8 import java.util.HashSet; | |
9 import java.util.Hashtable; | |
10 import java.util.Set; | |
11 | |
12 import org.apache.lucene.analysis.Analyzer; | |
13 import org.apache.lucene.analysis.LowerCaseFilter; | |
14 import org.apache.lucene.analysis.StopFilter; | |
15 import org.apache.lucene.analysis.Token; | |
16 import org.apache.lucene.analysis.TokenStream; | |
17 import org.apache.lucene.analysis.WordlistLoader; | |
18 import org.apache.lucene.analysis.br.BrazilianAnalyzer; | |
19 import org.apache.lucene.analysis.cz.CzechAnalyzer; | |
20 import org.apache.lucene.analysis.de.GermanAnalyzer; | |
21 import org.apache.lucene.analysis.fr.FrenchAnalyzer; | |
22 import org.apache.lucene.analysis.nl.DutchAnalyzer; | |
23 import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
24 | |
25 import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants; | |
26 | |
27 /** | |
28 * Analyzer for specific languages. Supports an external list of stopwords (words that | |
29 * will not be indexed at all) and an external list of exclusions (word that will | |
30 * not be stemmed, but indexed). | |
31 * A default set of stopwords is used unless an alternative list is specified, the | |
32 * exclusion list is empty by default. | |
33 */ | |
34 public class MpdlMorphAnalyzer extends Analyzer { | |
35 protected String language = MpdlConstants.DEFAULT_LANGUAGE; | |
36 | |
37 /** | |
38 * Contains the stopwords used with the StopFilter. | |
39 */ | |
40 protected Set stopSet = new HashSet(); | |
41 | |
42 /** | |
43 * Contains words that should be indexed but not stemmed. | |
44 */ | |
45 protected Set exclusionSet = new HashSet(); | |
46 | |
47 /** | |
48 * Builds an analyzer with the stop words for the given language | |
49 * (<code>GERMAN_STOP_WORDS</code>). | |
50 */ | |
51 public MpdlMorphAnalyzer() { | |
52 String[] stopWords = getStopWords(language); // stopwords for the language | |
53 stopSet = StopFilter.makeStopSet(stopWords); | |
54 } | |
55 | |
56 /** | |
57 * Builds an analyzer with the given stop words. | |
58 */ | |
59 public MpdlMorphAnalyzer(String[] stopwords) { | |
60 stopSet = StopFilter.makeStopSet(stopwords); | |
61 } | |
62 | |
63 /** | |
64 * Builds an analyzer with the given stop words. | |
65 */ | |
66 public MpdlMorphAnalyzer(Hashtable stopwords) { | |
67 stopSet = new HashSet(stopwords.keySet()); | |
68 } | |
69 | |
70 /** | |
71 * Builds an analyzer with the given stop words. | |
72 */ | |
73 public MpdlMorphAnalyzer(File stopwords) throws IOException { | |
74 stopSet = WordlistLoader.getWordSet(stopwords); | |
75 } | |
76 | |
77 public String getLanguage() { | |
78 return language; | |
79 } | |
80 | |
81 protected void setLanguage(String lang) { | |
82 this.language = lang; | |
83 } | |
84 | |
85 /** | |
86 * Get stopwords for the language: fetch them from the open language analyzers for some languages | |
87 Taken from: http://www.perseus.tufts.edu/hopper/stopwords | |
88 # English: a, a's, able, about, above, according, accordingly, across, actually, after, afterwards, again, against, ain't, all, allow, allows, almost, alone, along, already, also, although, always, am, among, amongst, an, and, another, any, anybody, anyhow, anyone, anything, anyway, anyways, anywhere, apart, appear, appreciate, appropriate, are, aren't, around, as, aside, ask, asking, associated, at, available, away, awfully, b, be, became, because, become, becomes, becoming, been, before, beforehand, behind, being, believe, below, beside, besides, best, better, between, beyond, both, brief, but, by, c, c'mon, c's, came, can, can't, cannot, cant, cause, causes, certain, certainly, changes, clearly, co, com, come, comes, concerning, consequently, consider, considering, contain, containing, contains, corresponding, could, couldn't, course, currently, d, definitely, described, despite, did, didn't, different, do, does, doesn't, doing, don't, done, down, downwards, during, e, each, edu, eg, eight, either, else, elsewhere, enough, entirely, especially, et, etc, even, ever, every, everybody, everyone, everything, everywhere, ex, exactly, example, except, f, far, few, fifth, first, five, followed, following, follows, for, former, formerly, forth, four, from, further, furthermore, g, get, gets, getting, given, gives, go, goes, going, gone, got, gotten, greetings, h, had, hadn't, happens, hardly, has, hasn't, have, haven't, having, he, he's, hello, help, hence, her, here, here's, hereafter, hereby, herein, hereupon, hers, herself, hi, him, himself, his, hither, hopefully, how, howbeit, however, i, i'd, i'll, i'm, i've, ie, if, ignored, immediate, in, inasmuch, inc, indeed, indicate, indicated, indicates, inner, insofar, instead, into, inward, is, isn't, it, it'd, it'll, it's, its, itself, j, just, k, keep, keeps, kept, know, known, knows, l, last, lately, later, latter, latterly, least, less, lest, let, let's, like, liked, likely, little, look, looking, looks, ltd, m, mainly, many, may, maybe, me, mean, meanwhile, merely, might, more, moreover, most, mostly, much, must, my, myself, n, name, namely, nd, near, nearly, necessary, need, needs, neither, never, nevertheless, new, next, nine, no, nobody, non, none, noone, nor, normally, not, nothing, novel, now, nowhere, o, obviously, of, off, often, oh, ok, okay, old, on, once, one, ones, only, onto, or, other, others, otherwise, ought, our, ours, ourselves, out, outside, over, overall, own, p, particular, particularly, per, perhaps, placed, please, plus, possible, presumably, probably, provides, q, que, quite, qv, r, rather, rd, re, really, reasonably, regarding, regardless, regards, relatively, respectively, right, s, said, same, saw, say, saying, says, second, secondly, see, seeing, seem, seemed, seeming, seems, seen, self, selves, sensible, sent, serious, seriously, seven, several, shall, she, should, shouldn't, since, six, so, some, somebody, somehow, someone, something, sometime, sometimes, somewhat, somewhere, soon, sorry, specified, specify, specifying, still, sub, such, sup, sure, t, t's, take, taken, tell, tends, th, than, thank, thanks, thanx, that, that's, thats, the, their, theirs, them, themselves, then, thence, there, there's, thereafter, thereby, therefore, therein, theres, thereupon, these, they, they'd, they'll, they're, they've, think, third, this, thorough, thoroughly, those, though, three, through, throughout, thru, thus, to, together, too, took, toward, towards, tried, tries, truly, try, trying, twice, two, u, un, under, unfortunately, unless, unlikely, until, unto, up, upon, us, use, used, useful, uses, using, usually, uucp, v, value, various, very, via, viz, vs, w, want, wants, was, wasn't, way, we, we'd, we'll, we're, we've, welcome, well, went, were, weren't, what, what's, whatever, when, whence, whenever, where, where's, whereafter, whereas, whereby, wherein, whereupon, wherever, whether, which, while, whilst, whither, who, who's, whoever, whole, whom, whose, why, will, willing, wish, with, within, without, won't, wonder, would, wouldn't, x, y, yes, yet, you, you'd, you'll, you're, you've, your, yours, yourself, yourselves, z, zero | |
89 | |
90 # Greek: a)/llos, a)/n, a)/ra, a)ll', a)lla/, a)po/, au)to/s, d', dai/, dai/s, de/, dh/, dia/, e(autou=, e)/ti, e)a/n, e)gw/, e)k, e)mo/s, e)n, e)pi/, ei), ei)/mi, ei)mi/, ei)s, ga/r, ga^, ge, h(, h)/, kai/, kata/, me/n, meta/, mh/, o(, o(/de, o(/s, o(/stis, o(/ti, oi(, ou(/tws, ou(=tos, ou), ou)/te, ou)=n, ou)de/, ou)dei/s, ou)k, para/, peri/, pro/s, so/s, su/, su/n, ta/, te, th/n, th=s, th=|, ti, ti/, ti/s, tis, to/, to/n, toi/, toiou=tos, tou/s, tou=, tw=n, tw=|, u(mo/s, u(pe/r, u(po/, w(/ste, w(s, w)= | |
91 | |
92 # Latin: ab, ac, ad, adhic, aliqui, aliquis, an, ante, apud, at, atque, aut, autem, cum, cur, de, deinde, dum, ego, enim, ergo, es, est, et, etiam, etsi, ex, fio, haud, hic, iam, idem, igitur, ille, in, infra, inter, interim, ipse, is, ita, magis, modo, mox, nam, ne, nec, necque, neque, nisi, non, nos, o, ob, per, possum, post, pro, quae, quam, quare, qui, quia, quicumque, quidem, quilibet, quis, quisnam, quisquam, quisque, quisquis, quo, quoniam, sed, si, sic, sive, sub, sui, sum, super, suus, tam, tamen, trans, tu, tum, ubi, uel, uero, unus, ut | |
93 | |
94 # Italian: a, ad, agli, al, alcun, alcuno, all', alla, alle, allo, altra, altre, altri, altro, assai, avere, bene, c', ch', che, chi, ci, cio, co', col, come, con, cosi, cosi\, d', da, dal, dall', dalla, dalle, de, de', degli, dei, del, dell', della, delle, dello, di, duo, e, ed, egli, essere, et, gia, gia\, gli, gran, grande, i, il, in, io, l', la, le, li, lo, ma, maggior, maggiore, mai, mio, molto, ne, ne', nel, nell', nella, nelle, non, o, ogn', ogni, oue, ove, per, perche, piu, piu\, poco, poi, puo, qual, qualche, qualcun, qualcuno, quale, quanta, quante, quanti, quanto, quasi, quella, quelle, quelli, quello, questa, queste, questi, questo, qui, s', se, sempre, senza, si, sotto, su, sua, sue, sui, suo, tal, tanta, tante, tanti, tanto, tra, tre, tutta, tutte, tutti, tutto, un, una, uno, vn, vna, vno | |
95 | |
96 # German: aber, alle, als, also, am, an, andern, auch, auf, aus, bei, bey, bis, da, daher, das, dass, de, dem, den, der, des, die, diese, dieser, dieses, doch, durch, eben, ein, eine, einem, einen, einer, eines, er, es, fur, gegen, haben, hat, ihre, im, in, ist, kan, man, mehr, mit, nach, nicht, noch, nur, oder, ohne, sehr, sei, selbst, sey, sich, sie, sind, so, uber, um, und, unter, vgl, vom, von, weil, welche, wenn, werden, wie, wird, zu, zur | |
97 | |
98 # French: a, amp, au, auec, aussi, autre, autres, aux, bien, car, ce, ces, cette, ceux, chose, choses, comme, d', dans, de, des, deux, dire, dont, du, elle, elles, en, encore, est, estre, et, faire, fait, faut, force, grande, ie, il, ils, l', la, le, les, leur, leurs, lors, luy, mais, mesme, n', ne, nous, on, ont, or, ou, par, parce, pas, peut, plus, plusieurs, point, pour, pourquoy, puis, qu', quand, que, qui, quoy, sa, sans, se, ses, si, soit, son, sont, sur, tous, tout, toutes, vn, vne, y | |
99 * @param language | |
100 * @return stopwords | |
101 */ | |
102 public String[] getStopWords(String language) { | |
103 String[] stopwords = new String[0]; | |
104 if (language != null) { | |
105 if (language.equals("en")) | |
106 stopwords = StandardAnalyzer.STOP_WORDS; | |
107 else if(language.equals("br")) | |
108 stopwords = BrazilianAnalyzer.BRAZILIAN_STOP_WORDS; | |
109 else if(language.equals("cz")) | |
110 stopwords = CzechAnalyzer.CZECH_STOP_WORDS; | |
111 else if(language.equals("de")) | |
112 stopwords = GermanAnalyzer.GERMAN_STOP_WORDS; | |
113 else if(language.equals("fr")) | |
114 stopwords = FrenchAnalyzer.FRENCH_STOP_WORDS; | |
115 else if(language.equals("nl")) | |
116 stopwords = DutchAnalyzer.DUTCH_STOP_WORDS; | |
117 } | |
118 return stopwords; | |
119 } | |
120 | |
121 /** | |
122 * Builds an exclusionlist from an array of Strings. | |
123 */ | |
124 public void setStemExclusionTable(String[] exclusionlist) { | |
125 exclusionSet = StopFilter.makeStopSet(exclusionlist); | |
126 } | |
127 | |
128 /** | |
129 * Builds an exclusionlist from a Hashtable. | |
130 */ | |
131 public void setStemExclusionTable(Hashtable exclusionlist) { | |
132 exclusionSet = new HashSet(exclusionlist.keySet()); | |
133 } | |
134 | |
135 /** | |
136 * Builds an exclusionlist from the words contained in the given file. | |
137 */ | |
138 public void setStemExclusionTable(File exclusionlist) throws IOException { | |
139 exclusionSet = WordlistLoader.getWordSet(exclusionlist); | |
140 } | |
141 | |
142 /** | |
143 * Creates a TokenStream which tokenizes all the text in the provided Reader. | |
144 * | |
145 * @return A TokenStream build from a StandardTokenizer filtered with | |
146 * StandardFilter, LowerCaseFilter, StopFilter, DonatusStemFilter | |
147 */ | |
148 public TokenStream tokenStream(String fieldName, Reader reader) { | |
149 MpdlNormalizer mpdlNormalizer = new MpdlNormalizer(language); | |
150 TokenStream result = new MpdlTokenizer(reader, language, mpdlNormalizer); | |
151 result = new MpdlFilter(result); // filter to remove the hyphen in a token etc. | |
152 result = new LowerCaseFilter(result); | |
153 result = new StopFilter(result, stopSet); | |
154 result = new MpdlStemFilter(this, result, exclusionSet); | |
155 return result; | |
156 } | |
157 | |
158 public ArrayList<String> getToken(String inputString) { | |
159 ArrayList<String> token = new ArrayList<String>(); | |
160 try { | |
161 Reader reader = new StringReader(inputString); | |
162 MpdlNormalizer mpdlNormalizer = new MpdlNormalizer(language); | |
163 TokenStream result = new MpdlTokenizer(reader, language, mpdlNormalizer); | |
164 result = new MpdlFilter(result); // filter to remove the hyphen in a token etc. | |
165 result = new LowerCaseFilter(result); | |
166 result = new StopFilter(result, stopSet); | |
167 Token t = result.next(); | |
168 while (t != null) { | |
169 String currentToken = String.valueOf(t.termBuffer()); | |
170 token.add(currentToken); | |
171 t = result.next(); | |
172 } | |
173 } catch (IOException e) { | |
174 e.printStackTrace(); | |
175 } | |
176 return token; | |
177 } | |
178 | |
179 } |