【问题标题】:Convert words (string) to Int将单词(字符串)转换为 Int
【发布时间】:2012-06-30 22:14:13
【问题描述】:

我确信这已经完成了一百次,但我希望有一个非常简单的方法来完成它。我想将单词更改为 int。

像下面的例子

一 = 1
二 = 2
三 = 3

所以基本上,如果我有字符串“One”,它会被转换为 1,即使我可以取回字符串“1”,我也可以转换它。

【问题讨论】:

标签: c#


【解决方案1】:

这样做是为了好玩...可能有很多边缘情况会失败...

private static Dictionary<string,long> numberTable=
    new Dictionary<string,long>
        {{"zero",0},{"one",1},{"two",2},{"three",3},{"four",4},
        {"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9},
        {"ten",10},{"eleven",11},{"twelve",12},{"thirteen",13},
        {"fourteen",14},{"fifteen",15},{"sixteen",16},
        {"seventeen",17},{"eighteen",18},{"nineteen",19},{"twenty",20},
        {"thirty",30},{"forty",40},{"fifty",50},{"sixty",60},
        {"seventy",70},{"eighty",80},{"ninety",90},{"hundred",100},
        {"thousand",1000},{"million",1000000},{"billion",1000000000},
        {"trillion",1000000000000},{"quadrillion",1000000000000000},
        {"quintillion",1000000000000000000}};
public static long ToLong(string numberString)
{
    var numbers = Regex.Matches(numberString, @"\w+").Cast<Match>()
         .Select(m => m.Value.ToLowerInvariant())
         .Where(v => numberTable.ContainsKey(v))
         .Select(v => numberTable[v]);
    long acc = 0,total = 0L;
    foreach(var n in numbers)
    {
        if(n >= 1000)
        {
            total += (acc * n);
            acc = 0;
        }
        else if(n >= 100){
            acc *= n;
        }
        else acc += n;          
    }
    return (total + acc)  * ( numberString.StartsWith("minus",
          StringComparison.InvariantCultureIgnoreCase) ? -1 : 1);
}

【讨论】:

  • 为什么你有双重continue;?你可以使用elses。 (当然还有 +1。)
  • 是的,我同意...更好地使用 else 语句(并且还减少了代码大小并满足了我对答案中没有滚动条的强迫症痴迷!!!)
  • 请注意the quick brown foxzero 都返回0
  • @EricJ。也许上面对“边缘”的使用被误导了。有几乎无限的案例会失败!
  • 很失望这个功能不能处理sextillion、septillion、octillion、nonillion...等;)
【解决方案2】:

