Esempio n. 1
0
  /**
   * ************************************************************************************************************
   * Put the "direct object" of this process into natural language.
   */
  void formulateNaturalDirectObject() {
    StringBuilder sBuild = new StringBuilder();

    VerbProperties verbProperties = new VerbPropertiesSimpleImpl();
    List<CaseRole> caseRolesToUse =
        verbProperties.getCaseRolesForGrammarRole(
            getVerb().getVerb(), SVOElement.SVOGrammarPosition.DIRECT_OBJECT);

    // It's possible that the direct object slot has already been filled, e.g. by the verb if we
    // didn't know how
    // to translate the process into natural language. Since only one direct object is allowed, in
    // this case we need to
    // precede any objects formulated here with a prefix, and add the data to the indirect object
    // list.
    String prefix = "";
    SVOElement element = directObject;
    if (!directObject.getSurfaceForm().isEmpty()) {
      prefix = "on ";
      element = new SVOElement(SVOGrammarPosition.INDIRECT_OBJECT);
      indirectObjects.add(element);
    }

    for (CaseRole role : caseRolesToUse) {
      String obj = formulateNounPhraseForCaseRole(role, directObject, kb);
      if (!obj.isEmpty()) {
        sBuild.append(prefix).append(" ").append(obj).append(" ");
        getCaseRolesScratchpad().removeAll(role);
        break;
      }
    }

    element.setSurfaceForm(sBuild.toString().replaceAll("\\s+", " ").trim());
  }
Esempio n. 2
0
  /**
   * ************************************************************************************************************
   * Attempt to perform natural language generation on this object.
   *
   * @return a sentence in natural language, or empty string on failure
   */
  public String toNaturalLanguage() {

    formulateNaturalSubject();
    formulateNaturalVerb();
    formulateNaturalDirectObject();
    formulateNaturalIndirectObject();

    if (subject.getSurfaceForm().isEmpty()) {
      reset();
      doPassiveVoice();
    }

    // If no subject, perform no NLG.
    if (subject.getSurfaceForm().isEmpty()) {
      return "";
    }

    String indirectObjectStr = concatenateIndirectObjects();
    String cleanedStr =
        (subject.getSurfaceForm()
            + " "
            + verb.getSurfaceForm()
            + " "
            + directObject.getSurfaceForm()
            + " "
            + indirectObjectStr);

    return cleanedStr.replaceAll("\\s+", " ").trim();
  }
Esempio n. 3
0
  /**
   * ************************************************************************************************************
   */
  private String concatenateIndirectObjects() {
    StringBuilder sBuild = new StringBuilder();

    for (SVOElement element : indirectObjects) {
      sBuild.append(element.getSurfaceForm()).append(" ");
    }

    return sBuild.toString().trim();
  }
Esempio n. 4
0
  /**
   * ************************************************************************************************************
   * If we haven't managed to create a subject, try creating one with Patient. The result will be
   * "experiences" + the process verb in a noun form. This can be seen as a precursor of correct
   * passive voice.
   */
  private void doPassiveVoice() {
    List<CaseRole> caseRolesToUse =
        Lists.newArrayList(
            CaseRole.EXPERIENCER,
            CaseRole.MOVES,
            CaseRole.PATIENT,
            CaseRole.RESOURCE,
            CaseRole.ATTENDS);

    // Set subject.
    StringBuilder sBuild = new StringBuilder();
    for (CaseRole role : caseRolesToUse) {
      String obj = formulateNounPhraseForCaseRole(role, subject, kb);
      if (!obj.isEmpty()) {
        sBuild.append(" ").append(obj).append(" ");
        getCaseRolesScratchpad().removeAll(role);
        break;
      }
    }

    subject.setSurfaceForm(sBuild.toString().replaceAll("\\s+", " ").trim());

    // Set verb to "experiences" and the direct object to a noun form of the process.
    verb.setVerbAndDirectObject(this);
  }
Esempio n. 5
0
  /**
   * ************************************************************************************************************
   * Return all the entities of the given role in the correct case. Assumes the role will be some
   * kind of noun.
   *
   * @param role
   * @param element
   * @param kb
   * @return
   */
  private String formulateNounPhraseForCaseRole(CaseRole role, SVOElement element, KB kb) {
    if (!getCaseRolesScratchpad().containsKey(role)) {
      return "";
    }
    Set<String> rawNouns = Sentence.getRoleEntities(role, getCaseRolesScratchpad());

    List<String> fixedNouns = Lists.newArrayList();
    // We're assuming that only names and reified objects are in uppercase.
    for (String noun : rawNouns) {
      String temp = noun;
      if (!NLGStringUtils.isVariable(noun)) {
        temp = addProperties(noun);
        if (Noun.takesIndefiniteArticle(noun, kb)) {
          temp = Noun.aOrAn(temp) + " " + temp;
        }
        // Replace the noun with its SUMO representation if it has one.
        String kbStr = SumoProcessCollector.getProperFormOfEntity(noun, kb);
        temp = temp.replaceAll(noun, kbStr);
      }
      fixedNouns.add(temp);
      element.addConsumedCaseRole(role);
    }

    return NLGStringUtils.concatenateWithCommas(fixedNouns);
  }
Esempio n. 6
0
  /**
   * ************************************************************************************************************
   * Put the "indirect object" of this process into natural language.
   */
  void formulateNaturalIndirectObject() {
    VerbProperties verbProperties = new VerbPropertiesSimpleImpl();
    String verb = getVerb().getVerb();
    List<CaseRole> caseRolesToUse =
        verbProperties.getCaseRolesForGrammarRole(
            verb, SVOElement.SVOGrammarPosition.INDIRECT_OBJECT);

    for (CaseRole role : caseRolesToUse) {
      SVOElement element = new SVOElement(SVOGrammarPosition.INDIRECT_OBJECT);
      String obj = formulateNounPhraseForCaseRole(role, element, kb);
      if (!obj.isEmpty()) {
        List<String> preps = verbProperties.getPrepositionForCaseRole(verb, role);
        // TODO: for time being, take just the first one in the list
        String prep = preps.get(0);
        element.setSurfaceForm(prep + " " + obj + " ");
        indirectObjects.add(element);
        getCaseRolesScratchpad().removeAll(role);
      }
    }
  }
Esempio n. 7
0
  /**
   * ************************************************************************************************************
   * Put the subject of this process into natural language.
   */
  void formulateNaturalSubject() {
    StringBuilder sBuild = new StringBuilder();

    VerbProperties verbProperties = new VerbPropertiesSimpleImpl();
    List<CaseRole> caseRolesToUse =
        verbProperties.getCaseRolesForGrammarRole(
            getVerb().getVerb(), SVOElement.SVOGrammarPosition.SUBJECT);

    for (CaseRole role : caseRolesToUse) {
      String obj = formulateNounPhraseForCaseRole(role, subject, kb);
      if (!obj.isEmpty()) {
        sBuild.append(" ").append(obj).append(" ");
        getCaseRolesScratchpad().removeAll(role);
        break;
      }
    }

    subject.setSurfaceForm(sBuild.toString().replaceAll("\\s+", " ").trim());
  }