public void CountWordsFindsTwoWordsInASimpleTwoWordSentence()
        {
            var sentence = "hello world";
            var cut = new WordCounter(sentence);

            Assert.AreEqual(2, cut.CountWords().Count);
        }
Ejemplo n.º 2
0
        public void CountWordsReturnCorrectCountIgnoringCaseTest()
        {
            // Arrange
            var word1 = Guid.NewGuid().ToString();
            var word2 = Guid.NewGuid().ToString();
            var word3 = "ABCd";
            var word3AllLowercase = "abcd";

            var words = new List<string>
            {
                word1,
                word2,
                word3,
                word3AllLowercase,
                word2,
                word2
            };

            // Act
            var counter = new WordCounter();
            var result = counter.CountWords(words);

            // Assert
            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(1, result[word1]);
            Assert.AreEqual(3, result[word2]);
            Assert.AreEqual(2, result[word3AllLowercase]);
            Assert.IsFalse(result.ContainsKey(word3));
        }
        public void CountWordsProcessesFindsAWordInASingleWordSentence()
        {
            var sentence = "Test";
            var cut = new WordCounter(sentence);

            Assert.AreEqual(1, cut.CountWords().Count);
        }
        public void CountWordsFindsAWordInASingleWordSentenceWithPunctuation()
        {
            var sentence = "Test!";
            var cut = new WordCounter(sentence);

            Assert.AreEqual(1, cut.CountWords().Count);
        }
        public void EmptyInputTest()
        {
            var wordCounter = new WordCounter(string.Empty);
            var result = wordCounter.Count();

            Assert.AreEqual(0, result.Count);
        }
Ejemplo n.º 6
0
        static int Main(string[] args)
        {
            string sentence = "";
            if (args.Length == 0)
            {
                Console.WriteLine("Please enter a sentence to parse:");
                sentence = Console.ReadLine();
            }
            else if (args.Length == 1)
            {
                sentence = args[0];
            }
            else
            {
                Console.WriteLine("Only one argument is expected.\nExample: WordCountApp.exe \"This is a statement, and so is this.\" ");
                return -1;
            }

            IWordCounter wc = new WordCounter();
            Dictionary<string, uint> actResult = null;
            if (wc.TryCalculate(sentence, out actResult))
            {
                foreach (KeyValuePair<string, uint> item in actResult)
                {
                    Console.WriteLine("{0} - {1}", item.Key, item.Value);
                }
                return 0;
            }
            else
            {
                Console.WriteLine("'{0}' is not a valid sentence.", sentence);
                return -1;
            }
        }
        public void CountWordsReturnsAnEmptyDictionaryForNullInput()
        {
            string nullSentence = null;
            var cut = new WordCounter(nullSentence);
            var expectedResult = new Dictionary<string, int>();

            Assert.AreEqual(expectedResult, cut.CountWords());
        }
        public void CountWordsReturnsAnEmptyDictionaryForEmptyInput()
        {
            var emptySentence = String.Empty;
            var cut = new WordCounter(emptySentence);
            var expectedResult = new Dictionary<string, int>();

            Assert.AreEqual(expectedResult, cut.CountWords());
        }
        public void NumbersInWordsTest()
        {
            var wordCounter = new WordCounter("a a123 a");
            var result = wordCounter.Count();

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(3, result["a"]);
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            string str = "Вот дом, Который построил Джек. А это пшеница, Которая в темном чулане хранится В доме, Который построил Джек. А это веселая птица-синица, Которая часто ворует пшеницу, Которая в темном чулане хранится В доме, Который построил Джек";
            WordCounter wc = new WordCounter(str);
            wc.MakeStatics();
            ShowStatistics(wc);

        }
        public void IgnoreCaseTest()
        {
            var wordCounter = new WordCounter("This this thiS");
            var result = wordCounter.Count();

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(3, result["this"]);
        }
Ejemplo n.º 12
0
        public void ShouldBeAbleToAddAWord()
        {
            IWordCounter wordCounter = new WordCounter(wordDelimiterCharacters);
            wordCounter.Add("test");

            int expectedResult = 1;

            Assert.AreEqual(expectedResult, wordCounter.GetTotalNumberOfWords());
        }
        public void CountWordsFindsADuplicateWordInATwoWordSentenceWithAmplePunctuation()
        {
            var sentence = "hello, !@#$%^&, hello";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 2}};

            Assert.AreEqual(expectedResult, result);
        }
