// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: Sentence.java,v 1.6 2003/03/06 00:40:49 moltmans Exp $
// Sentence.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Mon May 08 17:47:33 2000> 
// Time-stamp: <2001-03-10 15:51:00 moltmans> 
package rec.annotate.facts;

import rec.annotate.rules.*;
import java.util.*;
import rec.tms.*;
import rec.sys.*;
import rec.util.*;
import rec.speech.*;
import rec.annotate.widgets.*;

/**
 * Sentence widget for further processing by individual recognizers.
 **/
public class Sentence extends Annotation
{
  /** tags for this sentence **/
  private Tag m_tags;
  /** the sentence the tags came from **/
  private WSentence m_sentence;
  private Surface m_surface;
  
  public Sentence( Surface s, Tag tags, WSentence sentence )
  {
    super( tags.getStartTime(), tags.getEndTime() );
    m_surface = s;
    m_tags = tags;
    m_sentence = sentence;
    //    System.out.println( tags );
  }

  // it is an error for tag to have multiple nouns
  public static String getNoun( Tag tag ) 
  {
    if( tag == null )
      return null;
    
    return tag.getText( "noun" );
  }

  public boolean isSubjectExternal() 
  {
    return getTag( "subject" ).hasTag( "external_actor" );
  }

  public String getSubject() 
  {
    return getNoun( getTag( "subject" ) );
  }

  public boolean hasDirectObject() 
  {
    return getDirectObject() != null;
  }
  
  public String getDirectObject() 
  {
    return getNoun( getTag( "direct_object" ) );
  }

  public String getVerb()
  {
    return getTextForTag( "verb" );
  }

  public String getPreposition()
  {
    return getTextForTag( "preposition" );
  }

  public boolean hasPrepositionalPhrase() 
  {
    return getPrepositionalPhrase() != null;
  }

  public boolean isPrepositionRotational() 
  {
    Tag prepPhrase = getTag( "prep_phrase" );
    return (prepPhrase != null) && prepPhrase.hasTag( "rotational_prep" );
  }
  
  public Sentence getPrepositionalPhrase()
  {
    return getSentence( "prep_phrase" );
  }

  public String getObjectOfPreposition()
  {
    return getTextForTag( "object_of_prep" );
  }

  public boolean isCausal() 
  {
    return isTag( "causal_sentence" );
  }

  public Sentence getCause() 
  {
    return getSentence( "cause" );
  }

  public Sentence getEffect() 
  {
    return getSentence( "effect" );

  }

  public boolean isWhenStatement()
  {
    return isTag( "conditional_phrase" );
  }

  public boolean isWhenContinuation()
  {
    return isTag( "continued_conditional" );
  }

  public Sentence getClause1() 
  {
    return getSentence( "clause1" );
  }

  public Sentence getClause2() 
  {
    return getSentence( "clause2" );
  }

  public boolean isMovesStatement()
  {
    return isSimpleSentence( "moves" );
  }

  public boolean isRotatesStatement()
  {
    return isSimpleSentence( "rotates" );
  }

  public boolean isPropelsStatement()
  {
    return isSimpleSentence( "propels" );
  }

  public boolean isReleasesStatement()
  {
    return isSimpleSentence( "releases" );
  }

  public boolean isDistanceStatement() 
  {
    return isTag( "distance" );
  }

  public boolean isSimpleSentence() 
  {
    return isTag( "simple_sentence" );
  }

  /** @return true if this is a simple sentence about tag **/
  public boolean isSimpleSentence( String tag ) 
  {
    return ( isSimpleSentence() &&
	     hasTag( tag ) );
  }
    
  
  ////////////////////////////////////////////////////////////////////
  // interface to tags methods:
  
  public Tag getTag( String tag ) 
  {
    return m_tags.getTag( tag );
  }

  public Sentence getSentence( String tag ) 
  {
    Tag t = getTag( tag );
    if( t != null )
      return new Sentence( m_surface, t, m_sentence );
    return null;
  }
  
  /** tag is in the top level of the tag/parse tree.  **/
  public boolean isTag( String tag )
  {
    return m_tags.isTag( tag );
  }

  public boolean hasTag( String tag ) 
  {
    return m_tags.hasTag( tag );
  }

  public String getTextForTag( String tag ) 
  {
    return m_tags.getText( tag );
  }

  ////////////////////////////////////////////////////////////////////
  // Implement the interface for Annotation and Fact
  public String getFactType() 
  {
    return Type.SENTENCE;
  }

  public boolean sameFact( Fact fact ) 
  {
    return ( (fact instanceof Sentence) &&
	     m_sentence.equals( ((Sentence)fact).m_sentence ) &&
	     m_tags.equals( ((Sentence)fact).m_tags ) );
  }

  public WSentence getSentence()
  {
    return m_sentence;
  }

  public Tag getTags() 
  {
    return m_tags;
  }

  public String toEnglish() 
  {
    return m_tags.getText();
  }

  public String toString() 
  {
    return "<Sentence: " + m_tags.getText() + "(" +
      m_tags.getStartTime() + ", " + m_tags.getEndTime() + ")>";
  }
  
}

