📄 numberexpander.java
字号:
/** * Portions Copyright 2001-2003 Sun Microsystems, Inc. * Portions Copyright 1999-2001 Language Technologies Institute, * Carnegie Mellon University. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */package com.sun.speech.freetts.en.us;import com.sun.speech.freetts.FeatureSet;import com.sun.speech.freetts.Item;import com.sun.speech.freetts.util.Utilities;import java.util.List;import java.util.LinkedList;/** * Expands Strings containing digits characters into * a list of words representing those digits. * * It translates the following code from flite: * <code>lang/usEnglish/us_expand.c</code> */public class NumberExpander { private static final String[] digit2num = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; private static final String[] digit2teen = { "ten", /* shouldn't get called */ "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; private static final String[] digit2enty = { "zero", /* shouldn't get called */ "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static final String[] ord2num = { "zeroth", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth" }; private static final String[] ord2teen = { "tenth", /* shouldn't get called */ "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth" }; private static final String[] ord2enty = { "zeroth", /* shouldn't get called */ "tenth", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" }; /** * Unconstructable */ private NumberExpander() { } /** * Expands a digit string into a list of English words of those digits. * For example, "1234" expands to "one two three four" * * @param numberString the digit string to expand. * @param wordRelation words are added to this Relation */ public static void expandNumber(String numberString, WordRelation wordRelation) { int numDigits = numberString.length(); if (numDigits == 0) { // wordRelation = null; } else if (numDigits == 1) { expandDigits(numberString, wordRelation); } else if (numDigits == 2) { expand2DigitNumber(numberString, wordRelation); } else if (numDigits == 3) { expand3DigitNumber(numberString, wordRelation); } else if (numDigits < 7) { expandBelow7DigitNumber(numberString, wordRelation); } else if (numDigits < 10) { expandBelow10DigitNumber(numberString, wordRelation); } else if (numDigits < 13) { expandBelow13DigitNumber(numberString, wordRelation); } else { expandDigits(numberString, wordRelation); } } /** * Expands a two-digit string into a list of English words. * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ private static void expand2DigitNumber(String numberString, WordRelation wordRelation) { if (numberString.charAt(0) == '0') { // numberString is "0X" if (numberString.charAt(1) == '0') { // numberString is "00", do nothing } else { // numberString is "01", "02" ... String number = digit2num[numberString.charAt(1)-'0']; wordRelation.addWord(number); } } else if (numberString.charAt(1) == '0') { // numberString is "10", "20", ... String number = digit2enty[numberString.charAt(0)-'0']; wordRelation.addWord(number); } else if (numberString.charAt(0) == '1') { // numberString is "11", "12", ..., "19" String number = digit2teen[numberString.charAt(1)-'0']; wordRelation.addWord(number); } else { // numberString is "2X", "3X", ... String enty = digit2enty[numberString.charAt(0)-'0']; wordRelation.addWord(enty); expandDigits(numberString.substring(1,numberString.length()), wordRelation); } } /** * Expands a three-digit string into a list of English words. * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ private static void expand3DigitNumber(String numberString, WordRelation wordRelation) { if (numberString.charAt(0) == '0') { expandNumberAt(numberString, 1, wordRelation); } else { String hundredDigit = digit2num[numberString.charAt(0)-'0']; wordRelation.addWord(hundredDigit); wordRelation.addWord("hundred"); expandNumberAt(numberString, 1, wordRelation); } } /** * Expands a string that is a 4 to 6 digits number into a list * of English words. For example, "333000" into "three hundred * and thirty-three thousand". * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ private static void expandBelow7DigitNumber(String numberString, WordRelation wordRelation) { expandLargeNumber(numberString, "thousand", 3, wordRelation); } /** * Expands a string that is a 7 to 9 digits number into a list * of English words. For example, "19000000" into nineteen million. * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ private static void expandBelow10DigitNumber(String numberString, WordRelation wordRelation) { expandLargeNumber(numberString, "million", 6, wordRelation); } /** * Expands a string that is a 10 to 12 digits number into a list * of English words. For example, "27000000000" into twenty-seven * billion. * * @param numberString the string which is the number to expand * @param wordRelation words are added to this Relation */ private static void expandBelow13DigitNumber(String numberString, WordRelation wordRelation) { expandLargeNumber(numberString, "billion", 9, wordRelation); } /** * Expands a string that is a number longer than 3 digits into a list * of English words. For example, "1000" into one thousand. * * @param numberString the string which is the number to expand * @param order either "thousand", "million", or "billion" * @param numberZeroes the number of zeroes, depending on the order, so * its either 3, 6, or 9 * @param wordRelation words are added to this Relation */ private static void expandLargeNumber(String numberString, String order, int numberZeroes, WordRelation wordRelation) { int numberDigits = numberString.length(); // parse out the prefix, e.g., "113" in "113,000" int i = numberDigits - numberZeroes; String part = numberString.substring(0, i); // get how many thousands/millions/billions Item oldTail = wordRelation.getTail(); expandNumber(part, wordRelation); if (wordRelation.getTail() == oldTail) { expandNumberAt(numberString, i, wordRelation); } else { wordRelation.addWord(order); expandNumberAt(numberString, i, wordRelation); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -