Example #1
0
        private void ChangeSection(object sender, EventArgs e)
        {
            GenericPicker <SectionModel> picker = (GenericPicker <SectionModel>)sender;
            SectionModel section      = picker.SelectedItem;
            double       sectionScore = ScoringHelper.ScoreSection(section, inspection).Item3;

            sectionScoreLabel.Text = "Section score: " + (sectionScore * 100).ToString("0.00") + "%";
            setScoresColor(sectionScore, sectionScoreLabel);
            if (section.SectionParts.Count == 0)
            {
                partPicker.IsVisible     = false;
                partScoreLabel.IsVisible = false;
            }
            else
            {
                partPicker.SelectedIndexChanged -= ChangePart;
                partPicker.IsVisible             = true;
                partScoreLabel.IsVisible         = true;
                partPicker.ClearItems();
                foreach (SectionPart part in section.SectionParts)
                {
                    partPicker.AddItem(part);
                }
                partPicker.SelectedIndexChanged += ChangePart;
                partPicker.SelectedIndex         = 0;
            }
        }
    // +++ life cycle +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    void Start()
    {
        scoringHelper = new ScoringHelper();

        // subscribe to events
        nvp_EventManager_scr.INSTANCE.SubscribeToEvent(GameEvents.onBallOutOfBounds, OnBallOutOfBounds);
    }
Example #3
0
        private void SaveIndices(IList <Token> tokens, IList <string> linkTexts, UrlFile urlFile, ulong oldUrlFileId)
        {
            var invertedIndices = new List <Index>();
            var postingLists    = new List <PostingList>();

            foreach (var token in tokens)
            {
                var key = new IndexKey
                {
                    Word      = token.Word,
                    UrlFileId = urlFile.UrlFileId,
                };

                var weight = ScoringHelper.CalculateIndexWeight(urlFile.Title,
                                                                urlFile.TextContent,
                                                                urlFile.Url,
                                                                DateTime.FromBinary((long)urlFile.PublishDate),
                                                                token.OccurencesInTitle,
                                                                token.OccurencesInLinks,
                                                                linkTexts,
                                                                token.Word,
                                                                token.WordFrequency,
                                                                token.Positions);
                var index = new Index
                {
                    Key                 = key,
                    WordFrequency       = token.WordFrequency,
                    OccurencesInTitle   = token.OccurencesInTitle,
                    OccurencesInLinks   = token.OccurencesInLinks,
                    OccurencesInHeaders = token.OccurencesInHeaders,
                    Weight              = weight,
                };
                index.Positions.AddRange(token.Positions);

                invertedIndices.Add(index);

                var postingList = new PostingList
                {
                    Word              = token.Word,
                    WordFrequency     = token.WordFrequency,
                    DocumentFrequency = 1,
                    IsAdd             = true,
                };
                var posting = new Posting
                {
                    UrlFileId = urlFile.UrlFileId,
                    Weight    = weight,
                };
                postingList.Postings.Add(posting);

                postingLists.Add(postingList);
            }
            mConfig.PostingListStore.SavePostingLists(urlFile.UrlFileId, postingLists);
            mConfig.InvertedIndexStore.SaveIndices(urlFile.UrlFileId, invertedIndices);
        }
Example #4
0
        private void ChangePart(object sender, EventArgs e)
        {
            GenericPicker <SectionPart> picker = (GenericPicker <SectionPart>)sender;

            if (picker.SelectedIndex >= picker.Items.Count || picker.SelectedIndex < 0)
            {
                //this is a fix for a bug where the event listener is still listening even though it's been unassigned.
                return;
            }
            SectionPart part      = picker.SelectedItem;
            double      partScore = ScoringHelper.ScorePart(part, inspection).Item3;

            partScoreLabel.Text = "Part score: " + (partScore * 100).ToString("0.00") + "%";
            setScoresColor(partScore, partScoreLabel);
        }
