Ejemplo n.º 1
0
        /// <summary>
        /// Create a plumber object from a depth map.
        /// </summary>
        /// <param name="reference">Reference value for plumbing.</param>
        /// <param name="getDepth">Function for obtaining the depth at a given index.</param>
        /// <param name="comparer">Function for comparing relative position of reference value to stored value.</param>
        /// <param name="limit">Number of items in the depth map.</param>
        /// <returns>The plumbing object.</returns>
        public static Plumber <T> Create(T reference, Func <T, int, int> getDepth, Func <T, int, int> comparer, int limit)
        {
            var plumber = new Plumber <T>(reference, getDepth, comparer, limit);

            plumber.FindMaximumDepth();

            return(plumber);
        }
Ejemplo n.º 2
0
        int PredictNextPhraseWord(List <int> prefix)
        {
            int word;

            if (_dictionaryList.Count != 0)
            {
                var plumber = Plumber <int[]> .Create(prefix.ToArray(), GetPrefixDepth, Comparer, _dictionaryList.Count);

                var sliceEnumerable = plumber.GetDepthSlices();
                using (var sliceEnumerator = sliceEnumerable.GetEnumerator())
                {
                    var sliceMoveNext = sliceEnumerator.MoveNext();
                    Debug.Assert(sliceMoveNext, "There must be a first slice");

                    var slice = sliceEnumerator.Current;

                    if (slice.Start != 0 || slice.Limit != _dictionaryList.Count)
                    {
                        var suggestions = GetSliceSuggestions(slice.Start, slice.Limit, slice.Limit, slice.Limit);

                        using (var enumerator = suggestions.GetEnumerator())
                        {
                            var suggestionMoveNext = enumerator.MoveNext();
                            Debug.Assert(suggestionMoveNext, "There must be a first suggestion");

                            word = enumerator.Current;

                            prefix.Insert(0, word);
                        }
                    }
                    else
                    {
                        word = -1;
                    }
                }
            }
            else
            {
                word = -1;
            }

            return(word);
        }
Ejemplo n.º 3
0
        public IEnumerable <int> MakePredictions(int count, string startsWith, params int[] prefix)
        {
            if (_dictionaryList.Count != 0)
            {
                var emitted      = new HashSet <int>();
                var emittedCount = 0;

                var plumber = Plumber <int[]> .Create(prefix, GetPrefixDepth, Comparer, _dictionaryList.Count);

                var sliceEnumerable = plumber.GetDepthSlices();
                using (var sliceEnumerator = sliceEnumerable.GetEnumerator())
                {
                    if (sliceEnumerator.MoveNext())
                    {
                        var outerSlice = sliceEnumerator.Current;

                        var suggestions = GetSliceSuggestions(outerSlice.Start, outerSlice.Limit, outerSlice.Limit, outerSlice.Limit);

                        using (var enumerator = suggestions.GetEnumerator())
                        {
                            while (emittedCount < count && enumerator.MoveNext())
                            {
                                var word       = enumerator.Current;
                                var wordEntry  = _wordList[word];
                                var wordString = wordEntry.Word;

                                if (WordUsesBeforeUsingInSuggestions <= wordEntry.UseCount && wordString.StartsWith(startsWith))
                                {
                                    Debug.Assert(!emitted.Contains(word));
                                    emitted.Add(word);

                                    yield return(word);

                                    emittedCount++;
                                }
                            }
                        }

                        while (emittedCount < count && sliceEnumerator.MoveNext())
                        {
                            var innerSlice = outerSlice;
                            outerSlice = sliceEnumerator.Current;

                            suggestions = GetSliceSuggestions(outerSlice.Start, innerSlice.Start, innerSlice.Limit, outerSlice.Limit);

                            using (var enumerator = suggestions.GetEnumerator())
                            {
                                while (emittedCount < count && enumerator.MoveNext())
                                {
                                    var word       = enumerator.Current;
                                    var wordEntry  = _wordList[word];
                                    var wordString = wordEntry.Word;

                                    if (WordUsesBeforeUsingInSuggestions <= wordEntry.UseCount && wordString.StartsWith(startsWith) && !emitted.Contains(word))
                                    {
                                        emitted.Add(word);

                                        yield return(enumerator.Current);

                                        emittedCount++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        int FindPhrasePosition(int[] prefix)
        {
            var insertPosition = Plumber <int[]> .FindInsertPosition(prefix, Comparer, _dictionaryList.Count);

            return(insertPosition);
        }