DateParser extends AbstractParser { public StringBuilder trainingExtraction(List<String> inputs) { StringBuilder buffer = new StringBuilder(); try { if (inputs == null) return null; if (inputs.size() == 0) return null; List<String> tokenizations = null; List<String> dateBlocks = new ArrayList<String>(); for (String input : inputs) { if (input == null) continue; tokenizations = analyzer.tokenize(input); if (tokenizations.size() == 0) return null; for(String tok : tokenizations) { if (tok.equals("\n")) { dateBlocks.add("@newline"); } else if (!tok.equals(" ")) { dateBlocks.add(tok + " <date>"); } } dateBlocks.add("\n"); } String headerDate = FeaturesVectorDate.addFeaturesDate(dateBlocks); String res = label(headerDate); StringTokenizer st2 = new StringTokenizer(res, "\n"); String lastTag = null; boolean tagClosed = false; int q = 0; boolean addSpace; boolean hasYear = false; boolean hasMonth = false; boolean hasDay = false; String lastTag0; String currentTag0; boolean start = true; while (st2.hasMoreTokens()) { String line = st2.nextToken(); addSpace = false; if ((line.trim().length() == 0)) { buffer.append("</date>\n"); hasYear = false; hasMonth = false; hasDay = false; buffer.append("\t<date>"); continue; } else { String theTok = tokenizations.get(q); while (theTok.equals(" ")) { addSpace = true; q++; theTok = tokenizations.get(q); } q++; } StringTokenizer st3 = new StringTokenizer(line, "\t"); int ll = st3.countTokens(); int i = 0; String s1 = null; String s2 = null; while (st3.hasMoreTokens()) { String s = st3.nextToken().trim(); if (i == 0) { s2 = TextUtilities.HTMLEncode(s); } else if (i == ll - 1) { s1 = s; } i++; } if (start && (s1 != null)) { buffer.append("\t<date>"); start = false; } lastTag0 = null; if (lastTag != null) { if (lastTag.startsWith("I-")) { lastTag0 = lastTag.substring(2, lastTag.length()); } else { lastTag0 = lastTag; } } currentTag0 = null; if (s1 != null) { if (s1.startsWith("I-")) { currentTag0 = s1.substring(2, s1.length()); } else { currentTag0 = s1; } } tagClosed = lastTag0 != null && testClosingTag(buffer, currentTag0, lastTag0); String output = writeField(s1, lastTag0, s2, "<day>", "<day>", addSpace, 0); if (output != null) { if (lastTag0 != null) { if (hasDay && !lastTag0.equals("<day>")) { buffer.append("</date>\n"); hasYear = false; hasMonth = false; buffer.append("\t<date>"); } } hasDay = true; buffer.append(output); lastTag = s1; continue; } else { output = writeField(s1, lastTag0, s2, "<other>", "<other>", addSpace, 0); } if (output == null) { output = writeField(s1, lastTag0, s2, "<month>", "<month>", addSpace, 0); } else { buffer.append(output); lastTag = s1; continue; } if (output == null) { output = writeField(s1, lastTag0, s2, "<year>", "<year>", addSpace, 0); } else { if (lastTag0 != null) { if (hasMonth && !lastTag0.equals("<month>")) { buffer.append("</date>\n"); hasYear = false; hasDay = false; buffer.append("\t<date>"); } } buffer.append(output); hasMonth = true; lastTag = s1; continue; } if (output != null) { if (lastTag0 != null) { if (hasYear && !lastTag0.equals("<year>")) { buffer.append("</date>\n"); hasDay = false; hasMonth = false; buffer.append("\t<date>"); } } buffer.append(output); hasYear = true; lastTag = s1; continue; } lastTag = s1; } if (lastTag != null) { if (lastTag.startsWith("I-")) { lastTag0 = lastTag.substring(2, lastTag.length()); } else { lastTag0 = lastTag; } currentTag0 = ""; testClosingTag(buffer, currentTag0, lastTag0); buffer.append("</date>\n"); } } catch (Exception e) { throw new GrobidException("An exception occured while running Grobid.", e); } return buffer; } DateParser(); List<Date> processing(String input); void normalize(Date date); StringBuilder trainingExtraction(List<String> inputs); static final Pattern jan; static final Pattern feb; static final Pattern mar; static final Pattern apr; static final Pattern may; static final Pattern jun; static final Pattern jul; static final Pattern aug; static final Pattern sep; static final Pattern oct; static final Pattern nov; static final Pattern dec; static final Pattern[] months; }
@Test public void testTrainingExtraction_simpleDate1() throws Exception { List<String> input = Arrays.asList("December 1943"); StringBuilder sb = target.trainingExtraction(input); String output = sb.toString(); assertThat(output, is("\t<date><month>December</month> <year>1943</year></date>\n")); }
@Test public void testTrainingExtraction_simpleDate2() throws Exception { List<String> input = Arrays.asList("15 March 1942"); StringBuilder sb = target.trainingExtraction(input); String output = sb.toString(); assertThat(output, is("\t<date><day>15</day> <month>March</month> <year>1942</year></date>\n")); }
@Test public void testTrainingExtraction_simpleDate3() throws Exception { List<String> input = Arrays.asList("1943-1944"); StringBuilder sb = target.trainingExtraction(input); String output = sb.toString(); assertThat(output, is("\t<date><year>1943</year>-</date>\n\t<date><year>1944</year></date>\n")); }
@Test public void testTrainingExtraction_emptyInput() throws Exception { assertThat(target.trainingExtraction(null), nullValue()); }