📄 numberexpander.java
字号:
/** * Returns the number string list of the given string starting at * the given index. E.g., expandNumberAt("1100", 1) gives "one hundred" * * @param numberString the string which is the number to expand * @param startIndex the starting position * @param wordRelation words are added to this Relation */ private static void expandNumberAt(String numberString, int startIndex, WordRelation wordRelation) { expandNumber(numberString.substring(startIndex,numberString.length()), wordRelation); } /** * Expands given token to list of words pronouncing it as digits * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ public static void expandDigits(String numberString, WordRelation wordRelation) { int numberDigits = numberString.length(); for (int i = 0; i < numberDigits; i++) { char digit = numberString.charAt(i); if (isDigit(digit)) { wordRelation.addWord(digit2num[numberString.charAt(i)-'0']); } else { wordRelation.addWord("umpty"); } } } /** * Expands the digit string of an ordinal number. * * @param rawNumberString the string which is the number to expand * @param wordRelation words are added to this Relation */ public static void expandOrdinal(String rawNumberString, WordRelation wordRelation) { // remove all ','s from the raw number string String numberString = Utilities.deleteChar(rawNumberString, ','); expandNumber(numberString, wordRelation); // get the last in the list of number strings Item lastItem = wordRelation.getTail(); if (lastItem != null) { FeatureSet featureSet = lastItem.getFeatures(); String lastNumber = featureSet.getString("name"); String ordinal = findMatchInArray(lastNumber, digit2num, ord2num); if (ordinal == null) { ordinal = findMatchInArray(lastNumber, digit2teen, ord2teen); } if (ordinal == null) { ordinal = findMatchInArray(lastNumber, digit2enty, ord2enty); } if (lastNumber.equals("hundred")) { ordinal = "hundredth"; } else if (lastNumber.equals("thousand")) { ordinal = "thousandth"; } else if (lastNumber.equals("billion")) { ordinal = "billionth"; } // if there was an ordinal, set the last element of the list // to that ordinal; otherwise, don't do anything if (ordinal != null) { wordRelation.setLastWord(ordinal); } } } /** * Finds a match of the given string in the given array, * and returns the element at the same index in the returnInArray * * @param strToMatch the string to match * @param matchInArray the source array * @param returnInArray the return array * * @return an element in returnInArray, or <code>null</code> * if a match is not found */ private static String findMatchInArray(String strToMatch, String[] matchInArray, String[] returnInArray) { for (int i = 0; i < matchInArray.length; i++) { if (strToMatch.equals(matchInArray[i])) { if (i < returnInArray.length) { return returnInArray[i]; } else { return null; } } } return null; } /** * Expands the given number string as pairs as in years or IDs * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ public static void expandID(String numberString, WordRelation wordRelation) { int numberDigits = numberString.length(); if ((numberDigits == 4) && (numberString.charAt(2) == '0') && (numberString.charAt(3) == '0')) { if (numberString.charAt(1) == '0') { // e.g. 2000, 3000 expandNumber(numberString, wordRelation); } else { expandNumber(numberString.substring(0,2), wordRelation); wordRelation.addWord("hundred"); } } else if ((numberDigits == 2) && (numberString.charAt(0) == '0')) { wordRelation.addWord("oh"); expandDigits(numberString.substring(1,2), wordRelation); } else if ((numberDigits == 4 && numberString.charAt(1) == '0') || numberDigits < 3) { expandNumber(numberString, wordRelation); } else if (numberDigits % 2 == 1) { String firstDigit = digit2num[numberString.charAt(0)-'0']; wordRelation.addWord(firstDigit); expandID(numberString.substring(1,numberDigits), wordRelation); } else { expandNumber(numberString.substring(0,2), wordRelation); expandID(numberString.substring(2,numberDigits), wordRelation); } } /** * Expands the given number string as a real number. * * @param numberString the string which is the real number to expand * @param wordRelation words are added to this Relation */ public static void expandReal(String numberString, WordRelation wordRelation) { int stringLength = numberString.length(); int position; if (numberString.charAt(0) == '-') { // negative real numbers wordRelation.addWord("minus"); expandReal(numberString.substring(1, stringLength), wordRelation); } else if (numberString.charAt(0) == '+') { // prefixed with a '+' wordRelation.addWord("plus"); expandReal(numberString.substring(1, stringLength), wordRelation); } else if ((position = numberString.indexOf('e')) != -1 || (position = numberString.indexOf('E')) != -1) { // numbers with 'E' or 'e' expandReal(numberString.substring(0, position), wordRelation); wordRelation.addWord("e"); expandReal(numberString.substring(position + 1), wordRelation); } else if ((position = numberString.indexOf('.')) != -1) { // numbers with '.' String beforeDot = numberString.substring(0, position); if (beforeDot.length() > 0) { expandReal(beforeDot, wordRelation); } wordRelation.addWord("point"); String afterDot = numberString.substring(position + 1); if (afterDot.length() > 0) { expandDigits(afterDot, wordRelation); } } else { // everything else expandNumber(numberString, wordRelation); } } /** * Expands the given string of letters as a list of single char symbols. * * @param letters the string of letters to expand * @param wordRelation words are added to this Relation */ public static void expandLetters(String letters, WordRelation wordRelation) { letters = letters.toLowerCase(); char c; for (int i = 0; i < letters.length(); i++) { // if this is a number c = letters.charAt(i); if (isDigit(c)) { wordRelation.addWord(digit2num[c-'0']); } else if (letters.equals("a")) { wordRelation.addWord("_a"); } else { wordRelation.addWord(String.valueOf(c)); } } } /** * Returns the integer value of the given string of Roman numerals. * * @param roman the string of Roman numbers * * @return the integer value */ public static int expandRoman(String roman) { int value = 0; for (int p = 0; p < roman.length(); p++) { char c = roman.charAt(p); if (c == 'X') { value += 10; } else if (c == 'V') { value += 5; } else if (c == 'I') { if (p+1 < roman.length()) { char p1 = roman.charAt(p+1); if (p1 == 'V') { value += 4; p++; } else if (p1 == 'X') { value += 9; p++; } else { value += 1; } } else { value += 1; } } } return value; } /** * Returns true if the given character is a digit (0-9 only). * * @param ch the character to test * * @return true or false */ public static boolean isDigit(char ch) { return ('0' <= ch && ch <= '9'); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -