コード例 #1
0
        private void RebuildStatisticsTable()
        {
            statisticsBox.Clear();
            // TODO-Linux: SelectionTabs isn't implemented on Mono
            statisticsBox.SelectionTabs = new int[] { 10, 300 };
            //retrieve the default UI font.
            var font = FontHeightAdjuster.GetFontForStyle(StyleServices.NormalStyleName,
                                                          FontHeightAdjuster.StyleSheetFromMediator(mediator),
                                                          Cache.DefaultUserWs, Cache.WritingSystemFactory);
            //increase the size of the default UI font and make it bold for the header.
            Font headerFont = new Font(font.FontFamily, font.SizeInPoints + 1.0f, FontStyle.Bold, font.Unit, font.GdiCharSet);

            //refresh the statisticsDescription (in case of font changes)
            statisticsBox.Text = ITextStrings.ksStatisticsView_HeaderText;

            int row              = 0;
            var textList         = InterestingTextsDecorator.GetInterestingTextList(mediator, Cache.ServiceLocator);
            int numberOfSegments = 0;
            int wordCount        = 0;
            int uniqueWords      = 0;

            Dictionary <int, int>           languageCount     = new Dictionary <int, int>();
            Dictionary <int, Set <String> > languageTypeCount = new Dictionary <int, Set <String> >();

            //for each interesting text
            foreach (var text in textList.InterestingTexts)
            {
                //if a text is deleted in Interlinear there could be a text in this list which has invalid data.
                if (text.Hvo < 0)
                {
                    continue;
                }
                //for every paragraph in the interesting text
                for (int index = 0; index < text.ParagraphsOS.Count; ++index)
                {
                    //count the segments in this paragraph
                    numberOfSegments += text[index].SegmentsOS.Count;
                    //count all the things analyzed as words
                    var words = new List <IAnalysis>(text[index].Analyses);
                    foreach (var word in words)
                    {
                        var wordForm = word.Wordform;
                        if (wordForm != null)
                        {
                            var valdWSs = wordForm.Form.AvailableWritingSystemIds;
                            foreach (var ws in valdWSs)
                            {
                                // increase the count of words(tokens) for this language
                                int count = 0;
                                if (languageCount.TryGetValue(ws, out count))
                                {
                                    languageCount[ws] = count + 1;
                                }
                                else
                                {
                                    languageCount.Add(ws, 1);
                                }
                                //increase the count of unique words(types) for this language
                                Set <String> pair;
                                if (languageTypeCount.TryGetValue(ws, out pair))
                                {
                                    //add the string for this writing system in all lower case to the set, unique count is case insensitive
                                    pair.Add(word.Wordform.Form.get_String(ws).Text.ToLower());
                                }
                                else
                                {
                                    //add the string for this writing system in all lower case to the set, unique count is case insensitive
                                    languageTypeCount.Add(ws, new Set <String> {
                                        word.Wordform.Form.get_String(ws).Text.ToLower()
                                    });
                                }
                            }
                        }
                    }
                    words.RemoveAll(item => !item.HasWordform);
                    wordCount += words.Count;
                }
            }
            // insert total word type count
            statisticsBox.Text += Environment.NewLine + Environment.NewLine + Environment.NewLine + "\t" + ITextStrings.ksStatisticsViewTotalWordTypesText + "\t";             // Todo: find the right System.?.NewLine constant

            ++row;
            //add one row for the unique words in each language.
            foreach (KeyValuePair <int, Set <String> > keyValuePair in languageTypeCount)
            {
                var ws = Cache.WritingSystemFactory.get_EngineOrNull(keyValuePair.Key);

                string labText = (ws != null ? ws.ToString() : "#unknown#") + @":";
                statisticsBox.Text += Environment.NewLine + Environment.NewLine + "\t" + labText + "\t";                 // Todo: find the right System.?.NewLine constant
                statisticsBox.Text += "" + keyValuePair.Value.Count;
                ++row;
                uniqueWords += keyValuePair.Value.Count;                 //increase the total of unique words
            }

            // next insert the word count.
            statisticsBox.Text += Environment.NewLine + Environment.NewLine + Environment.NewLine + "\t" + ITextStrings.ksStatisticsViewTotalWordTokensText + "\t";             // Todo: find the right System.?.NewLine constant
            statisticsBox.Text += wordCount;
            ++row;
            //add one row for the token count for each language.
            foreach (KeyValuePair <int, int> keyValuePair in languageCount)
            {
                var ws = Cache.WritingSystemFactory.get_EngineOrNull(keyValuePair.Key);

                string labText = (ws != null ? ws.ToString() : "#unknown#") + @":";
                statisticsBox.Text += Environment.NewLine + Environment.NewLine + "\t" + labText + "\t";                 // Todo: find the right System.?.NewLine constant
                statisticsBox.Text += "" + keyValuePair.Value;
                ++row;
            }
            statisticsBox.Text += Environment.NewLine + Environment.NewLine + Environment.NewLine + "\t" + ITextStrings.ksStatisticsViewTotalSentencesText + "\t";             // Todo: find the right System.?.NewLine constant

            // next insert the sentence count.
            statisticsBox.Text += numberOfSegments;

            // insert the total word type count into the richTextBox (it wasn't available earlier)
            statisticsBox.SelectionStart = statisticsBox.Find(ITextStrings.ksStatisticsViewTotalWordTypesText) +
                                           ITextStrings.ksStatisticsViewTotalWordTypesText.Length;
            statisticsBox.SelectionLength = 1;
            statisticsBox.SelectedText    = "\t" + uniqueWords;

            // Set the font for the header. Do this after we add the other stuff to make sure
            // it doesn't apply to extra text added adjacent to it.
            statisticsBox.Select(0, ITextStrings.ksStatisticsView_HeaderText.Length);
            statisticsBox.SelectionFont = headerFont;
            statisticsBox.Select(0, 0);
        }