1   package org.daisy.pipeline.nlp.impl;
2   
3   import org.daisy.pipeline.nlp.TextCategorizer.CategorizedWord;
4   import org.daisy.pipeline.nlp.TextCategorizer.Category;
5   import org.daisy.pipeline.nlp.TextCategorizer.MatchMode;
6   
7   /**
8    * Mapping between a string matching method (e.g. some regex) and a word
9    * category, associated to a priority so as to rank the match rules.
10   */
11  public abstract class MatchRule {
12  	private final int mPriority;
13  	private final Category mCategory;
14  	protected final MatchMode mMatchMode;
15  	protected final boolean mCaseSensitive;
16  
17  	/**
18  	 * 
19  	 * @param category the classification applied to the words that match the
20  	 *            rule
21  	 * @param priority allows us to select a rule when several rules match the
22  	 *            word. High value means it will be chosen over lower ones. When
23  	 *            two priorities are equal, the one that returns the longest
24  	 *            match is chosen.
25  	 */
26  	public MatchRule(Category category, int priority, boolean caseSensitive,
27  	        MatchMode matchMode) {
28  		mCategory = category;
29  		mPriority = priority;
30  		mCaseSensitive = caseSensitive;
31  		mMatchMode = matchMode;
32  	}
33  
34  	public int getPriority() {
35  		return mPriority;
36  	}
37  
38  	public CategorizedWord match(String input, String lowerCaseInput) {
39  		String res;
40  		if (mCaseSensitive)
41  			res = match(input);
42  		else
43  			res = match(lowerCaseInput);
44  		if (res == null)
45  			return null;
46  		CategorizedWord cw = new CategorizedWord();
47  		cw.category = mCategory;
48  		cw.word = res;
49  		return cw;
50  	}
51  
52  	protected abstract String match(String input);
53  
54  	/**
55  	 * Can return false before returning true, but not the other way around.
56  	 */
57  	public abstract boolean threadsafe();
58  }