Ejemplo n.º 14
0
        public void ShouldBeAbleToAddAScentence()
        {
            IWordCounter wordCounter = new WordCounter(wordDelimiterCharacters);
            wordCounter.Add("This is a test.");

            int expectedResult = 4;

            Assert.AreEqual(expectedResult, wordCounter.GetTotalNumberOfWords());
        }
        public void CountWordsFindsADuplicateWordInATwoWordSentenceWithDifferentCases()
        {
            var sentence = "hello Hello";
            var cut = new WordCounter(sentence);
            var result = cut.CountWords();
            var expectedResult = new Dictionary<string, int> {{"hello", 2}};

            Assert.AreEqual(expectedResult, result);
        }
Ejemplo n.º 16
0
        public void IsInvalidFilePath_ThrowException()
        {
            //Creating a MOQ object of ILogger interface to instantiate the WordCounter
            //Arrange
            var logger = new Mock <ILoggerFactory>().Object;
            //Act
            var wordCounter = new WordCounter(logger);

            //Assert
            wordCounter.CountWords(String.Empty);
        }
Ejemplo n.º 17
0
        public ActionResult Results()//WordCounter newFind)
        {
            //var otherResult = new WordCount() { Text ="" };
            //otherResult.Text = "";
            string      text    = Request.Form["text"];
            string      find    = Request.Form["find"];
            WordCounter newFind = new WordCounter(text, find);

            newFind.CountWordInstances();
            return(View(newFind));
        }
Ejemplo n.º 18
0
        public void IsCaseInsensitive()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("single SINGLE sinGLE");

            // Assert
            Assert.That(wc.Counts.Single(w => w.Key == "single").Value, Is.EqualTo(3));
        }
Ejemplo n.º 19
0
        public void WordCounter_GetArray_ArrayWithoutPunct()
        {
            WordCounter testWords = new WordCounter("cat", "The cat is in the cathedral.");

            string[] result   = testWords.RemovePunctuation(testWords.Sentence);
            string[] expected = new string[] { "the", "cat", "is", "in", "the", "cathedral", "" };
            for (int i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(result[i], expected[i]);
            }
        }
Ejemplo n.º 20
0
        public void ReturnsZeroWhenNoWordBoundary()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            var result = wc.LastPositionOfWordBoundary("one");

            // Assert
            Assert.That(result, Is.EqualTo(0));
        }
Ejemplo n.º 21
0
        public void ReturnsOneWhenOnlyOneWord()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            wc.CountWords("single");

            // Assert
            Assert.That(wc.Counts.Single(w => w.Key == "single").Value, Is.EqualTo(1));
        }
Ejemplo n.º 22
0
        public void ReturnsFinalIndexOfWordBoundaryWhenOneExists()
        {
            // Arrange
            var wc = new WordCounter();

            // Act
            var result = wc.LastPositionOfWordBoundary("one two three");

            // Assert
            Assert.That(result, Is.EqualTo(8));
        }
Ejemplo n.º 23
0
        private static void CountText(string text, int expectedCount, int?expectedCharCount = null)
        {
            var wordCounter = new WordCounter(text);

            Assert.AreEqual(expectedCount, wordCounter.Words);

            if (expectedCharCount.HasValue)
            {
                Assert.AreEqual(expectedCharCount, wordCounter.Chars);
            }
        }
        public void MultipleLineInputTest()
        {
            var wordCounter = new WordCounter("This is a test.\r\nThis is a test.\nThis is a test.");
            var result = wordCounter.Count();

            Assert.AreEqual(4, result.Count);
            Assert.AreEqual(3, result["this"]);
            Assert.AreEqual(3, result["is"]);
            Assert.AreEqual(3, result["a"]);
            Assert.AreEqual(3, result["test"]);
        }
Ejemplo n.º 25
0
        public void TestCalc_Pass()
        {
            //arrange
            int         expectedCountValPass = 1;
            WordCounter testCalcPass         = new WordCounter();
            //act
            int actualCountValPass = testCalcPass.WordCounterCalc
                                         ("random", "This is a very RaNdom sentence.");

            //assert
            Assert.AreEqual(expectedCountValPass, actualCountValPass);
        }
