1 package uk.ac.ebi.intenz.tools.sib.translator.rules;
2
3 import org.apache.log4j.Logger;
4 import uk.ac.ebi.intenz.tools.sib.translator.XCharsASCIITranslator;
5
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.*;
9
10
11
12
13
14
15
16
17
18
19 public class UnorderedRules implements RuleGroup {
20
21 private static final Logger LOGGER = Logger.getLogger(UnorderedRules.class);
22
23 private static final UnorderedRules INSTANCE = new UnorderedRules();
24
25 private Map reverseRules;
26
27 private Map rules;
28
29 private Map spellingRules;
30
31
32
33
34 private UnorderedRules() {
35 InputStream stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("unorderedRules.txt");
36
37 rules = new HashMap();
38 reverseRules = new HashMap();
39 spellingRules = new HashMap();
40 try {
41
42
43 PropertyResourceBundle prb = new PropertyResourceBundle(stream);
44 Enumeration en = prb.getKeys();
45 while (en.hasMoreElements()) {
46 String key = (String) en.nextElement();
47 String value = prb.getString(key);
48 rules.put(key, value);
49 }
50 stream.close();
51
52
53
54 stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("unorderedRules_rev.txt");
55 prb = new PropertyResourceBundle(stream);
56 en = prb.getKeys();
57 while (en.hasMoreElements()) {
58 String key = (String) en.nextElement();
59 String value = prb.getString(key);
60 reverseRules.put(key, value);
61 }
62
63 stream = XCharsASCIITranslator.class.getClassLoader().getResourceAsStream("spellingRules.txt");
64 prb = new PropertyResourceBundle(stream);
65 en = prb.getKeys();
66 while (en.hasMoreElements()) {
67 String key = (String) en.nextElement();
68 String value = prb.getString(key);
69 spellingRules.put(key, value);
70 }
71
72 } catch (IOException e) {
73 LOGGER.error("Error while initialising 'UnorderedRules' class.", e);
74 } finally {
75 try {
76 stream.close();
77 } catch (IOException e) {
78 LOGGER.error("Error while closing input stream.", e);
79 }
80 }
81 }
82
83
84
85
86
87
88
89
90 public static UnorderedRules getInstance() {
91 return INSTANCE;
92 }
93
94
95
96
97
98
99
100
101 public String applyRules(String text) {
102 if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
103 text = translate(text, rules);
104 return text;
105 }
106
107
108
109
110
111
112
113
114
115
116
117 public String reverseRules(String text) {
118 if (text == null) throw new NullPointerException("Parameter 'text' must not be null.");
119 text = translate(text, reverseRules);
120 text = translate(text, spellingRules);
121
122 return text;
123 }
124
125
126
127
128
129
130
131
132 private String translate(String text, Map rules) {
133
134 Set rulePatterns = rules.keySet();
135 for (Iterator it = rulePatterns.iterator(); it.hasNext();) {
136 String pattern = (String) it.next();
137 String replacement = (String) rules.get(pattern);
138 text = text.replaceAll(pattern, replacement);
139 }
140
141 return text;
142 }
143
144 }