我对错误处理采取了稍微不同的方法……

  public static bool ParseEnglishNumberPhrase(string pNumberPhrase, out int pValue) {
     pValue = 0;
     string[]
        temporaryWords = pNumberPhrase.ToLower().Split(new char[] { '_', ' ', '-', ',' }, StringSplitOptions.RemoveEmptyEntries),
        ones = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" },
        teens = { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" },
        tens = { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" },
        minusWords = { "minus", "negative", "hyphen" };
     List<string> words = temporaryWords.ToList();
     bool minusFlag = false;
     Dictionary<string, int> modifiers = new Dictionary<string, int>() {
        { "billion", 1000000000 },
        { "million", 1000000 },
        { "thousand", 1000 },
        { "hundred", 100 } };
     int result = 0, currentResult = 0, lastModifier = 1;


     if (pNumberPhrase.Equals("eleventy billion")) {
        pValue = int.MaxValue; // 110,000,000,000 is out of range for an int!
        return false;
     }
     if (words[0].Equals("zero") && (words.Count == 1)) {
        pValue = 0;
        return true;
     }
     else if (words[0].Equals("zero"))
        words.RemoveAt(0);
     if (pNumberPhrase.StartsWith("-"))
        minusFlag = true;
     foreach (string word in minusWords) {
        if (pNumberPhrase.Contains(word)) {
           minusFlag = true;
           words.Remove(word);
        }
     }
     if (words.Count == 1) {
        if (int.TryParse(words[0], out int pOutValue)) {
           pValue = pOutValue;
           if (minusFlag)
              pValue *= -1;
           return true;
        }
     }
     foreach (string word in words) {
        if (modifiers.ContainsKey(word))
           lastModifier *= modifiers[word];
        else {
           int n;

           if (lastModifier > 1) {
              result += currentResult * lastModifier;
              lastModifier = 1;
              currentResult = 0;
           }
           if ((n = Array.IndexOf(ones, word) + 1) > 0)
              currentResult += n;
           else if ((n = Array.IndexOf(teens, word) + 1) > 0)
              currentResult += n + 10;
           else if ((n = Array.IndexOf(tens, word) + 1) > 0)
              currentResult += n * 10;
           else if (word != "and") {
              pValue = -1;
              return false;
           }
        }
     }
     pValue = result + currentResult * lastModifier;
     if (minusFlag)
        pValue *= -1;
     return true;
  }

【讨论】:

    【解决方案3】:

    这是一种方法。如果您需要更广泛的范围,它很容易扩展;只需使用longulong 甚至BigInt,然后将更多项目添加到modifiers 字典中。

    static int ParseEnglish(string number) {
        string[] words = number.ToLower().Split(new char[] {' ', '-', ','}, StringSplitOptions.RemoveEmptyEntries);
        string[] ones = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        string[] teens = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        string[] tens = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        Dictionary<string, int> modifiers = new Dictionary<string, int>() {
            {"billion", 1000000000},
            {"million", 1000000},
            {"thousand", 1000},
            {"hundred", 100}
        };
    
        if(number == "eleventy billion")
            return int.MaxValue; // 110,000,000,000 is out of range for an int!
    
        int result = 0;
        int currentResult = 0;
        int lastModifier = 1;
    
        foreach(string word in words) {
            if(modifiers.ContainsKey(word)) {
                lastModifier *= modifiers[word];
            } else {
                int n;
    
                if(lastModifier > 1) {
                    result += currentResult * lastModifier;
                    lastModifier = 1;
                    currentResult = 0;
                }
    
                if((n = Array.IndexOf(ones, word) + 1) > 0) {
                    currentResult += n;
                } else if((n = Array.IndexOf(teens, word) + 1) > 0) {
                    currentResult += n + 10;
                } else if((n = Array.IndexOf(tens, word) + 1) > 0) {
                    currentResult += n * 10;
                } else if(word != "and") {
                    throw new ApplicationException("Unrecognized word: " + word);
                }
            }
        }
    
        return result + currentResult * lastModifier;
    }
    

    【讨论】:

      【解决方案4】:

      这是上述算法的 C# 版本。为了清晰起见,我重命名并重写了一些代码,并添加了对负数、连字符数、零以及文本单词和数字组合(如“100 和 5”)的支持。感谢 Ry- 的良好开端。

        /// <summary>
        /// Convert text number strings to integer numbers. Credit to stackoverflow
        /// for the main algorithm.
        /// </summary>
        public static int
          WordNumberToInt (string number) {
          // define arrays of keywords to translate text words to integer positions
          // in the arrays. Thus, ordering of words in the array is important.
          string[] ones = {
            "one", "two", "three", "four", "five", "six",
            "seven", "eight", "nine"
          };
          string[] teens = {
            "eleven", "twelve", "thirteen", "fourteen", "fifteen",
            "sixteen", "seventeen", "eighteen", "nineteen"
          };
          string[] tens = {
            "ten", "twenty", "thirty", "forty", "fifty", "sixty",
            "seventy", "eighty", "ninety"
          };
          var bigscales = new Dictionary<string, int> () {
            {"hundred", 100}, {"hundreds", 100}, {"thousand", 1000},
            {"million", 1000000}, {"billion", 1000000000},
          };
          string[] minusWords = {"minus", "negative"};
          var splitchars = new char[] {' ', '-', ','};
      
          // flip all words to lowercase for proper matching
          var lowercase = number.ToLower ();
          var inputwords = lowercase.Split (splitchars, StringSplitOptions.RemoveEmptyEntries);
      
          // initalize loop variables and flags
          int result = 0;
          int currentResult = 0;
          int bigMultiplierValue = 1;
          bool bigMultiplierIsActive = false;
          bool minusFlag = false;
      
          foreach (string curword in inputwords) {
            // input words are either bigMultipler words or little words
            //
            if (bigscales.ContainsKey (curword)) {
              bigMultiplierValue *= bigscales[curword];
              bigMultiplierIsActive = true;
            }
      
            else {
              // multiply the current result by the previous word bigMultiplier
              // and disable the big multiplier until next time
              if (bigMultiplierIsActive) {
                result += currentResult * bigMultiplierValue;
                currentResult = 0;
                bigMultiplierValue = 1; // reset the multiplier value
                bigMultiplierIsActive = false; // turn it off until next time
              }
      
              // translate the incoming text word to an integer
              int n;
              if ((n = Array.IndexOf (ones, curword) + 1) > 0) {
                currentResult += n;
              }
              else if ((n = Array.IndexOf (teens, curword) + 1) > 0) {
                currentResult += n + 10;
              }
              else if ((n = Array.IndexOf (tens, curword) + 1) > 0) {
                currentResult += n * 10;
              }
              // allow for negative words (like "minus") 
              else if (minusWords.Contains (curword)) {
                minusFlag = true;
              }
              // allow for phrases like "zero 500" hours military time
              else if (curword == "zero") {
                continue;
              }
              // allow for text digits too, like "100 and 5"
              else if (int.TryParse (curword, out int tmp)) {
                currentResult += tmp;
              }
              else if (curword != "and") {
                throw new ApplicationException ("Expected a number: " + curword);
              }
            }
          }
      
          var final = result + currentResult * bigMultiplierValue;
          if (minusFlag)
            final *= -1;
          return final;
        }
      

      这是我运行的一些测试用例。

        -20 = minus twenty
       -261 = minus two hundred sixty one
       1965 = nineteen hundred and sixty five
         45 = forty five
         55 = fifty-five
         21 = twenty-one
         55 = fifty five
          0 = zero
        105 = one hundred 5
        105 = 100 and 5
      

      【讨论】:

        【解决方案5】:

        我认为一个更容易理解的解决方案如下:

        public static int ParseInt(string s)
        {
          var wordArray = s.Split(' ', '-');
          int finalNumber = 0;
          
          Dictionary<string, int> additionWords = new Dictionary<string, int>{
            {"one" , 1}, {"two", 2},{"three", 3},{"four", 4},{"five", 5},{"six", 6},
            {"seven", 7},{"eight", 8},{"nine", 9},{"ten", 10},{"eleven", 11},{"twelve", 12},
            {"thirteen", 13},{"fourteen", 14},{"fifteen", 15},{"sixteen", 16},{"seventeen", 17},
            {"eighteen", 18},{"nineteen", 19},{"twenty", 20},{"thirty", 30},{"forty", 40},
            {"fifty", 50},{"sixty", 60},{"seventy", 70},{"eighty", 80},{"ninety", 90}  
          };
          
          Dictionary<string, int> multiplicationWords = new Dictionary<string, int>{
            {"hundred", 100},{"thousand", 1000},{"million", 1000000}
          };
          
          int multiplier = 1;
          
          for (int i = wordArray.Length - 1; i >= 0; i--){
            if (additionWords.ContainsKey(wordArray[i])){
              finalNumber += additionWords[wordArray[i]] * multiplier;
            }
            if (multiplicationWords.ContainsKey(wordArray[i])){          
              if (multiplicationWords[wordArray[i]] < multiplier){
                multiplier *= multiplicationWords[wordArray[i]];
              }else{
                multiplier = multiplicationWords[wordArray[i]];
              }
            }
          }    
          return finalNumber;
        }
        

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 2011-10-02
        • 2011-03-06
        • 1970-01-01
        • 2018-07-05
        • 1970-01-01
        相关资源
        最近更新 更多