예제 #1
0
        internal void TransferSuccessorsToRunOn(ITile selected)
        {
            // Find the selected tile.
            var index = 0;

            while (!ReferenceEquals(selected, _headItems[index]))
            {
                index++;
            }
            Debug.Assert(index <= _selectedIndex);

            var predecessor = _headItems[index];

            if (index != _selectedIndex)
            {
                var subContext = new List <int>(Context);
                subContext.RemoveRange(_selectedIndex + 1, subContext.Count - _selectedIndex - 1);
                for (var i = _selectedIndex; index < i; i--)
                {
                    AddSequenceTail(subContext, LiveSequenceWeight);
                    subContext.RemoveAt(i);
                }
            }

            var ghostIndex = index + 1;
            var ghostLimit = _headItems.Count;

            if (_headItems[ghostLimit - 1] is GhostStopItem)
            {
                ghostLimit--;
            }
            for (; ghostIndex < ghostLimit; ghostIndex++)
            {
                var oldItem  = _headItems[ghostIndex];
                var encoding = oldItem.Content;
                var newItem  = new GhostWordItem(predecessor, this, encoding);
                _headItems[ghostIndex] = newItem;
                predecessor            = newItem;
            }

            SetSelectedIndex(index);

            if (ghostLimit < _headItems.Count)
            {
                Debug.Assert(ghostLimit + 1 == _headItems.Count);

                var ghostStop = new GhostStopItem(predecessor, this);
                _headItems[ghostLimit] = ghostStop;
            }

            SetSuggestionsView();

            ParanoidAssertValid();
        }
예제 #2
0
        internal void AddWords(IEnumerable <string> words)
        {
            var newWords = false;

            ParanoidAssertValid();

            foreach (var word in words)
            {
                if (_tokens.IsNewWord(word))
                {
                    newWords = true;
                    _spellingSource.AddNewWord(word);
                }

                var item = CreateHeadWordItem(word);
                _headItems.Insert(_selectedIndex + 1, item);
                SetSelectedIndex(_selectedIndex + 1);

                AddSequenceTail(Context, PersistedSequenceWeight);
            }

            if (newWords)
            {
                PopulateVocabularyList();
            }

            var ghostLimit = _headItems.Count;

            if (_headItems[ghostLimit - 1] is GhostStopItem)
            {
                ghostLimit--;
            }

            var predecessor = _headItems[_selectedIndex];

            for (var i = _selectedIndex + 1; i < ghostLimit; i++)
            {
                var oldGhost = _headItems[i].Content;
                var newGhost = new GhostWordItem(predecessor, this, oldGhost);
                _headItems[i] = newGhost;
                predecessor   = newGhost;
            }

            if (ghostLimit != _headItems.Count)
            {
                _headItems[ghostLimit] = new GhostStopItem(predecessor, this);
            }

            ParanoidAssertValid();
        }
예제 #3
0
        internal void ShowTestCard()
        {
            // Truncate head items.
            for (var i = _headItems.Count - 1; 0 < i; i--)
            {
                _headItems.RemoveAt(i);
            }

            var headWordItem = new HeadWordItem(_headItems[0], this, "Word");

            _headItems.Add(headWordItem);

            var ghostWordItem = new GhostWordItem(_headItems[1], this, "Ghost");

            _headItems.Add(ghostWordItem);

            var ghostStopItem = new GhostStopItem(_headItems[2], this);

            _headItems.Add(ghostStopItem);
        }
예제 #4
0
        internal void ContinueRunOnSuggestions()
        {
            var context = new List <int>
            {
                0
            };
            ITile predecessor = _headItems[0];

            for (var i = 1; i <= _selectedIndex; i++)
            {
                var word = (WordItem)_headItems[i];
                Debug.Assert(predecessor == word.Predecessor);
                predecessor = word;
                var token = _tokens.GetToken(word.Content);
                context.Add(token);
            }

            var count = _headItems.Count;

            if (_headItems[count - 1] is GhostStopItem)
            {
                count--;
                _headItems.RemoveAt(count);
            }


            for (var i = _selectedIndex + 1; i < count; i++)
            {
                var word  = (GhostWordItem)_headItems[i];
                var token = _tokens.GetToken(word.Content);
                context.Add(token);

                word          = new GhostWordItem(predecessor, this, word.Content);
                _headItems[i] = word;
                predecessor   = word;
            }

            var more = true;

            while (more && _headItems.Count - _selectedIndex - 1 < MaxRunOnSuggestionsCount)
            {
                var token = GetTopToken(context.ToArray());
                if (0 < token)
                {
                    var word = _tokens.GetString(token);
                    var item = CreateGhostWordItem(predecessor, word);
                    _headItems.Add(item);

                    context.Add(token);

                    predecessor = item;
                }
                else
                {
                    if (token == 0)
                    {
                        var item = new GhostStopItem(predecessor, this);
                        _headItems.Add(item);
                    }
                    more = false;
                }
            }

            {
                // TODO: Old code seemed to just remove terminal GhostStopItem if it was present! Was that necessary?
                // was _runOnSuggestions.RemoveAt(_runOnSuggestions.Count - 1);
                // could be: _headItems.RemoveAt(headItems.Count - 1);
            }

            ParanoidAssertValid();
        }
예제 #5
0
        internal void Commit(ITile stopTile)
        {
            ParanoidAssertValid();

            // Find position from which to work backwards to rebirth ghost items.
            ITile position;

            if (_selectedIndex == 0 &&
                stopTile is GhostStopItem)
            {
                position = _headItems[_headItems.Count - 1].Predecessor;
            }
            else
            {
                position = stopTile.Predecessor;
            }

            var wordList = new List <string>();

            while (!ReferenceEquals(LastTile, position))
            {
                wordList.Insert(0, position.Content);
                position = position.Predecessor;
            }

            AddWords(wordList);

            if (_selectedIndex == 0 &&
                2 < _headItems.Count &&
                _headItems[_headItems.Count - 1] is GhostStopItem)
            {
                TransferRunOnToSuccessors(_headItems[_headItems.Count - 2]);
            }

            ParanoidAssertValid();

            var selection = new List <int>(Context)
            {
                0
            };

            RollbackAndAddSequence(selection, PersistedSequenceWeight);

            var predecessor = _headItems[0];

            for (var i = 1; i <= _selectedIndex; i++)
            {
                var selected = _headItems[i];
                var ghost    = CreateGhostWordItem(predecessor, selected.Content);
                _headItems[i] = ghost;
                predecessor   = ghost;
            }

            while (_selectedIndex + 1 < _headItems.Count)
            {
                _headItems.RemoveAt(_headItems.Count - 1);
            }

            selection.RemoveAt(0);
            selection.RemoveAt(selection.Count - 1);
            var tiles = new List <TileData>();

            foreach (var token in selection)
            {
                var tokenString = _tokens.GetString(token);
                var tile        = TileData.FromTokenString(tokenString);
                tiles.Add(tile);
            }
            var utterance = TileSequence.FromData(tiles);

            SetSelectedIndex(0);
            var tail = new GhostStopItem(predecessor, this);

            _headItems.Add(tail);

            Model.SaveUtterance(utterance);

            InitializeUtterance();
            SetSuggestionsViewComplete();

            ParanoidAssertValid();
        }