IdenticalLemmaPhraseLinker implements AlignmentComponent { @Override public void annotate(JCas aJCas) throws AlignmentComponentException { if (aJCas == null) throw new AlignmentComponentException("annotate() got a null JCas object."); JCas textView; JCas hypoView; try { textView = aJCas.getView(LAP_ImplBase.TEXTVIEW); hypoView = aJCas.getView(LAP_ImplBase.HYPOTHESISVIEW); } catch (CASException e) { throw new AlignmentComponentException("Failed to access the Two views (TEXTVIEW, HYPOTHESISVIEW)", e); } logger.info("annotate() called with a JCas with the following T and H; "); logger.info("TEXT: " + textView.getDocumentText()); logger.info("HYPO: " + hypoView.getDocumentText()); Collection<Token> t; t = JCasUtil.select(textView, Token.class); Token[] tTokens = t.toArray(new Token[t.size()]); t = JCasUtil.select(hypoView, Token.class); Token[] hTokens = t.toArray(new Token[t.size()]); int[] matchingPhraseStartLocations = new int[hTokens.length]; int[] matchingPhraseLengths = new int[hTokens.length]; for (int i=0; i < hTokens.length; i++) { int bestMatchTextPosition = -1; int bestMatchLength = 0; for (int j=0; j < tTokens.length; j++) { int currentMatchLen = 0; currentMatchLen = maxMatchOnPositions(i,j, hTokens, tTokens); if (currentMatchLen > bestMatchLength) { bestMatchTextPosition = j; bestMatchLength = currentMatchLen; } } matchingPhraseStartLocations[i] = bestMatchTextPosition; matchingPhraseLengths[i] = bestMatchLength; } int lastBestMatchPos = -1; int lastBestMatchLen = 0; int[] finalMatchingPhraseStartLocations = new int[hTokens.length]; int[] finalMatchingPhraseLengths = new int[hTokens.length]; for(int i=0; i < hTokens.length; i++) { int bestMatchPos = matchingPhraseStartLocations[i]; int bestMatchLen = matchingPhraseLengths[i]; if ( (bestMatchPos == (lastBestMatchPos + 1)) && (lastBestMatchLen == (bestMatchLen + 1)) ) { finalMatchingPhraseStartLocations[i] = -1; finalMatchingPhraseLengths[i] = 0; } else { finalMatchingPhraseStartLocations[i] = matchingPhraseStartLocations[i]; finalMatchingPhraseLengths[i] = matchingPhraseLengths[i]; } lastBestMatchPos = bestMatchPos; lastBestMatchLen = bestMatchLen; } addLinkAnnotations(aJCas, finalMatchingPhraseStartLocations, finalMatchingPhraseLengths, tTokens, hTokens); } IdenticalLemmaPhraseLinker(); @Override void annotate(JCas aJCas); static int maxMatchOnPositions(int i, int j, Token[] iArray, Token[] jArray); @Override String getComponentName(); @Override String getInstanceName(); @Override void close(); static Map<String,Boolean> isNonContentPos; }
@Test public void test() { BasicConfigurator.resetConfiguration(); BasicConfigurator.configure(); Logger.getRootLogger().setLevel(Level.INFO); Logger testlogger = Logger.getLogger(this.getClass().toString()); TreeTaggerEN lemmatizer = null; try { lemmatizer = new TreeTaggerEN(); lemmatizer.generateSingleTHPairCAS("this is a test.", "TreeTagger in sight?"); } catch (Exception e) { if (ExceptionUtils.getRootCause(e) instanceof java.io.IOException) { testlogger.info("Skipping the test: TreeTagger binary and/or models missing. \n To run this testcase, TreeTagger installation is needed. (see /lap/src/scripts/treetagger/README.txt)"); Assume.assumeTrue(false); } fail(e.getMessage()); } testMaxMatchOnPositions(lemmatizer); IdenticalLemmaPhraseLinker testInstance = null; try { testInstance = new IdenticalLemmaPhraseLinker(); JCas aJCas = lemmatizer.generateSingleTHPairCAS("This is China's new cat, and a new cat is a good thing.", "This is a new cat, and a thing. Gil?"); testInstance.annotate(aJCas); } catch (Exception e) { fail(e.getMessage()); } LAPAccess tokenizer = lemmatizer; JCas aJCas = null; try { aJCas = tokenizer.generateSingleTHPairCAS( "David Golinkin is single-handedly responsible for uncovering and re-publishing dozens of responsa of the Committee on Jewish Law and Standards of the Rabbinical Assembly, making them available to the general public in a three-volume set.", "David Golinkin is the author of dozen of responsa of the Committee on Jewish Law and Standards of the Rabbinical Assembly."); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); aJCas = tokenizer.generateSingleTHPairCAS( "Ryo Okumoto (born in Osaka, Japan) is a keyboardist, best known for his work with progressive rock group Spock's Beard.", "The rock group Spock's Beard comes from Japan." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); aJCas = tokenizer.generateSingleTHPairCAS( "A Revenue Cutter, the ship was named for Harriet Lane, niece of President James Buchanan, who served as Buchanan's White House hostess.", "Harriet Lane was a relative of President James Buchanan." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); aJCas = tokenizer.generateSingleTHPairCAS( "Claude Chabrol (born June 24, 1930) is a French movie director and has become well-known in the 40 years since his first film, Le Beau Serge , for his chilling tales of murder, including Le Boucher.", "Le Boucher was made by a French movie director." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); } catch (Exception e) { fail(e.getMessage()); } try { aJCas = tokenizer.generateSingleTHPairCAS( "David Golinkin ist ganz allein für die Entdeckung und Neuveröffentlichung Dutzender von Erwiderungen des Ausschusses für jüdische Gesetze und Normen der Rabbinerversammlung verantwortlich, so dass sie nun der breiten Öffentlichkeit in einer dreibändigen Reihe zugänglich sind.", "David Golinkin ist der Autor Dutzender von Erwiderungen des Ausschusses für jüdische Gesetze und Normen der Rabbinerversammlung." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); aJCas = tokenizer.generateSingleTHPairCAS( "Ryo Okumoto (geboren in Osaka, Japan) ist ein Keyboarder, der für seine Arbeit mit der progressiven Rockgruppe Spocks Beard bekannt ist.", "Die Rockgruppe Spocks Beard kommt aus Japan." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); aJCas = tokenizer.generateSingleTHPairCAS( "Das Schiff, ein Zollkutter, wurde nach Harriet Lane benannt, der Nichte des Präsidenten James Buchanan, die im Weißen Haus als Buchanans Hausherrin diente.", "Harriet Lane war eine Verwandte des Präsidenten James Buchanan." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); aJCas = tokenizer.generateSingleTHPairCAS( "Claude Chabrol (geboren am 24. Juni 1930) ist ein französischer Regisseur und wurde in den 40er Jahren nach seinem ersten Film, 'Le Beau Serge', berühmt für seine schaurigen Mordgeschichten, wie 'Le Boucher'.", "Le Boucher wurde von einem französischen Regisseur geleitet." ); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); } catch (Exception e) { fail(e.getMessage()); } try { aJCas = tokenizer.generateSingleTHPairCAS("Claude Chabrol divorced Agnes, his first wife, to marry the actress Stéphane Audran. His third wife is Aurore Paquiss.", "Aurore Paquiss married Chabrol."); testInstance.annotate(aJCas); LinkUtils.dumpTokenLevelLinks(aJCas, System.out); } catch (Exception e) { fail(e.getMessage()); } }