-2

Currently I am making a AI based on text. I have a database with a pattern for each answer A pattern looks like [Who is the] Winner of the World Cup 2018

[] = Optional words

<> = Needed words

When I enter the sentence Who is the Winner of the World Cup 2018 my method should return the indentifier of the answer.

My database has 2 rows called "AnswerIndentifier" and "Pattern"

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • You have stated the problem, ok. Please improve it with a clear question and the code you have tried -in order to make people reproduce it maybe. – vahdet Jul 25 '18 at 12:38
  • I didn't began coding yet. For project planning I need to know how to – VerbxteneSkillz Jul 25 '18 at 12:39
  • @VerbxteneSkillz Welcome to Stack Overflow. In order to receive help, as vahdet mentioned, you must include what you have tried and a clear question as to where you are stuck. Please try to solve and if you reach a stopping point where you are stuck, explain the problem and your method of solution along with example code as well as what you are stuck on. – Rex Charles Jul 25 '18 at 12:55

1 Answers1

0

I did it myself and programmed this algoryhm:

    private static bool MatchesPattern(string text, string pattern)
{
  List<string> patternTokens = new List<string>();
  string tok = "";
  pattern = pattern.ToLower() + "[";
  int state = 0;
  for(int i = 0; i < pattern.ToCharArray().Length; i++)
  {
    char token = pattern[i];
    if(token == '[')
    {
      if(tok != "")
      {
        patternTokens.Add("NEC" + char.MaxValue + tok);
        tok = "";
      }
      state = 1;
      continue;
    }
    if(token == ']' && state == 1)
    {
      i++;
      state = 0;
      patternTokens.Add("OPT" + char.MaxValue + tok);
      tok = "";
      continue;
    }
    if(token == ' ' && i + 1 < text.ToCharArray().Length && text[i + 1] == '[')
      continue;
    tok += token;
  }
  string[] patternTokensCopy = new string[patternTokens.Count];
  for(int i = 0; i < patternTokens.Count; i++)
    patternTokensCopy[i] = patternTokens[i];
  int max = (int) Math.Pow(2, patternTokens.Where(x => x.StartsWith("OPT")).ToList().Count);
  for(int i = 0; i < max; i++)
  {
    string binary = Convert.ToString(i, 2).PadLeft(patternTokensCopy.Where(x => x.StartsWith("OPT")).ToList().Count, '0');
    for(int x = 0; x < patternTokensCopy.Where(t => t.StartsWith("OPT")).ToList().Count; x++)
      if(binary[x] == '0')
      {
        List<string> optionalTokens = new List<string>();
        foreach(string curpattern in patternTokensCopy)
          if(curpattern.StartsWith("OPT"))
            optionalTokens.Add(curpattern);
        patternTokens.Remove(optionalTokens[x]);
      }
    string patternAsSentence = "";
    foreach(string patternToken in patternTokens)
      patternAsSentence += patternToken.Split(char.MaxValue)[1] + " ";
    while(patternAsSentence[patternAsSentence.Length - 1] == ' ')
      patternAsSentence = patternAsSentence.Substring(0, patternAsSentence.Length - 1);
    patternAsSentence = patternAsSentence.Replace("\r", "").Replace("  ", " ");
    int similarity = StringSimilarity.GetStringSimilarity(patternAsSentence, text);
    if(text.Length < 6)
    {
      if(text == patternAsSentence)
        return true;
    }
    else
    {
      if(similarity <= 6)
        return true;
    }
    patternTokens = new List<string>();
    patternTokensCopy.ToList().ForEach(x => patternTokens.Add(x));
  }

  return false;
}

The only changes are that the needed text must not marked with <> and the similarity check(see C# - Compare String Similarity)