/**
   * Takes out all single character replacements and put them in a char array. This array can later
   * be used for adding or changing letters in getSuggestion().
   *
   * @return char[] An array of chars with replacements characters
   */
  public char[] getCodeReplaceList() {
    char[] replacements;
    TransformationRule rule;
    Vector tmp = new Vector();

    if (ruleArray == null) return null;
    for (int i = 0; i < ruleArray.length; i++) {
      rule = (TransformationRule) ruleArray[i];
      if (rule.getReplaceExp().length() == 1) tmp.addElement(rule.getReplaceExp());
    }
    replacements = new char[tmp.size()];
    for (int i = 0; i < tmp.size(); i++) {
      replacements[i] = ((String) tmp.elementAt(i)).charAt(0);
    }
    return replacements;
  }
示例#2
0
  /**
   * Given an object in the replacement, return the corresponding object in the pattern if any, or
   * null otherwise.
   *
   * @param replacementObject The object in the replacement.
   * @return The object in the pattern, or null if not found.
   */
  public static NamedObj getCorrespondingPatternObject(NamedObj replacementObject) {
    if (replacementObject instanceof Replacement) {
      return ((TransformationRule) replacementObject.getContainer()).getPattern();
    }

    PatternObjectAttribute attribute;
    try {
      attribute = getPatternObjectAttribute(replacementObject, false);
    } catch (KernelException e) {
      attribute = null;
    }
    if (attribute == null) {
      return null;
    }

    CompositeActorMatcher container = getContainingPatternOrReplacement(replacementObject);
    if (container == null) {
      return null;
    }

    String patternObjectName = attribute.getExpression();
    if (patternObjectName.equals("")) {
      return null;
    }

    TransformationRule transformer = (TransformationRule) container.getContainer();
    Pattern pattern = transformer.getPattern();
    if (replacementObject instanceof Attribute) {
      return pattern.getAttribute(patternObjectName);
    } else if (replacementObject instanceof Entity) {
      return pattern.getEntity(patternObjectName);
    } else if (replacementObject instanceof Relation) {
      return pattern.getRelation(patternObjectName);
    } else {
      return null;
    }
  }
  /**
   * Builds the phonetic code of the word.
   *
   * @param word the word to transform
   * @return the phonetic transformation of the word
   */
  public String transform(String word) {

    if (ruleArray == null) return null;

    TransformationRule rule;
    StringBuffer str = new StringBuffer(word.toUpperCase());
    int strLength = str.length();
    int startPos = 0, add = 1;

    while (startPos < strLength) {

      add = 1;
      if (Character.isDigit(str.charAt(startPos))) {
        StringUtility.replace(str, startPos, startPos + DIGITCODE.length(), DIGITCODE);
        startPos += add;
        continue;
      }

      for (int i = 0; i < ruleArray.length; i++) {
        // System.out.println("Testing rule#:"+i);
        rule = (TransformationRule) ruleArray[i];
        if (rule.startsWithExp() && startPos > 0) continue;
        if (startPos + rule.lengthOfMatch() > strLength) {
          continue;
        }
        if (rule.isMatching(str, startPos)) {
          String replaceExp = rule.getReplaceExp();

          add = replaceExp.length();
          StringUtility.replace(str, startPos, startPos + rule.getTakeOut(), replaceExp);
          strLength -= rule.getTakeOut();
          strLength += add;
          // System.out.println("Replacing with rule#:"+i+" add="+add);
          break;
        }
      }
      startPos += add;
    }
    // System.out.println(word);
    // System.out.println(str.toString());
    return str.toString();
  }