Class Sentence

java.lang.Object
games.stendhal.common.ErrorBuffer
games.stendhal.common.parser.Sentence
All Implemented Interfaces:
ErrorDrain, Iterable<Expression>
Direct Known Subclasses:
SentenceImplementation

public class Sentence extends ErrorBuffer implements Iterable<Expression>
ConversationParser returns the parsed sentence in this class. The Sentence class stores the sentence content in a list of parsed and classified expressions composed of the words forming the sentence. Words belonging to each other are merged into common Expression objects.
Author:
Martin Fuchs
  • Field Details

  • Constructor Details

    • Sentence

      protected Sentence(ConversationContext ctx)
      Create a Sentence object.
      Parameters:
      ctx -
  • Method Details

    • getType

      public Sentence.SentenceType getType()
      Returns:
      sentence type.
    • getExpressions

      public List<Expression> getExpressions()
      Return the list of expressions.
      Returns:
      Expression iterator
    • getExpressionsArrayList

      protected Expression[] getExpressionsArrayList()
      Return an array of the expressions.
      Returns:
      Expression array
    • iterator

      public Iterator<Expression> iterator()
      Return an iterator over all expressions.
      Specified by:
      iterator in interface Iterable<Expression>
      Returns:
      Expression iterator
    • getExpression

      public Expression getExpression(int idx, String typePrefix)
      Return verb [i] of the sentence.
      Parameters:
      idx -
      typePrefix -
      Returns:
      verb
    • getUnknownTypeCount

      public int getUnknownTypeCount()
      Count the number of Expressions with unknown type.
      Returns:
      number of Expressions with unknown type
    • getUnknownTypeExpression

      public Expression getUnknownTypeExpression(int idx)
      Return unknown word [i] of the sentence.
      Parameters:
      idx -
      Returns:
      Expression with unknown type
    • getTriggerExpression

      public Expression getTriggerExpression()
      Return trigger Expression for the FSM engine. TODO mf - replace by sentence matching.
      Returns:
      trigger string
    • getVerbCount

      public int getVerbCount()
      Return the number of Expression objects of type "VER" in the sentence.
      Returns:
      number of subjects
    • getVerb

      public Expression getVerb(int i)
      Return verb [i] of the sentence.
      Parameters:
      i -
      Returns:
      subject
    • getVerb

      public Expression getVerb()
      Return verb as Expression object for the special case of sentences with only one verb.
      Returns:
      normalized verb string
    • getVerbString

      public String getVerbString()
      Return verb as String for the special case of sentences with only one verb.
      Returns:
      normalized verb string
    • getSubjectCount

      public int getSubjectCount()
      Return the number of subjects.
      Returns:
      number of subjects
    • getSubject

      public Expression getSubject(int i)
      Return subject [i] of the sentence.
      Parameters:
      i -
      Returns:
      subject
    • getSubjectName

      public String getSubjectName()
      Return the subject as String for the special case of sentences with only one subject.
      Returns:
      normalized subject string
    • getObjectCount

      public int getObjectCount()
      Return the number of objects.
      Returns:
      number of objects
    • getObject

      public Expression getObject(int i)
      Return object [i] of the parsed sentence (e.g. item to be bought).
      Parameters:
      i -
      Returns:
      object
    • getObjectName

      public String getObjectName()
      Return the object as String for the special case of sentences with only one object.
      Returns:
      normalized object name
    • getObjectName

      public String getObjectName(int i)
      Return object name [i] of the parsed sentence.
      Parameters:
      i -
      Returns:
      normalized object name
    • getPrepositionCount

      public int getPrepositionCount()
      Return the number of prepositions.
      Returns:
      number of objects
    • getPreposition

      public Expression getPreposition(int i)
      Return the preposition [i] of the parsed sentence.
      Parameters:
      i -
      Returns:
      object
    • getNumeralCount

      public int getNumeralCount()
      Return the number of number Expressions.
      Returns:
      number of number Expressions
    • getNumeral

      public Expression getNumeral(int i)
      Return numeral [i] of the parsed sentence.
      Parameters:
      i -
      Returns:
      numeral
    • getNumeral

      public Expression getNumeral()
      Return the single numeral of the parsed sentence.
      Returns:
      numeral
    • isEmpty

      public boolean isEmpty()
      Return true if the sentence is empty.
      Returns:
      empty flag
    • getTrimmedText

      public String getTrimmedText()
      Return the complete text of the sentence with unchanged case, but with trimmed white space. There should be only as few code places as possible to rely on this method.
      Returns:
      string
    • getOriginalText

      public String getOriginalText()
      Return the original parsed text of the sentence. Leading and trailing white space is already trimmed. deprecate: There should be only as few code places as possible to rely on this method.
      Returns:
      string
    • getNormalized

      public String getNormalized()
      Return the sentence with all words normalized.
      Returns:
      string
    • getMatcher

      public ExpressionMatcher getMatcher()
      Return the expression matcher of the first expression.
      Returns:
      expression matcher
    • parse

      public Sentence parse(ConversationContext ctx)
      Parse the sentence again, using the given conversation context.
      Parameters:
      ctx -
      Returns:
      parsed Sentence
    • parseAsMatchingSource

      public Sentence parseAsMatchingSource()
      Return a parsed sentence object to be used as source in matching.
      Returns:
      parsed for matching as source Sentence
    • isIgnorable

      protected boolean isIgnorable(Expression expr)
      Check if the given Expression should be ignored.
      Parameters:
      expr -
      Returns:
      true, if the expression should be ignored
    • toString

      public String toString()
      Return the full sentence as lower case string including type specifiers.
      Overrides:
      toString in class Object
      Returns:
      string
    • appendPunctation

      protected void appendPunctation(SentenceBuilder builder)
      Append the trailing punctuation depending on the sentence type to the given SentenceBuilder.
      Parameters:
      builder -
    • equalsNormalized

      public boolean equalsNormalized(Sentence other)
      Check if two Sentences consist of identical normalized Expressions.
      Parameters:
      other -
      Returns:
      true if so
    • diffNormalized

      public String diffNormalized(Sentence other)
      Compare two sentences and return the difference as String.
      Parameters:
      other -
      Returns:
      difference String
    • nextValid

      public Expression nextValid(Iterator<Expression> it)
      Advance the iterator and return the next non-ignorable Expression.
      Parameters:
      it -
      Returns:
      the next non-ignorable Expression
    • matchesNormalized

      public boolean matchesNormalized(String text)
      Check if the Sentence matches the given String. The match Sentence can contain explicit expressions, which are compared after normalizing, or ExpressionType specifiers like "VER" or "SUB*" in upper case.
      Parameters:
      text -
      Returns:
      true if it matches.
    • matchesNormalizedStart

      public boolean matchesNormalizedStart(String text)
      Check if the Sentence beginning matches the given String. The match Sentence can contain explicit expressions, which are compared after normalizing, or ExpressionType specifiers like "VER" or "SUB*" in upper case.
      Parameters:
      text -
      Returns:
      true, if the text matches
    • matchesStartNormalized

      public boolean matchesStartNormalized(String text)
      Check if the Sentence matches the beginning of the given String. The match Sentence can contain explicit expressions, which are compared after normalizing, or ExpressionType specifiers like "VER" or "SUB*" in upper case.
      Parameters:
      text -
      Returns:
      true, if the text matches
    • matchesFull

      public boolean matchesFull(Sentence other)
      Check if the Sentence completely matches the given Sentence. The match Sentence can contain explicit expressions, which are compared after normalizing, or ExpressionType specifiers like "VER" or "SUB*" in upper case.
      Parameters:
      other -
      Returns:
      true, if the complete sentence matches
    • matchesStart

      public boolean matchesStart(Sentence other)
      Check if the Sentence start matches the given Sentence. The match Sentence can contain explicit expressions, which are compared after normalizing, or ExpressionType specifiers like "VER" or "SUB*" in upper case.
      Parameters:
      other -
      Returns:
      true, if the start sentence start matches
    • findMatchingName

      public NameSearch findMatchingName(Set<String> names)
      Searches for a matching name in the given Set.
      Parameters:
      names -
      Returns:
      name, or null if no match
    • getExpressionStringAfterVerb

      public String getExpressionStringAfterVerb()
      Return a string containing the sentence part referenced by a verb or simply the single object name.
      Returns:
      String or null if nothing found