1 package org.daisy.pipeline.nlp.impl.matchrules;
2
3 import java.math.BigInteger;
4
5 import org.daisy.pipeline.nlp.TextCategorizer.Category;
6 import org.daisy.pipeline.nlp.TextCategorizer.MatchMode;
7 import org.daisy.pipeline.nlp.impl.MatchRule;
8
9
10
11
12 public class NumberRangeMatchRule extends MatchRule {
13
14 public NumberRangeMatchRule(Category category, int priority, MatchMode matchMode) {
15 super(category, priority, false, matchMode);
16 }
17
18 @Override
19 protected String match(String input) {
20 if (input.charAt(0) <= '0' || input.charAt(0) > '9')
21 return null;
22 int k = 1;
23 for (; k < input.length() && input.charAt(k) >= '0' && input.charAt(k) <= '9'; ++k);
24 if (k == input.length() || input.charAt(k) != '-')
25 return null;
26 int prevk = k++;
27 if (k >= input.length() || input.charAt(k) <= '0' || input.charAt(k) > '9')
28 return null;
29 for (++k; k < input.length() && input.charAt(k) >= '0' && input.charAt(k) <= '9'; ++k);
30 if (k == prevk + 2
31 || !lowerThan(input.substring(0, prevk), input.substring(prevk + 1, k))
32 || (mMatchMode == MatchMode.FULL_MATCH && k != input.length()))
33 return null;
34
35 return input.substring(0, k);
36 }
37
38 private static boolean lowerThan(String x, String y){
39 return (new BigInteger(x).compareTo(new BigInteger(y)) == -1);
40 }
41
42 @Override
43 public boolean threadsafe() {
44 return true;
45 }
46 }