Example #1
0
 public static Searcher GetInstance()
 {
     if (instance == null)
     {
         instance = new Searcher(WordComparer.GetInstance());
     }
     return(instance);
 }
Example #2
0
        protected int FindMatchedEntry(string partialWord, out bool hasMismatches)
        {
            // default - word with highest match rating in the list.
            hasMismatches = true;
            int idx = -1;

            List <KeyValuePair <int, string> > words = new List <KeyValuePair <int, string> > ();

            if (!string.IsNullOrEmpty(partialWord))
            {
                for (int i = 0; i < list.filteredItems.Count; i++)
                {
                    int    index = list.filteredItems[i];
                    string text  = DataProvider.GetCompletionText(index);
                    if (!ListWidget.Matches(partialWord, text))
                    {
                        continue;
                    }
                    words.Add(new KeyValuePair <int, string> (i, text));
                }
            }

            ListWindow.WordComparer comparer = new WordComparer(partialWord);
            if (words.Count > 0)
            {
                words.Sort(comparer);
                idx           = words[0].Key;
                hasMismatches = false;
                // exact match found.
                if (words[0].Value.ToUpper() == (partialWord ?? "").ToUpper())
                {
                    return(idx);
                }
            }

            if (string.IsNullOrEmpty(partialWord) || partialWord.Length <= 2)
            {
                // Search for history matches.
                for (int i = 0; i < wordHistory.Count; i++)
                {
                    string historyWord = wordHistory[i];
                    if (ListWidget.Matches(partialWord, historyWord))
                    {
                        for (int xIndex = 0; xIndex < list.filteredItems.Count; xIndex++)
                        {
                            string currentWord = DataProvider.GetCompletionText(list.filteredItems[xIndex]);
                            if (currentWord == historyWord)
                            {
                                idx = xIndex;
                                break;
                            }
                        }
                    }
                }
            }
            return(idx);
        }
Example #3
0
        public VerbChoiceVariable(string name, List <string> options, PluginEnvironment plugenv, WordComparer comparer)
            : base(name, 100.0, new POSTagger(plugenv), new GrammarParser(plugenv))
        {
            this.options  = options;
            this.verbs    = new Verbs(plugenv);
            this.comparer = comparer;

            this.bases = new List <string>();
            foreach (string option in options)
            {
                bases.Add(verbs.InputToBase(option));
            }
        }
Example #4
0
        public int[] Optimize()
        {
            if (!this.Indexed)
            {
                Reindex();
            }
            int[] remapping = new int[Count];

            int[] indices = new int[Count];
            for (int i = 0; i < indices.Length; ++i)
            {
                indices[i] = i;
            }

            // First, sort words in descending frequency order
            Array.Sort(indices, new CountDescendingComparer(this));

            // Next, within each set with the same byte length, sort by ordinal
            IComparer <int> wordComparer   = new WordComparer(this);
            int             countDone      = 0;
            int             countForLength = 1 << WordCompressor.BitsPerByte;

            do
            {
                int countToDo = Math.Min(countForLength, Count - countDone);
                Array.Sort(indices, countDone, countToDo, wordComparer);
                countDone     += countToDo;
                countForLength = countForLength << WordCompressor.BitsPerByte;
            } while (countDone < Count);

            // Look up the old index for each word to map to the new index
            for (int i = 0; i < Count; ++i)
            {
                remapping[indices[i]] = i;
            }

            // Rebuild the word set
            String8Set newSet = new String8Set(Count, Words.LengthBytes);

            for (int i = 0; i < Count; ++i)
            {
                newSet.Add(Words[indices[i]]);
            }
            Words = newSet;

            // Clear the index (rebuild if needed later)
            Index.Clear();
            Indexed = false;

            return(remapping);
        }
        public static HashSet <Word> Solver(List <string> Words)
        {
            WordComparer comparer = new WordComparer();

            HashSet <Word> accepted = new HashSet <Word>(comparer);
            HashSet <Word> rejected = new HashSet <Word>(comparer);

            foreach (string word in Words)
            {
                Word   newWord = new Word();
                UInt64 hash    = 3074457345618258791ul;
                foreach (char letter in word)
                {
                    newWord.Letters[letter - 97] += 1;
                    hash = hash ^ (letter ^ 3074457345618258799ul);
                }
                newWord.Hash   = hash;
                newWord.origin = word;

                if (rejected.Contains(newWord))
                {
                    continue;
                }
                else
                {
                    if (accepted.Contains(newWord))
                    {
                        accepted.Remove(newWord);
                        rejected.Add(newWord);
                    }
                    else
                    {
                        accepted.Add(newWord);
                    }
                }
            }

            return(accepted);
        }
