@Test public void testNormalize() throws Exception { System.out.println("normalize"); TimexNormalizer instance = new TimexNormalizer(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(this.getClass().getResource("/time/timex-test.en-US.txt").toURI())), "UTF-8"))) { String line; int linen = 0; while ((line = reader.readLine()) != null) { linen++; if(!line.trim().isEmpty() && line.matches("[^\\|]+\\|[^\\|]+")){ System.out.println("testing (line "+linen+"): "+line); String [] test=line.trim().split("\\|"); System.out.println("expected: "+test[1]); String predicted=instance.normalize(test[0]); System.out.println("predicted: "+predicted); assertEquals(test[1], predicted); } } } TimexNormalizer instanceES = new TimexNormalizer(new Locale("es","ES")); }
public String normalize(String expr) { String norm_value = "default_norm"; try { if (expr == null) { throw new Exception("Input expression is null."); } expr = expr.trim(); if (expr.length() == 0) { throw new Exception("Input expression is empty."); } String normTextandPattern = timek.getNormTextandPattern(expr); if (normTextandPattern == null) { throw new Exception("Problem obtaining NormText and Pattern from: " + expr); } String[] normTextandPattern_arr = normTextandPattern.split("\\|"); String timex_text = normTextandPattern_arr[0]; String timex_pattern = normTextandPattern_arr[1]; if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { System.err.println("\n\ntimex:" + expr + " normtext:" + timex_text + " pattern:" + timex_pattern + "\nfound rules:"); } if (timex_pattern.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(timex_pattern); return applyRule(timex_text, timex_pattern, rule); } else { String left_cycle_pat = timex_pattern; String right_cycle_pat = timex_pattern; String left_cycle_text = timex_text; String right_cycle_text = timex_text; while (right_cycle_pat.split(" ").length > 2) { left_cycle_pat = left_cycle_pat.substring(left_cycle_pat.indexOf(' ') + 1); right_cycle_pat = right_cycle_pat.substring(0, right_cycle_pat.lastIndexOf(' ')); String med_cycle_pat = left_cycle_pat.substring(0, left_cycle_pat.lastIndexOf(' ')); left_cycle_text = left_cycle_text.substring(left_cycle_text.indexOf(' ') + 1); right_cycle_text = right_cycle_text.substring(0, right_cycle_text.lastIndexOf(' ')); String med_cycle_text = left_cycle_text.substring(0, left_cycle_text.lastIndexOf(' ')); if (left_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(left_cycle_pat); return applyRule(left_cycle_text, left_cycle_pat, rule); } else { if (right_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(right_cycle_pat); return applyRule(right_cycle_text, right_cycle_pat, rule); } else { if (med_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(med_cycle_pat); return applyRule(med_cycle_text, med_cycle_pat, rule); } } } } } norm_value = "NO RULE FOUND FOR: " + normTextandPattern; } catch (Exception e) { System.err.println("Errors found (" + this.getClass().getSimpleName() + "):\n\t" + e.getMessage() + "\n"); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } System.exit(1); } return norm_value; }
TimexNormalizer { public String normalize(String expr) { String norm_value = "default_norm"; try { if (expr == null) { throw new Exception("Input expression is null."); } expr = expr.trim(); if (expr.length() == 0) { throw new Exception("Input expression is empty."); } String normTextandPattern = timek.getNormTextandPattern(expr); if (normTextandPattern == null) { throw new Exception("Problem obtaining NormText and Pattern from: " + expr); } String[] normTextandPattern_arr = normTextandPattern.split("\\|"); String timex_text = normTextandPattern_arr[0]; String timex_pattern = normTextandPattern_arr[1]; if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { System.err.println("\n\ntimex:" + expr + " normtext:" + timex_text + " pattern:" + timex_pattern + "\nfound rules:"); } if (timex_pattern.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(timex_pattern); return applyRule(timex_text, timex_pattern, rule); } else { String left_cycle_pat = timex_pattern; String right_cycle_pat = timex_pattern; String left_cycle_text = timex_text; String right_cycle_text = timex_text; while (right_cycle_pat.split(" ").length > 2) { left_cycle_pat = left_cycle_pat.substring(left_cycle_pat.indexOf(' ') + 1); right_cycle_pat = right_cycle_pat.substring(0, right_cycle_pat.lastIndexOf(' ')); String med_cycle_pat = left_cycle_pat.substring(0, left_cycle_pat.lastIndexOf(' ')); left_cycle_text = left_cycle_text.substring(left_cycle_text.indexOf(' ') + 1); right_cycle_text = right_cycle_text.substring(0, right_cycle_text.lastIndexOf(' ')); String med_cycle_text = left_cycle_text.substring(0, left_cycle_text.lastIndexOf(' ')); if (left_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(left_cycle_pat); return applyRule(left_cycle_text, left_cycle_pat, rule); } else { if (right_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(right_cycle_pat); return applyRule(right_cycle_text, right_cycle_pat, rule); } else { if (med_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(med_cycle_pat); return applyRule(med_cycle_text, med_cycle_pat, rule); } } } } } norm_value = "NO RULE FOUND FOR: " + normTextandPattern; } catch (Exception e) { System.err.println("Errors found (" + this.getClass().getSimpleName() + "):\n\t" + e.getMessage() + "\n"); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } System.exit(1); } return norm_value; } }
TimexNormalizer { public String normalize(String expr) { String norm_value = "default_norm"; try { if (expr == null) { throw new Exception("Input expression is null."); } expr = expr.trim(); if (expr.length() == 0) { throw new Exception("Input expression is empty."); } String normTextandPattern = timek.getNormTextandPattern(expr); if (normTextandPattern == null) { throw new Exception("Problem obtaining NormText and Pattern from: " + expr); } String[] normTextandPattern_arr = normTextandPattern.split("\\|"); String timex_text = normTextandPattern_arr[0]; String timex_pattern = normTextandPattern_arr[1]; if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { System.err.println("\n\ntimex:" + expr + " normtext:" + timex_text + " pattern:" + timex_pattern + "\nfound rules:"); } if (timex_pattern.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(timex_pattern); return applyRule(timex_text, timex_pattern, rule); } else { String left_cycle_pat = timex_pattern; String right_cycle_pat = timex_pattern; String left_cycle_text = timex_text; String right_cycle_text = timex_text; while (right_cycle_pat.split(" ").length > 2) { left_cycle_pat = left_cycle_pat.substring(left_cycle_pat.indexOf(' ') + 1); right_cycle_pat = right_cycle_pat.substring(0, right_cycle_pat.lastIndexOf(' ')); String med_cycle_pat = left_cycle_pat.substring(0, left_cycle_pat.lastIndexOf(' ')); left_cycle_text = left_cycle_text.substring(left_cycle_text.indexOf(' ') + 1); right_cycle_text = right_cycle_text.substring(0, right_cycle_text.lastIndexOf(' ')); String med_cycle_text = left_cycle_text.substring(0, left_cycle_text.lastIndexOf(' ')); if (left_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(left_cycle_pat); return applyRule(left_cycle_text, left_cycle_pat, rule); } else { if (right_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(right_cycle_pat); return applyRule(right_cycle_text, right_cycle_pat, rule); } else { if (med_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(med_cycle_pat); return applyRule(med_cycle_text, med_cycle_pat, rule); } } } } } norm_value = "NO RULE FOUND FOR: " + normTextandPattern; } catch (Exception e) { System.err.println("Errors found (" + this.getClass().getSimpleName() + "):\n\t" + e.getMessage() + "\n"); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } System.exit(1); } return norm_value; } TimexNormalizer();  TimexNormalizer(Locale l); }
TimexNormalizer { public String normalize(String expr) { String norm_value = "default_norm"; try { if (expr == null) { throw new Exception("Input expression is null."); } expr = expr.trim(); if (expr.length() == 0) { throw new Exception("Input expression is empty."); } String normTextandPattern = timek.getNormTextandPattern(expr); if (normTextandPattern == null) { throw new Exception("Problem obtaining NormText and Pattern from: " + expr); } String[] normTextandPattern_arr = normTextandPattern.split("\\|"); String timex_text = normTextandPattern_arr[0]; String timex_pattern = normTextandPattern_arr[1]; if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { System.err.println("\n\ntimex:" + expr + " normtext:" + timex_text + " pattern:" + timex_pattern + "\nfound rules:"); } if (timex_pattern.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(timex_pattern); return applyRule(timex_text, timex_pattern, rule); } else { String left_cycle_pat = timex_pattern; String right_cycle_pat = timex_pattern; String left_cycle_text = timex_text; String right_cycle_text = timex_text; while (right_cycle_pat.split(" ").length > 2) { left_cycle_pat = left_cycle_pat.substring(left_cycle_pat.indexOf(' ') + 1); right_cycle_pat = right_cycle_pat.substring(0, right_cycle_pat.lastIndexOf(' ')); String med_cycle_pat = left_cycle_pat.substring(0, left_cycle_pat.lastIndexOf(' ')); left_cycle_text = left_cycle_text.substring(left_cycle_text.indexOf(' ') + 1); right_cycle_text = right_cycle_text.substring(0, right_cycle_text.lastIndexOf(' ')); String med_cycle_text = left_cycle_text.substring(0, left_cycle_text.lastIndexOf(' ')); if (left_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(left_cycle_pat); return applyRule(left_cycle_text, left_cycle_pat, rule); } else { if (right_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(right_cycle_pat); return applyRule(right_cycle_text, right_cycle_pat, rule); } else { if (med_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(med_cycle_pat); return applyRule(med_cycle_text, med_cycle_pat, rule); } } } } } norm_value = "NO RULE FOUND FOR: " + normTextandPattern; } catch (Exception e) { System.err.println("Errors found (" + this.getClass().getSimpleName() + "):\n\t" + e.getMessage() + "\n"); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } System.exit(1); } return norm_value; } TimexNormalizer();  TimexNormalizer(Locale l); String normalize(String expr); String applyRule(String timex_text, String timex_pattern, String rule); }
TimexNormalizer { public String normalize(String expr) { String norm_value = "default_norm"; try { if (expr == null) { throw new Exception("Input expression is null."); } expr = expr.trim(); if (expr.length() == 0) { throw new Exception("Input expression is empty."); } String normTextandPattern = timek.getNormTextandPattern(expr); if (normTextandPattern == null) { throw new Exception("Problem obtaining NormText and Pattern from: " + expr); } String[] normTextandPattern_arr = normTextandPattern.split("\\|"); String timex_text = normTextandPattern_arr[0]; String timex_pattern = normTextandPattern_arr[1]; if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { System.err.println("\n\ntimex:" + expr + " normtext:" + timex_text + " pattern:" + timex_pattern + "\nfound rules:"); } if (timex_pattern.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(timex_pattern); return applyRule(timex_text, timex_pattern, rule); } else { String left_cycle_pat = timex_pattern; String right_cycle_pat = timex_pattern; String left_cycle_text = timex_text; String right_cycle_text = timex_text; while (right_cycle_pat.split(" ").length > 2) { left_cycle_pat = left_cycle_pat.substring(left_cycle_pat.indexOf(' ') + 1); right_cycle_pat = right_cycle_pat.substring(0, right_cycle_pat.lastIndexOf(' ')); String med_cycle_pat = left_cycle_pat.substring(0, left_cycle_pat.lastIndexOf(' ')); left_cycle_text = left_cycle_text.substring(left_cycle_text.indexOf(' ') + 1); right_cycle_text = right_cycle_text.substring(0, right_cycle_text.lastIndexOf(' ')); String med_cycle_text = left_cycle_text.substring(0, left_cycle_text.lastIndexOf(' ')); if (left_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(left_cycle_pat); return applyRule(left_cycle_text, left_cycle_pat, rule); } else { if (right_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(right_cycle_pat); return applyRule(right_cycle_text, right_cycle_pat, rule); } else { if (med_cycle_pat.matches("(?i)" + rules.getRE())) { String rule = rules.getMapValue(med_cycle_pat); return applyRule(med_cycle_text, med_cycle_pat, rule); } } } } } norm_value = "NO RULE FOUND FOR: " + normTextandPattern; } catch (Exception e) { System.err.println("Errors found (" + this.getClass().getSimpleName() + "):\n\t" + e.getMessage() + "\n"); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } System.exit(1); } return norm_value; } TimexNormalizer();  TimexNormalizer(Locale l); String normalize(String expr); String applyRule(String timex_text, String timex_pattern, String rule);  }