Beispiel #1
0
            public Word GetWordByIndex(WordInSentenceIndex wordIndex)
            {
                Sentence sentence = GetSentenceByIndex(wordIndex.sentenceIndex);

                if (sentence == null)
                {
                    return(null);
                }

                return(sentence.GetWordByIndex(wordIndex.wordIndex));
            }
Beispiel #2
0
            // По индексу символа из исходной строки текста получает индекс слова в предложении
            // Если вернет true, значит попали точно в слово. Если false, значит индекс символа не на слове и будет возвращен индекс БЛИЖАЙШЕГО слова
            // например если мы запрашиваем пробел после слова, то будет выдано это слово.
            public bool GetWordInSentenceIndexByTextLetterIndex(int textLetterIndex, out WordInSentenceIndex wordInSentenceIndex)
            {
                wordInSentenceIndex.sentenceIndex = -1;
                wordInSentenceIndex.wordIndex     = -1;

                // Предложений вообще нет, только в этом случае отсутсвует выходные индексы
                if (m_sentences.Count == 0)
                {
                    return(false);
                }

                if (textLetterIndex <= m_sentences[0].FirstIndex)
                {
                    wordInSentenceIndex.sentenceIndex = 0;
                    wordInSentenceIndex.wordIndex     = 0;
                    return(textLetterIndex == m_sentences[0].FirstIndex);
                }

                if (textLetterIndex >= m_sentences[m_sentences.Count - 1].LastIndex)
                {
                    wordInSentenceIndex.sentenceIndex = m_sentences.Count - 1;
                    wordInSentenceIndex.wordIndex     = m_sentences[m_sentences.Count - 1].WordCount - 1;
                    return(textLetterIndex == m_sentences[m_sentences.Count - 1].LastIndex);
                }

                for (int sentenceIdx = 0; sentenceIdx < m_sentences.Count; sentenceIdx++)
                {
                    Sentence sentence = m_sentences[sentenceIdx];

                    if (textLetterIndex >= sentence.FirstIndex && textLetterIndex <= sentence.LastIndex)
                    {
                        wordInSentenceIndex.sentenceIndex = sentenceIdx;
                        return(sentence.GetWordIndexByTextLetterIndex(textLetterIndex, out wordInSentenceIndex.wordIndex));
                    }
                    else if (textLetterIndex < sentence.FirstIndex)
                    {
                        wordInSentenceIndex.sentenceIndex = Math.Max(0, sentenceIdx - 1);
                        wordInSentenceIndex.wordIndex     = m_sentences[wordInSentenceIndex.sentenceIndex].WordCount - 1;
                        return(false);
                    }
                }

                wordInSentenceIndex.sentenceIndex = m_sentences.Count - 1;
                wordInSentenceIndex.wordIndex     = m_sentences[m_sentences.Count - 1].WordCount - 1;
                return(false);
            }