简体   繁体   English

如何创建自然语言计算器

[英]How to create natural language calculator

i'm trying to create a simple natural language calculate.我正在尝试创建一个简单的自然语言计算。 this is what i am trying to achieve, If i input the string "five plus six" the output should be answer: 11.这就是我想要实现的,如果我输入字符串“五加六”,output 应该是答案:11。

below i tried to create a Enum of numbers but for operators it does not accept strings,i use this so i can fetch and compare to the array input and covert respectively,could you please suggest what i can maybe use to store operators as words so?下面我尝试创建一个数字枚举,但对于运算符它不接受字符串,我使用它,所以我可以分别获取和比较数组输入和隐蔽,请你建议我可以用什么来将运算符存储为单词所以? or suggest an article maybe.或者建议一篇文章。

code:代码:

 public class EnumSample
  {
    enum Numbers: int
    {
      zero = 0,
      obne = 1,
      two = 2,
      three =3,
      four =4,
      five =5,
      six =6,
      seven=7,
      eight = 8,
      nine=9,
    };
    enum Operator
    {
      Add = "+",
      plus ="+",
      minus ="-",
      subtract ="-"
    };

    public static void Main()
    {
      int[] arr = new int[200];
      Console.Write("Get input");
      for (int i = 0; i < 200; i++)
      {
        Console.Write("element - {0} : ", i);
        arr[i] = Convert.ToInt32(Console.ReadLine());
      }

      foreach (int item in Enum.GetValues(typeof (Numbers)))
      {
        String name = Enum.GetName(typeof(Numbers), item);
        Console.WriteLine(name+item);
   
      }
      Console.Read();
    }
  }

I prefer use Dictionary instead Enum.我更喜欢使用字典而不是枚举。 Here is solution only for 2 value but you can figure out something by looking at this solution;这是仅适用于 2 值的解决方案,但您可以通过查看此解决方案来找出答案;

public class EnumSample
  {
    private static int Solve(string operation, int first, int second)
    {
      switch (operation)
      {
        case "plus":
        {
          return second + first;
        }
        case "add":
        {
          return second + first;
        }
        default:
          return second-first;
      }
    }
    public static void Main()
    {
      var numbersDic = new Dictionary<string, int>()
      {
        {"zero", 0},
        {"one", 1},
        {"two", 2},
        {"three", 3},
        {"four", 4},
        {"five", 5},
        {"six", 6},
        {"seven", 7},
        {"eight", 8},
        {"nine", 9}
      };
      Console.WriteLine("Get input");
      string[] word = Console.ReadLine()?.Split(' '); // spliting words {"five","plus","six"}
      
      if (word != null) 
        Console.Write(Solve(word[1], numbersDic[word[0]], numbersDic[word[2]]));
      //                plus , 5 , 6
      Console.Read();
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM