Exemplo n.º 1
0
        private void InitInteractions()
        {
            #region Lava
            DocContent.Keywords = _keywords;
            _display.RatEnter  += (s, e) =>
            {
                if (e.Look is TreeNodeLook)
                {
                    e.VisualItem.Hover = true;
                    e.VisualItem.Repaint();
                }
            };
            _display.RatLeave += (s, e) =>
            {
                if (e.Look is TreeNodeLook)
                {
                    e.VisualItem.Hover = false;
                    e.VisualItem.Repaint();
                }
            };
            _display.RatClick += (s, e) =>
            {
                if (InteractionHelper.IsControlKeyPressed())
                {
                    if (e.Look is TreeNodeLook)
                    {
                        UserLabelType type            = UserLabelType.NotRelevant;
                        var           dataNode        = GetNodeByVisNodeItem(e.VisualItem);
                        var           treeIndex       = GetNodeTreeIndex(e.VisualItem);
                        var           _datanodeScheme = _dataNodeSchemes[treeIndex];
                        var           tree            = _trees[treeIndex];
                        UserLabelType prevType        = _datanodeScheme.GetUserLabelType(dataNode);

                        switch (prevType)
                        {
                        case UserLabelType.NotRelevant:
                            type = UserLabelType.Relevant;
                            break;

                        case UserLabelType.Relevant:
                            type = UserLabelType.NotRelevant;
                            break;

                        case UserLabelType.Mixed:
                            type = UserLabelType.Relevant;
                            break;

                        default:
                            throw new NotImplementedException();
                        }

                        foreach (var desNode in tree.BFS(dataNode))
                        {
                            _datanodeScheme.SetUserLabelType(desNode, type);
                        }

                        INode ancNode = tree.GetParent(dataNode);
                        while (ancNode != null)
                        {
                            //Update user label type
                            Counter <UserLabelType> counter = new Counter <UserLabelType>();
                            foreach (var child in tree.GetChildren(ancNode))
                            {
                                counter.Add(_datanodeScheme.GetUserLabelType(child));
                            }
                            var dict = counter.GetCountDictionary();
                            if (dict.Count == 1)
                            {
                                _datanodeScheme.SetUserLabelType(ancNode, dict.Keys.ElementAt(0));
                            }
                            else
                            {
                                _datanodeScheme.SetUserLabelType(ancNode, UserLabelType.Mixed);
                            }

                            ancNode = tree.GetParent(ancNode);
                        }

                        _visNodeTables[treeIndex].RepaintAll();
                    }
                }
                else
                {
                    if (e.Look is TreeNodeLook)
                    {
                        var treeIndex   = GetNodeTreeIndex(e.VisualItem);
                        var indexReader = _dataNodeSchemes[treeIndex].GetIndexReader();
                        if (indexReader == null)
                        {
                            return;
                        }

                        var        dataNode   = _visNodeSchemes[treeIndex].GetDataGraph().GetNode(e.VisualItem.Row);
                        var        docIndices = RandomUtils.GetRandomSamples(_dataNodeSchemes[treeIndex].GetTopicLeafDocIndices(dataNode), 50, new Random(0));
                        List <Doc> documents  = new List <Doc>();
                        foreach (var docIndex in docIndices)
                        {
                            documents.Add(GetDocument(indexReader, docIndex));
                        }

                        var docListPopup = new DocList(documents);
                        docListPopup.Owner = _window;
                        //startupLocation = PointToScreen(startupLocation);
                        //docListPopup.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                        docListPopup.WindowStartupLocation = WindowStartupLocation.Manual;
                        int mouseX, mouseY;
                        AutoControlUtils.GetMousePosition(out mouseX, out mouseY);
                        docListPopup.Left = mouseX - 4;
                        docListPopup.Top  = mouseY - 4;
                        docListPopup.Show();
                    }
                }
            };
            #endregion

            #region WPF
            //_window.TestCurrentButton.Click += (s, e) =>
            //    {
            //        var indexReader = _dataNodeSchemes.GetIndexReader();
            //        if(indexReader == null)
            //            return;

            //        InitializeFeatureVectors();
            //        _dataNodeSchemes.ClearDocCount();

            //        int correctCnt = 0;
            //        int totalCnt = 0;
            //        foreach(var docIndex in _dataNodeSchemes.GetTopicLeafDocIndices(_tree.Root))
            //        {
            //            var node = GetNode(docIndex);
            //            var groundTruthLabel = _dataNodeSchemes.GetUserLabelType(node);
            //            if (groundTruthLabel == UserLabelType.Mixed)
            //                continue;
            //            INode bestMatch;
            //            var projLabel = GetProjectedUserLabelType(indexReader.Document(docIndex), out bestMatch);
            //            if (groundTruthLabel == projLabel)
            //            {
            //                correctCnt++;
            //                //INode ancNode = node;
            //                //while (ancNode != null)
            //                //{
            //                //    _dataNodeScheme.IncrementCorrectDocCount(ancNode);
            //                //    ancNode = _tree.GetParent(ancNode);
            //                //}
            //            }
            //            //{
            //            //    INode ancNode = node;
            //            //    while (ancNode != null)
            //            //    {
            //            //        _dataNodeScheme.IncrementTotalDocCount(node);
            //            //        ancNode = _tree.GetParent(ancNode);
            //            //    }
            //            //}
            //            if(groundTruthLabel == UserLabelType.Relevant)
            //            {
            //                _dataNodeSchemes.IncrementCorrectDocCount(bestMatch);
            //                INode ancNode = bestMatch;
            //                while (ancNode != null)
            //                {
            //                    _dataNodeSchemes.IncrementTotalDocCount(ancNode);
            //                    ancNode = _tree.GetParent(ancNode);
            //                }
            //            }
            //            totalCnt++;
            //        }
            //        foreach(var node in _tree.Graph.Nodes)
            //            _dataNodeSchemes.SetTotalDocCount(node, totalCnt);

            //        _window.TestCurrentTextBlock.Text = (100 * correctCnt / totalCnt) + "% (" + correctCnt + ", " + totalCnt + ") ";
            //        _visNodeTable.RepaintAll();
            //    };
            //_window.RemoveNoiseButton.Click += (s, e) =>
            //    {
            //        string indexPath = _window.IndexPathTextBox.Text;
            //        int sampleNumber = int.Parse(_window.SampleNumberTextBox.Text);
            //        var thread = new Thread(() => RemoveNoiseByCurrentLabel(indexPath, sampleNumber));
            //        thread.Start();
            //    };
            //_window.SaveUserLabelsButton.Click += (s, e) =>
            //    {
            //        var sw = new GetStreamWriter(_dataNodeSchemes.GetBRTFileName() + ".label");
            //        foreach(var node in _tree.Graph.Nodes)
            //        {
            //            var topicID = _dataNodeSchemes.GetTopicID(node);
            //            var type = _dataNodeSchemes.GetUserLabelType(node);
            //            sw.WriteLine(topicID + "\t" + type.ToString());
            //        }
            //        sw.Flush();
            //        sw.Close();
            //    };
            //_window.LoadUserLabelsButton.Click += (s, e) =>
            //{
            //    var sr = new StreamReader(_dataNodeSchemes.GetBRTFileName() + ".label");
            //    string line;
            //    while ((line = sr.ReadLine()) != null && line.Length > 0)
            //    {
            //        var tokens = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
            //        var topicID = int.Parse(tokens[0]);
            //        var type = (UserLabelType)StringOperations.ParseEnum(typeof(UserLabelType), tokens[1]);
            //        var node = _dataNodeSchemes.GetNodeByTopicID(topicID);
            //        _dataNodeSchemes.SetUserLabelType(node, type);
            //    }
            //    sr.Close();
            //    _visNodeTable.RepaintAll();
            //};
            #endregion
        }