Ejemplo n.º 26
0
        public void UserWord_FalseIfNumber_Bool()
        {
            // PASSED
            WordCounter testWordCount = new WordCounter();
            string      testWord      = "5";
            bool        expected      = false;

            testWordCount.SetWord(testWord);
            bool actual = testWordCount.WordLettersOnly(testWord);

            Assert.AreEqual(expected, actual);
        }
        public ActionResult Result()
        {
            string inputWord   = Request.Form["input-word"];
            string inputPhrase = Request.Form["input-phrase"];

            WordCounter newWordCounter = new WordCounter();

            int userCounter = newWordCounter.WordCounterCalc
                                  (inputWord, inputPhrase);

            return(View("../WordCounter/Result", userCounter));
        }
Ejemplo n.º 28
0
 static void ShowStatistics(WordCounter wc)
 {
     int uniqueWords = 0;
     int allWords = 0;
     foreach (var obj in wc.GetDict())
     {
         allWords += obj.Value;
         if (obj.Value == 1) ++uniqueWords;
         Console.WriteLine("{0} {1}", obj.Key, obj.Value);
     }
     Console.WriteLine("всего слов {0} уникальных {1}", allWords, uniqueWords);
 }
Ejemplo n.º 29
0
        static void Main()
        {
            var bookBuilder   = new BookBuilder();
            var consoleWriter = new ConsoleWriter();

            var text   = TextReader.ReadText(path);
            var myBook = bookBuilder.PopulateBook(text);

            var wordCounter = new WordCounter(myBook);

            consoleWriter.WriteToConsole(wordCounter.CreateReport());
        }
Ejemplo n.º 30
0
        public void TestCalc_Fail()
        {
            //arrange
            int         expectedCountValFail = 0;
            WordCounter testCalcFail         = new WordCounter();
            //act
            int actualCountValFail = testCalcFail.WordCounterCalc
                                         ("ask", "Hello, I am failing. Don't ask me why.");

            //assert
            Assert.AreEqual(expectedCountValFail, actualCountValFail);
        }