Example #5
0
        public IEnumerable <Score> Rank(IEnumerable <ulong> urlFileIds, IEnumerable <string> words)
        {
            var documentCount = mConfig.UrlFileStore.GetCount();

            foreach (var urlFileId in urlFileIds)
            {
                var urlFile = mConfig.UrlFileStore.GetUrlFile(urlFileId);
                if (urlFile == null)
                {
                    yield return(new Score
                    {
                        Value = 0,
                        DebugInfo = new DebugInfo(nameof(VectorSpaceModelRanker),
                                                  "Error", "UrlFile Not Found"),
                    });

                    continue;
                }
                var titleLength   = urlFile.Title.Length + 1;
                var contentLength = urlFile.TextContent.Length + 1;

                double headerScore  = 0;
                double linkScore    = 0;
                double titleScore   = 0;
                double contentScore = 0;

                foreach (var word in words)
                {
                    var urlFileIndexStat = mConfig.InvertedIndexStore.GetIndex(urlFileId, word);

                    if (urlFileIndexStat == null)
                    {
                        continue;
                    }

                    var wordFrequencyInTitle    = urlFileIndexStat.OccurencesInTitle;
                    var wordFrequencyInLinks    = urlFileIndexStat.OccurencesInLinks;
                    var wordFrequencyInHeaders  = urlFileIndexStat.OccurencesInHeaders;
                    var wordFrequencyInDocument = urlFileIndexStat.WordFrequency;

                    var postingList       = mConfig.PostingListStore.GetPostingList(word);
                    var documentFrequency = postingList.DocumentFrequency;

                    var wordScore =
                        ScoringHelper.TfIdf(wordFrequencyInDocument, documentFrequency, documentCount);

                    var titleWordScore =
                        ScoringHelper.TfIdf(wordFrequencyInTitle, documentFrequency, documentCount);

                    var linkWordScore =
                        ScoringHelper.TfIdf(wordFrequencyInLinks, documentFrequency, documentCount);

                    var headerWordScore =
                        ScoringHelper.TfIdf(wordFrequencyInHeaders, documentFrequency, documentCount);

                    titleScore   += titleWordScore * Math.Min(1, (0.3 + word.Length / titleLength));
                    linkScore    += linkWordScore / urlFile.InLinkCount; // * word.Length / (1 + urlFile.LinkTotalLength);
                    headerScore  += headerWordScore;                     // * word.Length / (1 + urlFile.HeaderTotalLength);
                    contentScore += wordScore * Math.Min(1, (0.3 + word.Length / contentLength));
                }
                var finalScore = (ContentFactor * contentScore
                                  + TitleFactor * titleScore
                                  + LinkFactor * linkScore
                                  + HeaderFactor * headerScore)
                                 / (ContentFactor + TitleFactor + LinkFactor + HeaderFactor);

                var debugInfo = new DebugInfo(nameof(VectorSpaceModelRanker));
                debugInfo.Properties["ContentScore"] = new StringDebugInfoValue(contentScore.ToString());
                debugInfo.Properties["TitleScore"]   = new StringDebugInfoValue(titleScore.ToString());
                debugInfo.Properties["HeaderScore"]  = new StringDebugInfoValue(headerScore.ToString());
                debugInfo.Properties["LinkScore"]    = new StringDebugInfoValue(linkScore.ToString());

                yield return(new Score
                {
                    Value = finalScore,
                    DebugInfo = debugInfo,
                });
            }
        }
