1   package org.daisy.pipeline.nlp;
2   
3   import java.io.IOException;
4   
5   /**
6    * Categorize input strings. Two modes are possible:
7    * 
8    * - FULL_MATCH: try to categorize the whole input
9    * 
10   * - PREFIX_MATCH: try to categorize the longest prefix match
11   */
12  public abstract class TextCategorizer {
13  	protected MatchMode mMatchMode = MatchMode.FULL_MATCH;
14  
15  	public enum Category {
16  		COMMON,
17  		UNKNOWN,
18  		PROPER_NOUN,
19  		PROPER_NOUN_PLACE,
20  		PROPER_NOUN_NAME,
21  		ABBREVIATION,
22  		ACRONYM,
23  		PUNCTUATION,
24  		EMAIL_ADDR,
25  		WEB_LINK,
26  		QUANTITY,
27  		ORDINAL,
28  		TIME,
29  		QUOTE,
30  		DATE,
31  		SPACE,
32  		CURRENCY,
33  		DIMENSIONS,
34  		MEASURE,
35  		PHONE_NUMBER,
36  		RANGE,
37  		IDENTIFIER,
38  		NUMBERING_ITEM
39  	};
40  
41  	public static boolean isProperNoun(Category c) {
42  		switch (c) {
43  		case PROPER_NOUN:
44  		case PROPER_NOUN_PLACE:
45  		case PROPER_NOUN_NAME:
46  			return true;
47  		default:
48  			return false;
49  		}
50  	}
51  
52  	public static boolean isSpeakable(Category c) {
53  		switch (c) {
54  		case SPACE:
55  		case PUNCTUATION:
56  		case QUOTE:
57  			return false;
58  		default:
59  			return true;
60  		}
61  	}
62  
63  	public static class CategorizedWord {
64  		public Category category;
65  		public String word;
66  	}
67  
68  	public enum MatchMode {
69  		FULL_MATCH,
70  		PREFIX_MATCH
71  	}
72  
73  	public void init(MatchMode mode) throws IOException {
74  		mMatchMode = mode;
75  	}
76  
77  	/**
78  	 * Called after init()
79  	 */
80  	public abstract void compile();
81  
82  	public abstract void resetContext();
83  
84  	/**
85  	 * Can return false before returning true, but not the other way around.
86  	 */
87  	public abstract boolean threadsafe();
88  
89  	/**
90  	 * Both the fullcase and lower case versions are provided to prevent us from
91  	 * performing the conversion everytime.
92  	 * 
93  	 * @param fullcase fullcase version of the string to match
94  	 * @param lowercase lowercase version of the string to match
95  	 * @return null if the word cannot be categorized, otherwise the
96  	 *         CategorizedWord will the contain the longest match (i.e. a
97  	 *         substring of @param fullcase) depending on the match mode.
98  	 */
99  	public abstract CategorizedWord categorize(String fullcase, String lowercase);
100 }