Package opennlp.tools.formats.ad
Class ADSentenceStream.SentenceParser
java.lang.Object
opennlp.tools.formats.ad.ADSentenceStream.SentenceParser
- Enclosing class:
ADSentenceStream
Parses a sample of AD corpus. A sentence in AD corpus is represented by a
Tree. In this class we declare some types to represent that tree. Today we get only
the first alternative (A1).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents the AD leafprivate static final recordThe end of a leaf line.private static final recordThe part of a leaf line after the lemma.private static final classWhat the scans of one line remember: for each index, whether no end of a leaf line was found there, and the index of the next closing parenthesis at or after it.static classRepresents the AD node(package private) static final recordA punctuation line.private static final recordA tag at the start of a line.static classRepresents a tree element, Node or Leaf -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final charprivate static final charprivate static final charprivate static final charprivate static final charprivate static final charprivate static final charprivate static final charprivate static final charprivate static final charprivate static final org.slf4j.Loggerprivate Stringprivate static final charprivate static final charprivate static final charprivate static final Stringprivate static final Stringprivate static final charprivate Stringprivate static final char -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringfixPunctuation(String text) Removes the whitespace between a closing guillemet and a period or a comma that follows it.getElement(String line) Reads one tree line into aADSentenceStream.SentenceParser.Nodeor aADSentenceStream.SentenceParser.Leaf.private booleanisLemmaStart(String line, int from) Tests for the start of a quoted lemma: an opening parenthesis and a quote.private booleanisNodeTail(String line, int from) Tests the rest of a node line after the tag: an optional part in parentheses, then the tag groups.private booleanisQuote(char c) Tests for a double or single quote.private booleanisTagGroupRun(String line, int from) Tests for tag groups up to the end of the line: after optional whitespace either no more text, or an opening parenthesis and angle bracket, one or more characters, a closing angle bracket and parenthesis, and optional whitespace.private booleanisWordWithMarkup(String lexeme) Tests whether a lexeme starts with a letter, a digit, or an underscore and has a period or an angle bracket after it.private intFinds the matching quote that ends a lemma field.private intlevelPrefixEnd(String line) Finds the end of the level prefix: the run of equals signs and hyphens at the start of the line.private StringReads the morphological tag at the end of a leaf line.Parses a sentence string into aADSentenceStream.Sentence.parseBizarreLeaf(String line) Parses a leaf line with an equals sign in place of the colon in its tag: the level prefix, the tag, and in parentheses an optional quoted lemma and an optional morphological tag, then whitespace and the lexeme.parseBizarreLeafAfterTag(String line, ADSentenceStream.SentenceParser.Tag tag, ADSentenceStream.SentenceParser.LineScan scan) Parses the part of a leaf line after a tag with an equals sign and the opening parenthesis, seeparseBizarreLeaf(String).Parses a leaf line: the level prefix, a syntactic tag, a colon, a functional tag, and in parentheses a quoted lemma, secondary tags in angle brackets, and a morphological tag, then whitespace and the lexeme.parseLeafAfterTag(String line, ADSentenceStream.SentenceParser.Tag tag, ADSentenceStream.SentenceParser.LineScan scan) Parses the part of a leaf line after the tag, seeparseLeaf(String).Parses a node line: the level prefix, a syntactic tag with a colon, an optional part in parentheses, and optional tag groups.(package private) ADSentenceStream.SentenceParser.PunctuationLineparsePunctuationLine(String line) Parses a punctuation line: leading equals signs followed by one or more characters, none of which is a letter, a decimal digit, or an underscore.scanLeafEnd(String line, int from, ADSentenceStream.SentenceParser.LineScan scan) Scans the end of a leaf line: optional whitespace, an optional morphological tag up to the first closing parenthesis, that parenthesis, whitespace, and the lexeme.scanLevelAndTag(String line, int prefix, char separator, int maxStart) Scans the tag at the start of a line after the level prefix.private intscanLexemeStart(String line, int close) Finds the lexeme after the closing parenthesis: whitespace, then the rest of the line.scanSecondaryTags(String line, int from, ADSentenceStream.SentenceParser.LineScan scan) Scans secondary tags and the end of a leaf line after them.Scans a tag: one or more characters other than a colon or an equals sign, the separator, and one or more characters that are neither an opening parenthesis nor whitespace.private intskipWhitespace(String line, int from) Skips whitespace asStringUtil.isUnicodeWhitespace(char)defines it.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
LEVEL_MARK
private static final char LEVEL_MARK- See Also:
-
LEVEL_HYPHEN
private static final char LEVEL_HYPHEN- See Also:
-
TAG_SEPARATOR
private static final char TAG_SEPARATOR- See Also:
-
BIZARRE_TAG_SEPARATOR
private static final char BIZARRE_TAG_SEPARATOR- See Also:
-
GROUP_OPEN
private static final char GROUP_OPEN- See Also:
-
GROUP_CLOSE
private static final char GROUP_CLOSE- See Also:
-
BRACKET_OPEN
private static final char BRACKET_OPEN- See Also:
-
BRACKET_CLOSE
private static final char BRACKET_CLOSE- See Also:
-
SQUARE_BRACKET_CLOSE
private static final char SQUARE_BRACKET_CLOSE- See Also:
-
DOUBLE_QUOTE
private static final char DOUBLE_QUOTE- See Also:
-
SINGLE_QUOTE
private static final char SINGLE_QUOTE- See Also:
-
CLOSING_GUILLEMET
private static final char CLOSING_GUILLEMET- See Also:
-
PERIOD
private static final char PERIOD- See Also:
-
COMMA
private static final char COMMA- See Also:
-
UNDERSCORE
private static final char UNDERSCORE- See Also:
-
TAG_GROUP_OPEN
- See Also:
-
TAG_GROUP_CLOSE
- See Also:
-
text
-
meta
-
-
Constructor Details
-
SentenceParser
public SentenceParser()
-
-
Method Details
-
parse
public ADSentenceStream.Sentence parse(String sentenceString, int para, boolean isTitle, boolean isBox) Parses a sentence string into aADSentenceStream.Sentence.- Parameters:
sentenceString- The input string to parse.para- The parameter number.isTitle-trueif it represents a title element,falseotherwise.isBox-trueif it represents a box element,falseotherwise.- Returns:
- A
ADSentenceStream.Sentenceinstance parsed fromsentenceString.
-
fixPunctuation
Removes the whitespace between a closing guillemet and a period or a comma that follows it.- Parameters:
text- The text.- Returns:
- The text with those runs joined, or the text itself if it has no closing guillemet.
-
parsePunctuationLine
Parses a punctuation line: leading equals signs followed by one or more characters, none of which is a letter, a decimal digit, or an underscore. Letters and digits are judged by code point, so an accented or non-Latin word is not punctuation. A line of equals signs only also matches, with the last one as lexeme.- Parameters:
line- The line.- Returns:
- The level and the lexeme, or
nullif the line is not a punctuation line.
-
getElement
Reads one tree line into aADSentenceStream.SentenceParser.Nodeor aADSentenceStream.SentenceParser.Leaf. A leaf line with an equals sign in its tag part, as in=H==CJT:num("818-5817" ...), takes the functional tag after the last colon and the syntactic tag before it; without a colon it has no functional tag.- Parameters:
line- The tree line.- Returns:
- The element, or
nullfor a line that is no element.
-
parseNode
Parses a node line: the level prefix, a syntactic tag with a colon, an optional part in parentheses, and optional tag groups.- Parameters:
line- The line.- Returns:
- The node, or
nullif the line is not a node line.
-
parseLeaf
Parses a leaf line: the level prefix, a syntactic tag, a colon, a functional tag, and in parentheses a quoted lemma, secondary tags in angle brackets, and a morphological tag, then whitespace and the lexeme.- Parameters:
line- The line.- Returns:
- The leaf, or
nullif the line is not a leaf line.
-
isLemmaStart
Tests for the start of a quoted lemma: an opening parenthesis and a quote.- Parameters:
line- The line.from- The index after the tag.- Returns:
trueif a quoted lemma starts there.
-
parseLeafAfterTag
private ADSentenceStream.SentenceParser.Leaf parseLeafAfterTag(String line, ADSentenceStream.SentenceParser.Tag tag, ADSentenceStream.SentenceParser.LineScan scan) Parses the part of a leaf line after the tag, seeparseLeaf(String).- Parameters:
line- The line.tag- The tag.scan- What the scans of the line remember.- Returns:
- The leaf, or
nullif the part after the tag does not have that form.
-
lemmaEnd
Finds the matching quote that ends a lemma field. A quote followed by whitespace, a secondary tag, or a closing parenthesis or square bracket ends the field; quotes embedded in a word remain literal. Later quotes in the morphology or lexeme cannot extend the lemma.- Parameters:
line- The tree line.open- The index of the opening quote.- Returns:
- The closing quote index, or -1 if the field has no closing quote.
-
parseBizarreLeaf
Parses a leaf line with an equals sign in place of the colon in its tag: the level prefix, the tag, and in parentheses an optional quoted lemma and an optional morphological tag, then whitespace and the lexeme.- Parameters:
line- The line.- Returns:
- The leaf, or
nullif the line does not have that form.
-
parseBizarreLeafAfterTag
private ADSentenceStream.SentenceParser.Leaf parseBizarreLeafAfterTag(String line, ADSentenceStream.SentenceParser.Tag tag, ADSentenceStream.SentenceParser.LineScan scan) Parses the part of a leaf line after a tag with an equals sign and the opening parenthesis, seeparseBizarreLeaf(String).- Parameters:
line- The line.tag- The tag.scan- What the scans of the line remember.- Returns:
- The leaf, or
nullif the part after the tag does not have that form.
-
morphology
Reads the morphological tag at the end of a leaf line.- Parameters:
line- The line.end- The end of the line.- Returns:
- The tag, or
nullif it is empty.
-
levelPrefixEnd
Finds the end of the level prefix: the run of equals signs and hyphens at the start of the line.- Parameters:
line- The line.- Returns:
- The index after the run, or 0 if the line does not start with one.
-
scanLevelAndTag
private ADSentenceStream.SentenceParser.Tag scanLevelAndTag(String line, int prefix, char separator, int maxStart) Scans the tag at the start of a line after the level prefix. A longer prefix is preferred; hyphens at its end may move into the tag, so the callers try the next shorter prefix when the rest of the line does not parse.- Parameters:
line- The line.prefix- The index after the level prefix, seelevelPrefixEnd(String).separator- The character between the two parts of the tag.maxStart- The highest index where the tag may start: the end of the prefix for the first candidate, one less than the previous start for the next one.- Returns:
- The tag, or
nullif there is no further candidate.
-
scanTag
Scans a tag: one or more characters other than a colon or an equals sign, the separator, and one or more characters that are neither an opening parenthesis nor whitespace.- Parameters:
line- The line.from- The index where the tag starts.separator- The character between the two parts of the tag.- Returns:
- The tag, or
nullif there is no tag atfrom.
-
isNodeTail
Tests the rest of a node line after the tag: an optional part in parentheses, then the tag groups.- Parameters:
line- The line.from- The index after the tag.- Returns:
trueif the rest of the line has that form.
-
isTagGroupRun
Tests for tag groups up to the end of the line: after optional whitespace either no more text, or an opening parenthesis and angle bracket, one or more characters, a closing angle bracket and parenthesis, and optional whitespace.- Parameters:
line- The line.from- The index where the tag groups start.- Returns:
trueif the rest of the line has that form.
-
scanSecondaryTags
private ADSentenceStream.SentenceParser.LeafRest scanSecondaryTags(String line, int from, ADSentenceStream.SentenceParser.LineScan scan) Scans secondary tags and the end of a leaf line after them. Each tag is an opening angle bracket, one or more characters, and a closing angle bracket; a longer tag, and then one more tag, is preferred when the rest of the line still parses after it.- Parameters:
line- The line.from- The index where the next secondary tag would start.scan- What the scans of the line remember.- Returns:
- The rest of the line after the lemma, or
nullif it does not have that form.
-
scanLeafEnd
private ADSentenceStream.SentenceParser.LeafEnd scanLeafEnd(String line, int from, ADSentenceStream.SentenceParser.LineScan scan) Scans the end of a leaf line: optional whitespace, an optional morphological tag up to the first closing parenthesis, that parenthesis, whitespace, and the lexeme.- Parameters:
line- The line.from- The index after the secondary tags or the lemma.scan- What the scans of the line remember.- Returns:
- The end of the line, or
nullif it does not have that form.
-
scanLexemeStart
Finds the lexeme after the closing parenthesis: whitespace, then the rest of the line. A line with no text or only whitespace after the parenthesis has no lexeme.- Parameters:
line- The line.close- The index of the closing parenthesis.- Returns:
- The start of the lexeme, or -1 if there is none.
-
isWordWithMarkup
Tests whether a lexeme starts with a letter, a digit, or an underscore and has a period or an angle bracket after it. Letters and digits are checked by code point, so a word of any script counts.- Parameters:
lexeme- The lexeme.- Returns:
truefor such a lexeme.
-
skipWhitespace
Skips whitespace asStringUtil.isUnicodeWhitespace(char)defines it.- Parameters:
line- The line.from- The index to start at.- Returns:
- The index of the first character at or after
fromthat is not whitespace, or the length of the line.
-
isQuote
private boolean isQuote(char c) Tests for a double or single quote.- Parameters:
c- The character.- Returns:
truefor one of the two.
-