Example #6
0
        private static List <ReportSection> PrepareInspectionForScoring(Inspection inspection)
        {
            List <ReportSection> reportSections = new List <ReportSection>();
            double sumTotalAvailablePoints      = 0;
            double sumTotalEarnedPoints         = 0;
            bool   anyUnacceptables             = false;

            foreach (SectionModel section in inspection.Checklist.Sections)
            {
                //We can have a commendable section even with unacceptable parts.
                //bool anyUnacceptableParts = false;
                if (section.SectionParts.Any())
                {
                    double sumSectionAvailablePoints = 0;
                    double sumSectionEarnedPoints    = 0;
                    foreach (SectionPart part in section.SectionParts)
                    {
                        Tuple <double, double, double> partScores = ScoringHelper.ScorePart(part, inspection);
                        part.availablePoints = partScores.Item1;
                        part.earnedPoints    = partScores.Item2;
                        part.percentage      = partScores.Item3;
                        if (part.availablePoints > 0)
                        {
                            double percentScore = part.percentage * 100;
                            if (percentScore < inspection.Checklist.ScoreThresholdSatisfactory)
                            {
                                part.rating = Rating.Unacceptable;
                                //anyUnacceptableParts = true;
                            }
                            else if (percentScore < inspection.Checklist.ScoreThresholdCommendable)
                            {
                                part.rating = Rating.Satisfactory;
                            }
                            else
                            {
                                part.rating = Rating.Commendable;
                            }
                        }
                        sumSectionAvailablePoints += partScores.Item1;
                        sumSectionEarnedPoints    += partScores.Item2;
                    }
                    section.availablePoints = sumSectionAvailablePoints;
                    section.earnedPoints    = sumSectionEarnedPoints;
                    if (sumSectionAvailablePoints > 0)
                    {
                        section.percentage = sumSectionEarnedPoints / sumSectionAvailablePoints;
                    }
                    else
                    {
                        section.percentage = 0;
                    }
                }
                else
                {
                    Tuple <double, double, double> sectionScores = ScoringHelper.ScoreSection(section, inspection);
                    section.availablePoints = sectionScores.Item1;
                    section.earnedPoints    = sectionScores.Item2;
                    section.percentage      = sectionScores.Item3;
                }

                if (section.availablePoints > 0)
                {
                    double percentScore = section.percentage * 100;
                    if (percentScore < inspection.Checklist.ScoreThresholdSatisfactory)
                    {
                        anyUnacceptables = true;
                        section.rating   = Rating.Unacceptable;
                    }
                    else if (percentScore < inspection.Checklist.ScoreThresholdCommendable /*|| anyUnacceptableParts*/)
                    {
                        section.rating = Rating.Satisfactory;
                    }
                    else
                    {
                        section.rating = Rating.Commendable;
                    }
                }
                else
                {
                    section.rating = Rating.None;
                }

                sumTotalAvailablePoints += section.availablePoints;
                sumTotalEarnedPoints    += section.earnedPoints;
                if (true)                       //if we're supposed to render this section
                {
                    ReportSection reportSection = new ReportSection(section);
                    reportSection.PartsToRender = section.SectionParts;
                    reportSections.Add(reportSection);
                }
            }
            inspection.availablePoints = sumTotalAvailablePoints;
            inspection.earnedPoints    = sumTotalEarnedPoints;
            if (sumTotalAvailablePoints > 0)
            {
                inspection.percentage = sumTotalEarnedPoints / sumTotalAvailablePoints;
                double percentScore = inspection.percentage * 100;
                if (percentScore < inspection.Checklist.ScoreThresholdSatisfactory)
                {
                    inspection.rating = Rating.Unacceptable;
                }
                else if (percentScore < inspection.Checklist.ScoreThresholdCommendable || anyUnacceptables)
                {
                    inspection.rating = Rating.Satisfactory;
                }
                else
                {
                    inspection.rating = Rating.Commendable;
                }
            }
            else
            {
                inspection.percentage = 0;
                inspection.rating     = Rating.None;
            }
            return(reportSections);
        }
Example #7
0
        public ScoresPage(Inspection inspection, Question initialQuestion)
        {
            this.inspection = inspection;
            ChecklistModel checklist = inspection.Checklist;

            scoresThreshold = new Threshold(checklist.ScoreThresholdCommendable, checklist.ScoreThresholdSatisfactory);
            Padding         = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);

            this.Title = "Scores";
            ScoresLayout layout = new ScoresLayout
            {
                VerticalOptions   = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center
            };
            Label label = new Label
            {
                Text = "Choose section and part to see scores"
            };
            //add all the other stuff.
            GenericPicker <SectionModel> sectionPicker = new GenericPicker <SectionModel>();

            foreach (SectionModel section in inspection.Checklist.Sections)
            {
                sectionPicker.AddItem(section);
            }
            partPicker = new GenericPicker <SectionPart>();
            foreach (SectionPart part in initialQuestion.section.SectionParts)
            {
                partPicker.AddItem(part);
            }
            Button backButton = new Button
            {
                Text = "Back"
            };

            sectionScoreLabel = new Label
            {
                Text      = "Section label",
                TextColor = Color.White
            };
            partScoreLabel = new Label
            {
                Text      = "Part label",
                TextColor = Color.White
            };
            backButton.Clicked += BackButtonClicked;
            layout.Children.Add(label);
            layout.Children.Add(sectionPicker);
            layout.Children.Add(sectionScoreLabel);
            layout.Children.Add(partPicker);
            layout.Children.Add(partScoreLabel);
            double cumulativeScore      = ScoringHelper.ScoreInspection(inspection).Item3;
            Label  cumulativeScoreLabel = new Label
            {
                Text      = "Cumulative Score: " + (cumulativeScore * 100).ToString("0.00") + "%",
                TextColor = Color.White
            };

            setScoresColor(cumulativeScore, cumulativeScoreLabel);
            layout.Children.Add(cumulativeScoreLabel);
            layout.Children.Add(backButton);

            this.Content = layout;

            sectionPicker.SelectedIndexChanged += ChangeSection;
            sectionPicker.SelectedIndex         = sectionPicker.TItems.IndexOf(initialQuestion.section);
            partPicker.SelectedIndexChanged    += ChangePart;
            if (initialQuestion.part != null)
            {
                partPicker.SelectedIndex = partPicker.TItems.IndexOf(initialQuestion.part);
            }
        }