Пример #1
0
  /**
   * 功能:切换字符串中的所有字母大小写。<br>
   *
   * <pre>
   * StringUtil.swapCase(null)                 = null
   * StringUtil.swapCase("")                   = ""
   * StringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
   * </pre>
   *
   * @param str 源字符串
   * @return String
   */
  public static String swapCase(String str) {
    if (StringUtil.isEmpty(str)) {
      return str;
    }
    char[] buffer = str.toCharArray();

    boolean whitespace = true;

    for (int i = 0; i < buffer.length; i++) {
      char ch = buffer[i];
      if (Character.isUpperCase(ch)) {
        buffer[i] = Character.toLowerCase(ch);
        whitespace = false;
      } else if (Character.isTitleCase(ch)) {
        buffer[i] = Character.toLowerCase(ch);
        whitespace = false;
      } else if (Character.isLowerCase(ch)) {
        if (whitespace) {
          buffer[i] = Character.toTitleCase(ch);
          whitespace = false;
        } else {
          buffer[i] = Character.toUpperCase(ch);
        }
      } else {
        whitespace = Character.isWhitespace(ch);
      }
    }
    return new String(buffer);
  }
Пример #2
0
  // Code taken from Berkley Parser v1.1 SophisticatedLexicon.java
  public static String signature(String word, Model model) {

    int wlen = word.length();
    int numCaps = 0;
    boolean hasDigit = false;
    boolean hasDash = false;
    boolean hasLower = false;

    for (int i = 0; i < wlen; i++) {
      char ch = word.charAt(i);
      if (Character.isDigit(ch)) {
        hasDigit = true;
      } else if (ch == '-') {
        hasDash = true;
      } else if (Character.isLetter(ch)) {
        if (Character.isLowerCase(ch)) {
          hasLower = true;
        } else if (Character.isTitleCase(ch)) {
          hasLower = true;
          numCaps++;
        } else {
          numCaps++;
        }
      }
    }

    StringBuilder sb = new StringBuilder();

    char ch0 = word.charAt(0);
    String lowered = word.toLowerCase();
    if (Character.isUpperCase(ch0) || Character.isTitleCase(ch0)) {
      if (numCaps == 1) {
        sb.append("-INITC");
        if (model.isKnown(lowered)) {
          sb.append("-KNOWNLC");
        }
      } else {
        sb.append("-CAPS");
      }
    } else if (!Character.isLetter(ch0) && numCaps > 0) {
      sb.append("-CAPS");
    } else if (hasLower) { // (Character.isLowerCase(ch0)) {
      sb.append("-LC");
    }
    if (hasDigit) {
      sb.append("-NUM");
    }
    if (hasDash) {
      sb.append("-DASH");
    }

    SuffixTrie trie = model.getSuffixTrie();
    String lang = model.getProperties().getLanguage();

    if (trie != null) {

      if (word.length() >= 5 && !hasDash && !hasDigit && numCaps == 0) {
        String suffix = trie.getSuffix(word);
        if (suffix != null) {
          sb.append("-");
          sb.append(suffix);
        }
      }

    } else if (lang.equals("en")) {
      if (lowered.endsWith("s") && wlen >= 3) {
        char ch2 = lowered.charAt(wlen - 2);
        if (ch2 != 's' && ch2 != 'i' && ch2 != 'u') {
          sb.append("-s");
        }
      } else if (word.length() >= 5 && !hasDash && !(hasDigit && numCaps > 0)) {
        if (lowered.endsWith("ed")) {
          sb.append("-ed");
        } else if (lowered.endsWith("ing")) {
          sb.append("-ing");
        } else if (lowered.endsWith("ion")) {
          sb.append("-ion");
        } else if (lowered.endsWith("er")) {
          sb.append("-er");
        } else if (lowered.endsWith("est")) {
          sb.append("-est");
        } else if (lowered.endsWith("ly")) {
          sb.append("-ly");
        } else if (lowered.endsWith("ity")) {
          sb.append("-ity");
        } else if (lowered.endsWith("y")) {
          sb.append("-y");
        } else if (lowered.endsWith("al")) {
          sb.append("-al");
        }
      }
    }

    return sb.toString();
  }
Пример #3
0
 /**
  * Returns a parser that produces a title-case character.
  *
  * @param missing The error if there is no character on the stream to produce a title-case
  *     character with.
  * @param sat The error if the produced character is not a title-case character.
  * @return A parser that produces a title-case character.
  * @see Character#isTitleCase(char)
  */
 public static <E> Parser<Stream<Character>, Character, E> titleCase(
     final F0<E> missing, final F<Character, E> sat) {
   return StreamParser.satisfy(missing, sat, c -> Character.isTitleCase(c));
 }