コード例 #1
0
        public void Do_ShouldReturnACollectionOfNGrams_WhenProperArguments()
        {
            // Arrange
            // Act
            List <INGram> actual1 = new NGramTokenizer()
                                    .Do(
                ObjectMother.NGramTokenizer_Text1,
                new TokenizationStrategy(),
                new NGramTokenizerRuleSet());
            List <INGram> actual2 = new NGramTokenizer()
                                    .Do(
                ObjectMother.NGramTokenizer_Text1,
                new TokenizationStrategy());
            List <INGram> actual3 = new NGramTokenizer()
                                    .Do(
                ObjectMother.NGramTokenizer_Text1,
                new NGramTokenizerRuleSet());
            List <INGram> actual4 = new NGramTokenizer()
                                    .Do(
                ObjectMother.NGramTokenizer_Text1);

            // Assert
            Assert.IsTrue(
                ObjectMother.AreEqual(
                    ObjectMother.NGramTokenizer_Text1_NGrams,
                    actual1));
            Assert.IsTrue(
                ObjectMother.AreEqual(
                    ObjectMother.NGramTokenizer_Text1_NGrams,
                    actual2));
            Assert.IsTrue(
                ObjectMother.AreEqual(
                    ObjectMother.NGramTokenizer_Text1_NGrams,
                    actual3));
            Assert.IsTrue(
                ObjectMother.AreEqual(
                    ObjectMother.NGramTokenizer_Text1_NGrams,
                    actual4));
        }
コード例 #2
0
        public void PredictLabel_ShouldReturnANullLabelAndLogTheExpectedMessages_WhenContainsAllIndexAverageValuesEqualToZero()
        {
            // Arrange
            List <string>   actualLogMessages = new List <string>();
            Action <string> fakeLoggingAction
                = (message) => actualLogMessages.Add(message);
            TextClassifierComponents components
                = new TextClassifierComponents(
                      new NGramTokenizer(),
                      new SimilarityIndexCalculatorJaccard(),
                      TextClassifierComponents.DefaultRoundingFunction,
                      TextClassifierComponents.DefaultTextTruncatingFunction,
                      fakeLoggingAction);
            TextClassifier textClassifier = new TextClassifier(components, new TextClassifierSettings());
            string         truncatedText
                = TextClassifierComponents.DefaultTextTruncatingFunction.Invoke(
                      ObjectMother.TextClassifier_Text3,
                      TextClassifierSettings.DefaultTruncateTextInLogMessagesAfter);
            List <string> expectedLogMessages = new List <string>()
            {
                MessageCollection.TextClassifier_AttemptingToPredictLabel,
                MessageCollection.TextClassifier_FollowingTextHasBeenProvided.Invoke(truncatedText),
                MessageCollection.TextClassifier_FollowingTokenizationStrategyWillBeUsed.Invoke(new TokenizationStrategy()),
                MessageCollection.TextClassifier_FollowingNGramsTokenizerRuleSetWillBeUsed.Invoke(new NGramTokenizerRuleSet()),
                MessageCollection.TextClassifier_XLabeledExamplesHaveBeenProvided.Invoke(ObjectMother.TextClassifier_LabeledExamples),
                MessageCollection.TextClassifier_ProvidedTextHasBeenTokenizedIntoXNGrams.Invoke(ObjectMother.TextClassifier_Text3_NGrams),

                MessageCollection.TextClassifier_ComparingProvidedTextAgainstFollowingLabeledExample.Invoke(ObjectMother.TextClassifier_LabeledExamples[0]),
                MessageCollection.TextClassifier_CalculatedSimilarityIndexValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes[0].Value),
                MessageCollection.TextClassifier_RoundedSimilarityIndexValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes[0].Value),
                MessageCollection.TextClassifier_FollowingSimilarityIndexObjectHasBeenAddedToTheList.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes[0]),

                MessageCollection.TextClassifier_ComparingProvidedTextAgainstFollowingLabeledExample.Invoke(ObjectMother.TextClassifier_LabeledExamples[1]),
                MessageCollection.TextClassifier_CalculatedSimilarityIndexValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes[1].Value),
                MessageCollection.TextClassifier_RoundedSimilarityIndexValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes[1].Value),
                MessageCollection.TextClassifier_FollowingSimilarityIndexObjectHasBeenAddedToTheList.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes[1]),

                MessageCollection.TextClassifier_TokenizedTextHasBeenComparedAgainstTheProvidedLabeledExamples,
                MessageCollection.TextClassifier_XSimilarityIndexObjectsHaveBeenComputed.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexes),

                MessageCollection.TextClassifier_FollowingUniqueLabelsHaveBeenFound.Invoke(ObjectMother.TextClassifier_Text3_UniqueLabels),

                MessageCollection.TextClassifier_CalculatingIndexAverageForTheFollowingLabel.Invoke(ObjectMother.TextClassifier_Text3_UniqueLabels[0]),
                MessageCollection.TextClassifier_CalculatedSimilarityIndexAverageValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages[0].Value),
                MessageCollection.TextClassifier_RoundedSimilarityIndexAverageValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages[0].Value),
                MessageCollection.TextClassifier_FollowingSimilarityIndexAverageObjectHasBeenAddedToTheList.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages[0]),

                MessageCollection.TextClassifier_CalculatingIndexAverageForTheFollowingLabel.Invoke(ObjectMother.TextClassifier_Text3_UniqueLabels[1]),
                MessageCollection.TextClassifier_CalculatedSimilarityIndexAverageValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages[1].Value),
                MessageCollection.TextClassifier_RoundedSimilarityIndexAverageValueIs.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages[1].Value),
                MessageCollection.TextClassifier_FollowingSimilarityIndexAverageObjectHasBeenAddedToTheList.Invoke(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages[1]),

                MessageCollection.TextClassifier_XSimilarityIndexAverageObjectsHaveBeenComputed(ObjectMother.TextClassifier_Text3_SimilarityIndexAverages),

                MessageCollection.TextClassifier_FollowingVerificationHasFailed.Invoke("ContainsAtLeastOneIndexAverageThatIsNotZero"),

                MessageCollection.TextClassifier_PredictedLabelIs.Invoke(ObjectMother.TextClassifier_Text3_Label),
                MessageCollection.TextClassifier_PredictionHasFailedTryIncreasingTheAmountOfProvidedLabeledExamples
            };

            // Act
            TextClassifierResult actual
                = textClassifier.PredictLabel(
                      ObjectMother.TextClassifier_Text3,
                      ObjectMother.TextClassifier_LabeledExamples);

            // Assert
            Assert.IsTrue(
                ObjectMother.AreEqual(
                    ObjectMother.TextClassifier_Text3_TextClassifierResult,
                    actual
                    ));
            Assert.AreEqual(expectedLogMessages, actualLogMessages);
        }