Ejemplo n.º 31
0
        public void CheckWordAgainstSentenceWords_OccurenceRaisesScore_Int()
        {
            //PASSED
            WordCounter testWordCount = new WordCounter();
            string      lowCaseWord   = "bang";

            string[] compareWords = { "chitty", "bangbang", "bang", "bang" };
            int      expected     = 2;
            int      actual       = testWordCount.WordOccurs(compareWords, lowCaseWord);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 32
0
        public void GetSetUserWord_SetsUserWordToLower_String()
        {
            // PASSED
            WordCounter testWordCount = new WordCounter();
            string      testWord      = "CRASH";
            string      expected      = "crash";

            testWordCount.SetWord(testWord);
            string actual = testWordCount.GetWord();

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 33
0
        public void UserWord_UserWordNoSpaces_Bool()
        {
            // PASSED
            WordCounter testWordCount = new WordCounter();
            string      testWord      = "cr ash";
            bool        expected      = false;

            testWordCount.SetWord(testWord);
            bool actual = testWordCount.WordNoSpaces(testWord);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 34
0
        static void Main(string[] args)
        {
            var counter = new WordCounter();

            var manager = new FileManager();

            var words = counter.CountWords(manager.GetLines("data.txt"));

            var sorter = new WordCollectionSorter();

            manager.SaveResult("results.txt", sorter.GetLines(words));
        }
        public void RepeatCounterMethod_ForOne_Word()
        {
            //Arrange
            string      word  = "manasa";
            WordCounter word1 = new WordCounter(word, word);

            //Act
            int result = word1.GetRepeatCounter();

            //Assert
            Assert.AreEqual(1, result);
        }
Ejemplo n.º 36
0
        public void TestLongSentence()
        {
            var results = WordCounter.Count("Word is case insensitive, i.e. \"file\", \"FILE\" and \"File\" are considered the same word.");

            Assert.AreEqual(results.Count, 6);
            Assert.AreEqual(results["word"], 2);
            Assert.AreEqual(results["case"], 1);
            Assert.AreEqual(results["insensitive"], 1);
            Assert.AreEqual(results["file"], 3);
            Assert.AreEqual(results["considered"], 1);
            Assert.AreEqual(results["same"], 1);
        }
Ejemplo n.º 37
0
        public void WordCounter_IgnoresCase()
        {
            string[] inputData = new[] { "one", "two", "three", "One" };
            IWordCounter counter = new WordCounter();
            var result = counter.Count(inputData);

            Assert.AreEqual(3, result.Keys.Count);

            Assert.AreEqual(2, result["one"]);
            Assert.AreEqual(1, result["two"]);
            Assert.AreEqual(1, result["three"]);
        }
        public void GetWord()
        {
            //Arrange
            string      word  = "cat";
            WordCounter word1 = new WordCounter(word, word);

            //Act
            string result = word1.GetWord();

            //Assert
            Assert.AreEqual(word, result);
        }
Ejemplo n.º 39
0
        public void GetSetUserWords_SetsUserWordToLower_String()
        {
            // PASSED
            WordCounter testWordCount = new WordCounter();
            string      testWords     = "CRASH BANG BOOM";
            string      expected      = "crash bang boom";

            testWordCount.SetWords(testWords);
            string actual = testWordCount.GetWords();

            Assert.AreEqual(expected, actual);
        }
        public void ShouldReturnCountOfWords()
        {
            FileInfo file = new FileInfo("tmp");

            File.WriteAllText(file.FullName, "Keep the bar green to keep the code clean.");

            var counter = new WordCounter(file);

            Assert.Equal(9, counter.NumberOfWords());

            file.Delete();
        }
        public void GetRepeatCounterMethod_ForFour_Words()
        {
            //Arrange
            string      word     = "the";
            string      sentence = "the The THe thE .";
            WordCounter word1    = new WordCounter(word, sentence);

            //Act
            int result = word1.GetRepeatCounter();

            //Assert
            Assert.AreEqual(4, result);
        }
 public void CountWordsWithRemovalOfPunctuation()
 {
     var wc = new WordCounter(new Mock<ILogger>().Object, new Mock<IConfig>().Object);
     var res = wc.CountWordsInStatement("Hello, I am Slava!", true);
     Assert.IsNotNullOrEmpty(res.Statement);
     Assert.IsFalse(res.HasError);
     Assert.IsNull(res.Error);
     Assert.IsTrue("Hello, I am Slava!".Equals(res.Statement));
     Assert.IsTrue(res.ToString().Contains("HELLO - 1"));
     Assert.IsTrue(res.ToString().Contains("I - 1"));
     Assert.IsTrue(res.ToString().Contains("AM - 1"));
     Assert.IsTrue(res.ToString().Contains("SLAVA - 1"));
 }
Ejemplo n.º 43
0
        public static void Main()                                //private = this function can't access it
        {
            string      userInput  = "apple";                    //instantiating a new word
            WordCounter newCounter = new WordCounter(userInput); //instantiating WordCounter

            newCounter.AddWordToList(userInput);                 //Adding words to list
            newCounter.LetterCounter();
            newCounter.PrintWords();

            // string userWord = Console.ReadLine();
            // newCounter.AddWordToList(userWord);
            // newCounter.PrintWords();
        }
        public void GeneralCountTest()
        {
            var wordCounter = new WordCounter("This is a statement, and so is this.");
            var result = wordCounter.Count();

            Assert.AreEqual(6, result.Count);
            Assert.AreEqual(2, result["this"]);
            Assert.AreEqual(2, result["is"]);
            Assert.AreEqual(1, result["a"]);
            Assert.AreEqual(1, result["statement"]);
            Assert.AreEqual(1, result["and"]);
            Assert.AreEqual(1, result["so"]);
        }
Ejemplo n.º 45
0
        public void Setup()
        {
            bookModel = new BookModel
            {
                WholeBook   = "This is a test",
                WordsInBook = new List <string>()
                {
                    "This", "is", "a", "test"
                }
            };

            wordCounter = new WordCounter(bookModel);
        }
        public void GetRepeatCounterMethod_ForNoSentence()
        {
            //Arrange
            string      word     = "the";
            string      sentence = "";
            WordCounter word1    = new WordCounter(word, sentence);

            //Act
            int result = word1.GetRepeatCounter();

            //Assert
            Assert.AreEqual(0, result);
        }
        public void GetTargetWord_ReturnTargetWord_String()
        {
            //Arrange
            string      testTarget  = "the";
            WordCounter testCounter = new WordCounter(testTarget);

            //Act

            string result = testCounter.GetTargetWord();

            //Assert
            Assert.AreEqual(testTarget, result);
        }
        public void CheckSplitPhrase_ReturnNumberOfMatches_Int()
        {
            //Arrange
            string      testWord    = "The";
            string      testPhrase  = "The Cat walked down THe street to THE store.";
            WordCounter testCounter = new WordCounter();

            //Act
            int result = testCounter.CheckSplitPhrase(testWord, testPhrase);

            //Assert
            Assert.AreEqual(3, result);
        }
Ejemplo n.º 49
0
        public void GetNumbers_GetListOfNumbers_CharList()
        {
            //Arrange
            WordCounter newWordCounter = new WordCounter("test", "test sentence");
            List <char> comparisonList = new List <char> {
                '0', '2', '3', '4', '5', '6', '7', '8', '9', '1'
            };
            //Act
            List <char> numList = newWordCounter.Numbers;

            //Assert
            CollectionAssert.AreEquivalent(numList, comparisonList);
        }
        public void CountOccurencesForText_OnExecuteWithShortWords_SkipsWordsThatAre3CharactersOrShorter()
        {
            var wordCounter = new WordCounter();

            var text = "test test test tes tes tes tes";

            var result = wordCounter.CountOccurencesForText(text);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Any());
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(3, result.GetWordCount("test"));
        }
Ejemplo n.º 51
0
        public static void Main(string[] args)
        {
            WordCounter file1 = new WordCounter("1 1 1");
            file1.DisplayAll();

            WordCounter file2 = new WordCounter("This is a statement, and so is this.");
            file2.DisplayAll();

            WordCounter file3 = new WordCounter(string.Empty);
            file3.DisplayAll();

            WordCounter file4 = new WordCounter("21312 2452 123 123!!$$%#$^&$%");
            file4.DisplayAll();
        }
Ejemplo n.º 52
0
        static void Main(string[] args)
        {
            string sentence = String.Empty;
            if (args.Length != 0)
            {
                sentence = args[0];
                Console.WriteLine(sentence);
            }
            else
            {
                Console.WriteLine("Please enter the sentence");
                sentence = Console.ReadLine();
            }

            var result = new WordCounter(sentence).CountWords();

            Console.WriteLine();
            foreach (var key in result.Keys)
            {
                Console.WriteLine("{0} - {1}", key, result[key]);
            }
            Console.ReadKey();
        }
Ejemplo n.º 53
0
    protected void RadEditor1_SubmitClicked(object sender, EventArgs e)
    {
        string sDoc = Request.QueryString.Get("doc").ToString();

        WordCounter wc = new WordCounter(RadEditor1.Text);
        int numWords, numChars;
        wc.CountStats(out numWords, out numChars);

        if (numChars <= 15000)
        {
            switch (sDoc)
            {
                case "narrative":
                    Session[ssId] = RadEditor1.Html;
                    break;
            }

            InjectScript.Text = "<script>CloseAndRebind()</" + "script>";
        }
        else
        {
            lblError.Text = "Document length is too long. Please limit to 15000 characters.";
        }
    }
Ejemplo n.º 54
0
 public Executor(WordCounter wordCounter)
 {
     WordCounter = wordCounter;
 }
    CountTermsInEdgeOrVertex
    (
        IMetadataProvider oEdgeOrVertex,
        String sTextColumnName,
        WordCounter oWordCounter,
        WordPairCounter oWordPairCounter
    )
    {
        Debug.Assert(oEdgeOrVertex != null);
        Debug.Assert( !String.IsNullOrEmpty(sTextColumnName) );
        Debug.Assert(oWordCounter != null);
        Debug.Assert(oWordPairCounter != null);
        AssertValid();

        Object oTextAsObject;

        if ( oEdgeOrVertex.TryGetValue(sTextColumnName, typeof(String),
            out oTextAsObject ) )
        {
            String sText = (String)oTextAsObject;

            if ( !String.IsNullOrEmpty(sText) )
            {
                oWordCounter.CountTermsInDocument(sText);
                oWordPairCounter.CountTermsInDocument(sText);
            }
        }
    }
    TryCountTermsNoGroups
    (
        IGraph oGraph,
        WordMetricUserSettings oWordMetricUserSettings,
        WordCounter oWordCounter,
        WordPairCounter oWordPairCounter,
        HashSet<String> oUniqueImportedIDs,
        out GraphMetricColumn [] oGraphMetricColumns
    )
    {
        Debug.Assert(oGraph != null);
        Debug.Assert(oWordMetricUserSettings != null);
        Debug.Assert(oWordCounter != null);
        Debug.Assert(oWordPairCounter != null);
        AssertValid();

        Boolean bTextColumnIsOnEdgeWorksheet =
            oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet;

        System.Collections.IEnumerable oEdgesOrVertices =
            bTextColumnIsOnEdgeWorksheet ?
            (System.Collections.IEnumerable)oGraph.Edges :
            (System.Collections.IEnumerable)oGraph.Vertices;

        // Count the terms in each of the column's cells.

        foreach ( IMetadataProvider oEdgeOrVertex in EnumerateEdgesOrVertices(
            oEdgesOrVertices, bTextColumnIsOnEdgeWorksheet, oGraph,
            oUniqueImportedIDs) )
        {
            CountTermsInEdgeOrVertex(oEdgeOrVertex,
                oWordMetricUserSettings.TextColumnName, oWordCounter,
                oWordPairCounter);
        }

        oWordCounter.CalculateSalienceOfCountedTerms();
        oWordPairCounter.CalculateSalienceOfCountedTerms();
        oWordPairCounter.CalculateMutualInformationOfCountedTerms();

        // Transfer the words and word pairs to graph metric value lists.

        List<GraphMetricValueOrdered> oWordWordValues, oWordCountValues,
            oWordSalienceValues;

        List<GraphMetricValueOrdered> oWordPairWord1Values,
            oWordPairWord2Values, oWordPairCountValues,
            oWordPairSalienceValues, oWordPairMutualInformationValues;

        CreateGraphMetricValueLists(
            out oWordWordValues, out oWordCountValues, out oWordSalienceValues,
            
            out oWordPairWord1Values, out oWordPairWord2Values,
            out oWordPairCountValues, out oWordPairSalienceValues,
            out oWordPairMutualInformationValues
            );

        foreach (CountedWord oCountedWord in oWordCounter.CountedTerms)
        {
            AddCountedWordToValueLists(oCountedWord, oWordMetricUserSettings,
                oWordWordValues, oWordCountValues, oWordSalienceValues);
        }

        foreach (CountedWordPair oCountedWordPair in
            oWordPairCounter.CountedTerms)
        {
            AddCountedWordPairToValueLists(oCountedWordPair,
                oWordMetricUserSettings, oWordPairWord1Values,
                oWordPairWord2Values, oWordPairCountValues,
                oWordPairSalienceValues, oWordPairMutualInformationValues);
        }

        oGraphMetricColumns = CreateGraphMetricColumns(
            oWordWordValues, oWordCountValues, oWordSalienceValues, null,

            oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues,
            oWordPairSalienceValues, oWordPairMutualInformationValues, null
            );

        return (true);
    }
    TryCountVertexTermsByGroup
    (
        IGraph oGraph,
        WordMetricUserSettings oWordMetricUserSettings,
        WordCounter oWordCounter,
        WordPairCounter oWordPairCounter,
        HashSet<String> oUniqueImportedIDs,
        out GraphMetricColumn [] oGraphMetricColumns
    )
    {
        Debug.Assert(oGraph != null);
        Debug.Assert(oWordMetricUserSettings != null);
        Debug.Assert(oWordCounter != null);
        Debug.Assert(oWordPairCounter != null);
        AssertValid();

        List<GraphMetricValueOrdered> oWordWordValues, oWordCountValues,
            oWordSalienceValues;

        List<GraphMetricValueOrdered> oWordPairWord1Values,
            oWordPairWord2Values, oWordPairCountValues,
            oWordPairSalienceValues, oWordPairMutualInformationValues;

        CreateGraphMetricValueLists(
            out oWordWordValues, out oWordCountValues, out oWordSalienceValues,
            
            out oWordPairWord1Values, out oWordPairWord2Values,
            out oWordPairCountValues, out oWordPairSalienceValues,
            out oWordPairMutualInformationValues
            );

        List<GraphMetricValueOrdered> oWordGroupNameValues =
            new List<GraphMetricValueOrdered>();

        List<GraphMetricValueOrdered> oWordPairGroupNameValues =
            new List<GraphMetricValueOrdered>();

        // Get a list of the graph's groups, adding a dummy group for the
        // entire graph and another to contain any non-grouped vertices.

        foreach ( GroupInfo oGroup in
            EnumerateGroupsForCountingVertexTerms(oGraph) )
        {
            // Count the terms in this group.

            oWordCounter.Clear();
            oWordPairCounter.Clear();

            foreach ( IVertex oVertex in EnumerateEdgesOrVertices(
                oGroup.Vertices, false, oGraph, oUniqueImportedIDs) )
            {
                CountTermsInEdgeOrVertex(oVertex,
                    oWordMetricUserSettings.TextColumnName, oWordCounter,
                    oWordPairCounter);
            }

            oWordCounter.CalculateSalienceOfCountedTerms();
            oWordPairCounter.CalculateSalienceOfCountedTerms();
            oWordPairCounter.CalculateMutualInformationOfCountedTerms();

            // Transfer the words and word pairs to the graph metric value
            // lists.

            AddCountedWordsToValueLists(oWordCounter.CountedTerms,
                oWordMetricUserSettings, oGroup.Name, oWordWordValues,
                oWordCountValues, oWordSalienceValues, oWordGroupNameValues);

            AddCountedWordPairsToValueLists(oWordPairCounter.CountedTerms,
                oWordMetricUserSettings, oGroup.Name, oWordPairWord1Values,
                oWordPairWord2Values, oWordPairCountValues,
                oWordPairSalienceValues, oWordPairMutualInformationValues,
                oWordPairGroupNameValues);
        }

        oGraphMetricColumns = CreateGraphMetricColumns(
            oWordWordValues, oWordCountValues, oWordSalienceValues,
            oWordGroupNameValues,
        
            oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues,
            oWordPairSalienceValues, oWordPairMutualInformationValues,
            oWordPairGroupNameValues
            );

        return (true);
    }
    TryCountEdgeTermsByGroup
    (
        IGraph oGraph,
        WordMetricUserSettings oWordMetricUserSettings,
        WordCounter oWordCounter,
        WordPairCounter oWordPairCounter,
        HashSet<String> oUniqueImportedIDs,
        out GraphMetricColumn [] oGraphMetricColumns
    )
    {
        Debug.Assert(oGraph != null);
        Debug.Assert(oWordMetricUserSettings != null);
        Debug.Assert(oWordCounter != null);
        Debug.Assert(oWordPairCounter != null);
        AssertValid();

        List<GraphMetricValueOrdered> oWordWordValues, oWordCountValues,
            oWordSalienceValues;

        List<GraphMetricValueOrdered> oWordPairWord1Values,
            oWordPairWord2Values, oWordPairCountValues,
            oWordPairSalienceValues, oWordPairMutualInformationValues;

        CreateGraphMetricValueLists(
            out oWordWordValues, out oWordCountValues, out oWordSalienceValues,
            
            out oWordPairWord1Values, out oWordPairWord2Values,
            out oWordPairCountValues, out oWordPairSalienceValues,
            out oWordPairMutualInformationValues
            );

        List<GraphMetricValueOrdered> oWordGroupNameValues =
            new List<GraphMetricValueOrdered>();

        List<GraphMetricValueOrdered> oWordPairGroupNameValues =
            new List<GraphMetricValueOrdered>();

        // Get the edges in each of the graph's groups.  Include a "dummy"
        // group that contains the edges that aren't contained in any real
        // groups.

        foreach ( GroupEdgeInfo oGroupEdgeInfo in
            GroupEdgeSorter.SortGroupEdges(oGraph, Int32.MaxValue,
                true, true) )
        {
            // Count the terms in this group.

            oWordCounter.Clear();
            oWordPairCounter.Clear();

            foreach ( IEdge oEdge in EnumerateEdgesOrVertices(
                oGroupEdgeInfo.Edges, true, oGraph, oUniqueImportedIDs) )
            {
                CountTermsInEdgeOrVertex(oEdge,
                    oWordMetricUserSettings.TextColumnName, oWordCounter,
                    oWordPairCounter);
            }

            oWordCounter.CalculateSalienceOfCountedTerms();
            oWordPairCounter.CalculateSalienceOfCountedTerms();
            oWordPairCounter.CalculateMutualInformationOfCountedTerms();

            // Transfer the words and word pairs to the graph metric value
            // lists.

            String sGroupName = oGroupEdgeInfo.GroupName;

            AddCountedWordsToValueLists( oWordCounter.CountedTerms,
                oWordMetricUserSettings, sGroupName, oWordWordValues,
                oWordCountValues, oWordSalienceValues, oWordGroupNameValues);

            AddCountedWordPairsToValueLists( oWordPairCounter.CountedTerms,
                oWordMetricUserSettings, sGroupName, oWordPairWord1Values,
                oWordPairWord2Values, oWordPairCountValues,
                oWordPairSalienceValues, oWordPairMutualInformationValues,
                oWordPairGroupNameValues);

            if (
                sGroupName == GroupEdgeSorter.DummyGroupNameForEntireGraph
                &&
                oUniqueImportedIDs != null
                )
            {
                // This is the dummy group that stores all the edges in the
                // graph.  Note that SortGroupEdges() guarantees that this is
                // the first group, so the imported IDs need to be cleared only
                // once within this loop.

                oUniqueImportedIDs.Clear();
            }
        }

        oGraphMetricColumns = CreateGraphMetricColumns(
            oWordWordValues, oWordCountValues, oWordSalienceValues,
            oWordGroupNameValues,
        
            oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues,
            oWordPairSalienceValues, oWordPairMutualInformationValues,
            oWordPairGroupNameValues
            );

        return (true);
    }
    TryCalculateGraphMetrics
    (
        IGraph graph,
        CalculateGraphMetricsContext calculateGraphMetricsContext,
        out GraphMetricColumn [] graphMetricColumns
    )
    {
        Debug.Assert(graph != null);
        Debug.Assert(calculateGraphMetricsContext != null);
        AssertValid();

        graphMetricColumns = new GraphMetricColumn[0];

        WordMetricUserSettings oWordMetricUserSettings =
            calculateGraphMetricsContext.GraphMetricUserSettings
            .WordMetricUserSettings;

        if (
            !calculateGraphMetricsContext.ShouldCalculateGraphMetrics(
                GraphMetrics.Words)
            ||
            String.IsNullOrEmpty(oWordMetricUserSettings.TextColumnName)
            )
        {
            return (true);
        }

        String [] asWordsToSkip = StringUtil.SplitOnCommonDelimiters(
            oWordMetricUserSettings.WordsToSkip);

        WordCounter oWordCounter = new WordCounter(asWordsToSkip);
        WordPairCounter oWordPairCounter = new WordPairCounter(asWordsToSkip);

        // The edges or vertices may have unique imported IDs.  If so, this
        // becomes a collection of the IDs.

        HashSet<String> oUniqueImportedIDs = 
            EdgesOrVerticesHaveImportedIDs(graph,
                oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet) ?
            new HashSet<String>() : null;

        if (oWordMetricUserSettings.CountByGroup)
        {
            if (oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet)
            {
                return ( TryCountEdgeTermsByGroup(graph,
                    oWordMetricUserSettings, oWordCounter, oWordPairCounter,
                    oUniqueImportedIDs, out graphMetricColumns) );
            }
            else
            {
                return ( TryCountVertexTermsByGroup(graph,
                    oWordMetricUserSettings, oWordCounter, oWordPairCounter,
                    oUniqueImportedIDs, out graphMetricColumns) );
            }
        }
        else
        {
            return ( TryCountTermsNoGroups(graph,
                oWordMetricUserSettings, oWordCounter, oWordPairCounter,
                oUniqueImportedIDs, out graphMetricColumns) );
        }
    }
Ejemplo n.º 60
0
    TestCountTermsInDocument13()
    {
        // Don't convert to lower case.

        WordCounter oWordCounterForThisTest =
            new WordCounter(false, WordsToSkip);

        oWordCounterForThisTest.CountTermsInDocument("the brown jumping fox");
        oWordCounterForThisTest.CountTermsInDocument("FOX JUMPING BROWN THE");

        Assert.AreEqual(2, oWordCounterForThisTest.TotalDocuments);
        Assert.AreEqual(7, oWordCounterForThisTest.TotalWordsInDocuments);

        IEnumerable<CountedWord> oCountedWords =
            oWordCounterForThisTest.CountedTerms;

        Assert.AreEqual( 7, oCountedWords.Count() );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "brown"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "jumping"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "fox"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "BROWN"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "JUMPING"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "FOX"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );

        oCountedWords.Single(
            oCountedWord => (
                oCountedWord.Word == "THE"
                &&
                oCountedWord.Count == 1
                &&
                oCountedWord.DocumentsInWhichTermWasCounted == 1
            ) );
    }