private void FillWordsTable()
 {
     for (int i = 0; i < FunctionsList.Count; i++)
     {
         Funct f = FunctionsList[i];
         foreach (string word in f.FirstClassWords)
         {
             if (!wordsTable.Keys.Contains(word))
             {
                 wordsTable.Add(word, new wordFunctions());
             }
             if (!wordsTable[word].firstClassFunctions.Contains(i))
             {
                 wordsTable[word].AddTofirstClass(i);
             }
         }
         foreach (string word in f.SecondClassWords)
         {
             if (!wordsTable.Keys.Contains(word))
             {
                 wordsTable.Add(word, new wordFunctions());
             }
             if (!wordsTable[word].SecondClassFunctions.Contains(i))
             {
                 wordsTable[word].AddToSecondClass(i);
             }
         }
     }
 }
        public CaptchaSolver()
        {
            Funct f0 = new Funct(SampleFunctions.ListWordContainLetterOrWhereLetterIsAt, SampleFunctions.f1RegexClues, SampleFunctions.f1simpleClues);
            Funct f1 = new Funct(SampleFunctions.ObjectAtPositionInList, SampleFunctions.ObjectAtPositionInListReq, SampleFunctions.ObjectAtPositionInListOpt);
            Funct f2 = new Funct(SampleFunctions.WeekendDay, SampleFunctions.WeekendDayReq, SampleFunctions.WeekendDayOpt);
            Funct f3 = new Funct(SampleFunctions.ListHowManyObject, SampleFunctions.f3RegexClues, SampleFunctions.f3simpleClues);
            Funct f4 = new Funct(SampleFunctions.LetterAtPosition, SampleFunctions.f4neededwords, SampleFunctions.f4second);
            Funct f5 = new Funct(SampleFunctions.ObjectInSeq, SampleFunctions.ObjectInSeqReq, SampleFunctions.ObjectInSeqOpt);
            Funct f6 = new Funct(SampleFunctions.SmallestNumber, SampleFunctions.f8NeededWords, SampleFunctions.f8OtherWords);
            Funct f7 = new Funct(SampleFunctions.NumberOfLettersInAWord, SampleFunctions.f5neededwords, SampleFunctions.f5second);
            Funct f8 = new Funct(SampleFunctions.DetermineDay, SampleFunctions.DetermineDayRequiredWords, SampleFunctions.DetermineDayOtherWords);
            Funct f9 = new Funct(SampleFunctions.NumberAsDigits, SampleFunctions.NumberAsDigitsRequired, SampleFunctions.NumberAsDigitsOther);
            Funct f10 = new Funct(SampleFunctions.GetCapitalWord, SampleFunctions.WordInCapitalsRequiredWords, SampleFunctions.WordInCapitalsAnotherWords);
            Funct f11 = new Funct(SampleFunctions.wordDoNotBelong, SampleFunctions.wordDoNotBelongREQ, SampleFunctions.wordDoNotBelongOPT);
            Funct f12 = new Funct(SampleFunctions.TypeWord, SampleFunctions.TypeWordREQ, SampleFunctions.TypeWordOPT);
            Funct f13 = new Funct(SampleFunctions.GetMissingWord, SampleFunctions.GetMissingWordReq, SampleFunctions.GetMissingWordOPT);

            FunctionsDetails functions = new FunctionsDetails(new List<Funct>() { f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13 });
            solver = new LogicCaptchaSolver(functions);
        }
        public CaptchaSolver()
        {
            Funct f0  = new Funct(SampleFunctions.ListWordContainLetterOrWhereLetterIsAt, SampleFunctions.f1RegexClues, SampleFunctions.f1simpleClues);
            Funct f1  = new Funct(SampleFunctions.ObjectAtPositionInList, SampleFunctions.ObjectAtPositionInListReq, SampleFunctions.ObjectAtPositionInListOpt);
            Funct f2  = new Funct(SampleFunctions.WeekendDay, SampleFunctions.WeekendDayReq, SampleFunctions.WeekendDayOpt);
            Funct f3  = new Funct(SampleFunctions.ListHowManyObject, SampleFunctions.f3RegexClues, SampleFunctions.f3simpleClues);
            Funct f4  = new Funct(SampleFunctions.LetterAtPosition, SampleFunctions.f4neededwords, SampleFunctions.f4second);
            Funct f5  = new Funct(SampleFunctions.ObjectInSeq, SampleFunctions.ObjectInSeqReq, SampleFunctions.ObjectInSeqOpt);
            Funct f6  = new Funct(SampleFunctions.SmallestNumber, SampleFunctions.f8NeededWords, SampleFunctions.f8OtherWords);
            Funct f7  = new Funct(SampleFunctions.NumberOfLettersInAWord, SampleFunctions.f5neededwords, SampleFunctions.f5second);
            Funct f8  = new Funct(SampleFunctions.DetermineDay, SampleFunctions.DetermineDayRequiredWords, SampleFunctions.DetermineDayOtherWords);
            Funct f9  = new Funct(SampleFunctions.NumberAsDigits, SampleFunctions.NumberAsDigitsRequired, SampleFunctions.NumberAsDigitsOther);
            Funct f10 = new Funct(SampleFunctions.GetCapitalWord, SampleFunctions.WordInCapitalsRequiredWords, SampleFunctions.WordInCapitalsAnotherWords);
            Funct f11 = new Funct(SampleFunctions.wordDoNotBelong, SampleFunctions.wordDoNotBelongREQ, SampleFunctions.wordDoNotBelongOPT);
            Funct f12 = new Funct(SampleFunctions.TypeWord, SampleFunctions.TypeWordREQ, SampleFunctions.TypeWordOPT);
            Funct f13 = new Funct(SampleFunctions.GetMissingWord, SampleFunctions.GetMissingWordReq, SampleFunctions.GetMissingWordOPT);

            FunctionsDetails functions = new FunctionsDetails(new List <Funct>()
            {
                f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13
            });

            solver = new LogicCaptchaSolver(functions);
        }
        public string SelectFunction(string[] words, string originalText)
        {
            InitializeFuncStatsTable();


            foreach (string word in words)
            {
                IEnumerable <string> matches = from n in functionList.wordsTable.Keys
                                               where Regex.IsMatch(word, "^" + n + "$")
                                               select n;
                if (matches.Count() > 0)
                {
                    foreach (string wordmatched in matches)
                    {
                        this.UpdateStats(functionList.wordsTable[wordmatched], wordmatched, word);
                    }
                }
            }


            //get statisfied functions;
            IEnumerable <FunctionStats> Appropriatefunctions = from n in FunctionsStats
                                                               where n.requiredWords.Count == 0
                                                               orderby n.Score
                                                               select n;

            if (Appropriatefunctions.Count() < 1)
            {
                throw new Exception("no function to select");
            }



            Appropriatefunctions = Appropriatefunctions.Reverse();
            int           max = Appropriatefunctions.ElementAt(0).Score;
            FunctionStats rightFunctionStats = Appropriatefunctions.ElementAt(0);
            int           i = 0;

            while ((rightFunctionStats = Appropriatefunctions.ElementAt(i)).Score == max)
            {
                string[]             keywords      = rightFunctionStats.words.ToArray();
                int                  functionIndex = rightFunctionStats.functionIndex;
                Funct                function      = functionList.FunctionsList[functionIndex];
                IEnumerable <string> data          = from n in words
                                                     where !keywords.Contains(n)
                                                     select n;
                try
                {
                    string resultat = function.executeFunction(keywords, data.ToArray(), originalText, words);
                    return(resultat);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    i++;
                }
            }
            throw new Exception("no function can be executed");
        }