Example #6
0
 public void SetUp()
 {
     _comparer = new WordComparer();
 }
Example #7
0
        public void PrepareAttack()
        {
            // Test input
            if (this.calpha == null || this.ctext == null || this.freq == null || this.lDic == null)
            {
                return;
            }

            List <Byte[]> text_in_words = this.ctext.ToSingleWords(this.calpha);

            // Add only unigue words
            WordComparer word_comparer = new WordComparer();

            for (int i = 0; i < text_in_words.Count; i++)
            {
                Word w         = new Word(i, text_in_words[i]);
                bool vorhanden = false;
                for (int j = 0; j < this.words.Count; j++)
                {
                    if (word_comparer.Compare(w, this.words[j]) == 0)
                    {
                        vorhanden = true;
                    }
                }

                if (vorhanden == false)
                {
                    this.words.Add(w);
                }

                if (this.stopFlag == true)
                {
                    return;
                }
            }

            // Look up words with same pattern in dictionary
            foreach (Word w in words)
            {
                w.Candidates = GetCandidates(w);
                if (w.Candidates == null || w.Candidates.Length == 0)
                {
                    w.Enabled = false;
                }
            }

            // Generate order of ciphertext letters
            int[] letter_frequencies = new int[this.calpha.GetAlphabetQuantity()];

            foreach (Word w in this.words)
            {
                for (int i = 0; i < w.ByteValue.Length; i++)
                {
                    letter_frequencies[w.ByteValue[i]]++;
                }
            }
            this.histogram = letter_frequencies;

            this.historder = new byte[letter_frequencies.Length];
            for (int i = 0; i < this.historder.Length; i++)
            {
                this.historder[i] = (byte)i;
            }

            for (int i = 0; i < letter_frequencies.Length; i++)
            {
                for (int j = i + 1; j < letter_frequencies.Length; j++)
                {
                    if (letter_frequencies[j] > letter_frequencies[i])
                    {
                        int helper = letter_frequencies[i];
                        letter_frequencies[i] = letter_frequencies[j];
                        letter_frequencies[j] = helper;
                        helper            = this.historder[i];
                        this.historder[i] = this.historder[j];
                        this.historder[j] = (byte)helper;
                    }
                }
            }

            // Calculate max progress
            int nr = this.words.Count;
            int v1 = 0;
            int v2 = 0;

            if (nr < 200)
            {
                for (int i = 0; i < nr; i++)
                {
                    if (nr < 70)
                    {
                        for (int j = i + 1; j < nr; j++)
                        {
                            if (nr < 30)
                            {
                                for (int t = j + 1; t < nr; t++)
                                {
                                    v2++;
                                }
                            }
                            v1++;
                        }
                    }
                }
            }
            this.maxProgressIt = (1 + nr + v1 + v2 + 300);
        }
Example #8
0
 public PhraseChoiceVariable(string name, List <List <string> > options, PluginEnvironment plugenv, WordComparer comparer)
     : base(name, 100.0, new POSTagger(plugenv), new GrammarParser(plugenv))
 {
     this.options  = options;
     this.comparer = comparer;
 }
Example #9
0
 public WordChoiceVariable(string name, List <string> options, WordComparer comparer)
     : base(name)
 {
     this.options  = options;
     this.comparer = comparer;
 }