private bool tryRefreshWordPredictionsAndSetCurrentWord()
{
bool retVal = true;
if (_wordListWidgetWidget == null)
{
return true;
}
try
{
String wordAtCaret;
String nwords;
var charAtCaret = '\0';
using (var agentContext = Context.AppAgentMgr.ActiveContext())
{
// we need the word at the cursor and also the previous n-words
// in the current sentence
int caretPos = agentContext.TextAgent().GetCaretPos();
Log.Debug("Perform WordPrediction at caretPos " + caretPos);
agentContext.TextAgent().GetPrefixAndWordAtCaret(out nwords, out wordAtCaret);
Log.Debug("wordAtCaret: [" + wordAtCaret + "]");
agentContext.TextAgent().GetCharAtCaret(out charAtCaret);
Log.Debug("charAtCaret: [" + charAtCaret + "]");
}
if (String.IsNullOrEmpty(nwords) && String.IsNullOrEmpty(wordAtCaret))
{
if (_currentWordWidget != null)
{
_currentWordWidget.SetCurrentWord(String.Empty);
}
_wordListWidgetWidget.ClearEntries();
if (Common.AppPreferences.SeedWordPredictionOnNewSentence)
{
nwords = " ";
wordAtCaret = String.Empty;
}
else
{
return retVal;
}
}
Log.Debug("wordatcaret length: " + wordAtCaret.Length);
//Log.Debug("Text: [" + text + "], caretPos: " + caretPos.ToString());
Log.Debug("Prefix: [" + nwords + "]");
Log.Debug("CurrentWord: [" + wordAtCaret + "]");
// do the word prediction
if (wordAtCaret.Length > 1 && Char.IsPunctuation(wordAtCaret[0]))
{
wordAtCaret = wordAtCaret.Substring(1);
}
var predictedWordList = Context.AppWordPredictionManager.ActiveWordPredictor.Predict(nwords, wordAtCaret);
Log.Debug("predictedWordList count: " + predictedWordList.Count());
// check if the current word is a possessive word. If not, we need to create
// a possessive version of the word and add it as the last word
// in the predicton list.
var possessiveWord = String.Empty;
if (CultureInfo.CurrentCulture.Parent.Name != "pt" &&
CultureInfo.CurrentCulture.Name != "pt" &&
!String.IsNullOrEmpty(wordAtCaret) &&
Context.AppAgentMgr.CurrentEditingMode == EditingMode.Edit &&
!wordAtCaret.EndsWith("'s", StringComparison.InvariantCultureIgnoreCase) &&
(charAtCaret == '\0' || charAtCaret == 0x0D || charAtCaret == 0x0A ||
TextUtils.IsPunctuationOrWhiteSpace(charAtCaret) ||
TextUtils.IsTerminatorOrWhiteSpace(charAtCaret)))
{
possessiveWord = wordAtCaret + "'s";
}
int ii = 0;
foreach (var word in predictedWordList)
{
if (ii >= predictedWordList.Count() || ii >= WordPredictionWordCountMax)
{
break;
}
Log.Debug("setting Word for " + ii + ": " + word);
var text = formatWord(ii + 1, word);
_wordListWidgetWidget.Children.ElementAt(ii).SetText(text);
_wordListWidgetWidget.Children.ElementAt(ii).Value = word;
ii++;
}
// if there is a possessive form of the word, add
// it to the list.
if (!String.IsNullOrEmpty(possessiveWord) &&
!contains(predictedWordList, ii, possessiveWord))
{
if (ii == 0)
{
ii++;
}
int index = ii - 1;
var text = formatWord(index + 1, possessiveWord);
_wordListWidgetWidget.Children.ElementAt(index).SetText(text);
_wordListWidgetWidget.Children.ElementAt(index).Value = possessiveWord;
}
// if we had less than max words, clear out the remaining word list items
_wordListWidgetWidget.ClearEntries(ii);
if (_currentWordWidget != null)
{
Log.Debug("Calling SetCurrentWord with : [" + wordAtCaret + "]");
_currentWordWidget.SetCurrentWord(wordAtCaret);
}
}
catch (Exception ex)
{
Log.Debug(ex.ToString());
retVal = false;
}
Log.Debug("Returning");
return retVal;
}