OpenNlpChunkedSentenceParser { public ChunkedSentence parseSentence(String sent) throws ParseException { sent = sent.replace(" ]", "]"); ArrayList<String> tokensList = new ArrayList<String>(); ArrayList<String> posTagsList = new ArrayList<String>(); ArrayList<String> npChunkTagsList = new ArrayList<String>(); String[] parts = sent.trim().split(" "); String currentChunk = null; boolean atChunkStart = false; for (String part : parts) { if (isChunk(part)) { currentChunk = getChunkType(part); atChunkStart = true; } else { String[] tokTag = getTokenTag(part); String token = tokTag[0]; String posTag = tokTag[1]; tokensList.add(token); posTagsList.add(posTag); if (currentChunk == null) { npChunkTagsList.add("O"); } else if (atChunkStart) { npChunkTagsList.add("B-" + currentChunk); atChunkStart = false; } else { npChunkTagsList.add("I-" + currentChunk); } if (isEndChunk(part)) currentChunk = null; } } String[] tokens = tokensList.toArray(new String[0]); String[] posTags = posTagsList.toArray(new String[0]); String[] npChunkTags = npChunkTagsList.toArray(new String[0]); if (attachOfs) { OpenNlpUtils.attachOfs(tokens, npChunkTags); } else { OpenNlpUtils.detatchOfs(tokens, npChunkTags); } if (attachPossessives) { OpenNlpUtils.attachPossessives(posTags, npChunkTags); } else { OpenNlpUtils.detatchPossessives(posTags, npChunkTags); } try { return new ChunkedSentence(tokens, posTags, npChunkTags); } catch (SequenceException e) { throw new ParseException(e.getMessage(), 0); } }  boolean attachOfs(); boolean attachPossessives(); void attachOfs(boolean attachOfs); void attachPossessives(boolean attachPossessives); ChunkedSentence parseSentence(String sent);  }
@Test public void testParseSentence() throws Exception { String sentStr = " [NP Numerous/JJ Palestinian/JJ churches/NNS] [VP are/VBP holding/VBG] [NP special/JJ prayers/NNS] [NP this/DT morning/NN] [PP for/IN] [NP all/PDT those/DT suffering/VBG and/CC mourning/VBG] [PP in/IN] [NP the/DT wake/NN] [PP of/IN] [NP this/DT unthinkable/JJ tragedy/NN] ./."; int expectedLength = 23; String[] expectedPos = "JJ JJ NNS VBP VBG JJ NNS DT NN IN PDT DT VBG CC VBG IN DT NN IN DT JJ NN .".split(" "); String[] expectedToks = "Numerous Palestinian churches are holding special prayers this morning for all those suffering and mourning in the wake of this unthinkable tragedy .".split(" "); ArrayList<Range> expectedRanges = new ArrayList<Range>(); expectedRanges.add(new Range(0, 3)); expectedRanges.add(new Range(5, 2)); expectedRanges.add(new Range(7, 2)); expectedRanges.add(new Range(10, 5)); expectedRanges.add(new Range(16, 2)); expectedRanges.add(new Range(19, 3)); OpenNlpChunkedSentenceParser reader = new OpenNlpChunkedSentenceParser(); reader.attachOfs(false); ChunkedSentence sent = reader.parseSentence(sentStr); ArrayList<Range> gotRanges = new ArrayList<Range>(); Iterables.addAll(gotRanges, sent.getNpChunkRanges()); assertEquals(expectedLength, sent.getLength()); assertArrayEquals(expectedPos, sent.getPosTags().toArray()); assertArrayEquals(expectedToks, sent.getTokens().toArray()); assertEquals(expectedRanges, gotRanges); }