Class ADSentenceStream.SentenceParser

java.lang.Object
opennlp.tools.formats.ad.ADSentenceStream.SentenceParser
Enclosing class:
ADSentenceStream

public static class ADSentenceStream.SentenceParser extends Object
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).
  • Field Details

  • 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 a ADSentenceStream.Sentence.
      Parameters:
      sentenceString - The input string to parse.
      para - The parameter number.
      isTitle - true if it represents a title element, false otherwise.
      isBox - true if it represents a box element, false otherwise.
      Returns:
      A ADSentenceStream.Sentence instance parsed from sentenceString.
    • fixPunctuation

      private String fixPunctuation(String text)
      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 null if the line is not a punctuation line.
    • getElement

      Reads one tree line into a ADSentenceStream.SentenceParser.Node or a ADSentenceStream.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 null for a line that is no element.
    • parseNode

      private ADSentenceStream.SentenceParser.Node parseNode(String line)
      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 null if the line is not a node line.
    • parseLeaf

      private ADSentenceStream.SentenceParser.Leaf parseLeaf(String line)
      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 null if the line is not a leaf line.
    • isLemmaStart

      private boolean isLemmaStart(String line, int from)
      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:
      true if a quoted lemma starts there.
    • parseLeafAfterTag

      Parses the part of a leaf line after the tag, see parseLeaf(String).
      Parameters:
      line - The line.
      tag - The tag.
      scan - What the scans of the line remember.
      Returns:
      The leaf, or null if the part after the tag does not have that form.
    • lemmaEnd

      private int lemmaEnd(String line, int open)
      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

      private ADSentenceStream.SentenceParser.Leaf 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.
      Parameters:
      line - The line.
      Returns:
      The leaf, or null if the line does not have that form.
    • parseBizarreLeafAfterTag

      Parses the part of a leaf line after a tag with an equals sign and the opening parenthesis, see parseBizarreLeaf(String).
      Parameters:
      line - The line.
      tag - The tag.
      scan - What the scans of the line remember.
      Returns:
      The leaf, or null if the part after the tag does not have that form.
    • morphology

      private String morphology(String line, ADSentenceStream.SentenceParser.LeafEnd end)
      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 null if it is empty.
    • levelPrefixEnd

      private int levelPrefixEnd(String line)
      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, see levelPrefixEnd(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 null if there is no further candidate.
    • scanTag

      private ADSentenceStream.SentenceParser.Tag scanTag(String line, int from, char separator)
      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 null if there is no tag at from.
    • isNodeTail

      private boolean isNodeTail(String line, int from)
      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:
      true if the rest of the line has that form.
    • isTagGroupRun

      private boolean isTagGroupRun(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.
      Parameters:
      line - The line.
      from - The index where the tag groups start.
      Returns:
      true if the rest of the line has that form.
    • scanSecondaryTags

      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 null if it does not have that form.
    • scanLeafEnd

      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 null if it does not have that form.
    • scanLexemeStart

      private int scanLexemeStart(String line, int close)
      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

      private boolean isWordWithMarkup(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. Letters and digits are checked by code point, so a word of any script counts.
      Parameters:
      lexeme - The lexeme.
      Returns:
      true for such a lexeme.
    • skipWhitespace

      private int skipWhitespace(String line, int from)
      Skips whitespace as StringUtil.isUnicodeWhitespace(char) defines it.
      Parameters:
      line - The line.
      from - The index to start at.
      Returns:
      The index of the first character at or after from that 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:
      true for one of the two.