Esempio n. 1
0
        public static List <TextSummary> SummarizeBooks(List <TextInfo> books, int decimals = 3)
        {
            var res = new List <TextSummary>();

            foreach (var book in books)
            {
                var preProcessedText =
                    Stylo.PreProcessText(Path.Combine(@"..\..\Tekster\", book.Filename), Encoding.UTF8, true);
                var s = new Stylo.TextSummary(book, preProcessedText, decimals);
                res.Add(s);
            }
            return(res);
        }
Esempio n. 2
0
        public static List <ShortSummary> SummarizeWordClusters(TextInfo info, int ClusterSize, int decimals = 3)
        {
            var summary          = new List <ShortSummary>();
            var preProcessedText =
                Stylo.PreProcessText(Path.Combine(@"..\..\Tekster\", info.Filename), Encoding.UTF8, true);
            var clusters = Stylo.getWordClusters(preProcessedText, ClusterSize, false);
            int counter  = 1;

            foreach (var cluster in clusters)
            {
                var s = new Stylo.ShortSummary(info, cluster, decimals);
                s.Cluster = counter;
                counter++;
                summary.Add(s);
            }
            return(summary);
        }
Esempio n. 3
0
        private static void writeTestDataset(List <TextInfo> works, string headers, Options options,
                                             bool writeToConsole = true)
        {
            if (writeToConsole)
            {
                ConsoleHelper.Write(ConsoleColor.DarkGray, $"Klargør import af {works.Count} tekster til TEST-sæt...");
            }
            using (var writer = new StreamWriter(ModelBuilder.TestCSVFile(options), false, Encoding.UTF8))
            {
                writer.WriteLine(headers);
                foreach (TextInfo work in works)
                {
                    if (writeToConsole)
                    {
                        ConsoleHelper.Write(ConsoleColor.DarkGray, $"---> {work.Title}");
                    }
                    List <string> list             = new List <string>();
                    var           preProcessedText = Stylo.PreProcessText(Path.Combine(@"..\..\Tekster\", work.Filename), Encoding.UTF8,
                                                                          true);
                    work.TextSummary = new Stylo.TextSummary(work, preProcessedText);

                    switch (options.ImportFormat)
                    {
                    case TextImportFormat.SENTENCES:
                        var    sentences = Stylo.GetSentences(preProcessedText, options.removeStopWords);
                        int    counter   = 0;
                        string ss        = "";
                        for (int i = 0; i < sentences.Count; i++)
                        {
                            string sentence = sentences[i];
                            if (counter < options.testClusterSize)
                            {
                                ss = ss + sentence + " ";
                                counter++;
                            }
                            else
                            {
                                list.Add(ss);
                                ss      = sentence;
                                counter = 0;
                            }
                        }

                        list.Add(ss);
                        break;

                    case TextImportFormat.WORDS:
                        list = Stylo.getWordClusters(preProcessedText, options.testClusterSize, options.removeStopWords);
                        break;
                        //case TextImportFormat.CHARS:
                        //    list = Stylo.getCharacterClusters(preProcessedText, options.testClusterSize);
                        //    break;
                        //default:
                        //    list = Stylo.getCharacterClusters(preProcessedText, options.testClusterSize);
                        //    break;
                    }

                    //                    List<string> list = Cluster Sentences.GetSentences(s.Text);
                    List <ImportDataModel> data = new List <ImportDataModel>();
                    foreach (string value in list)
                    {
                        ImportDataModel cluster = new ImportDataModel(work, value);
                        //cluster.DifferentWords = cluster.DifferentWords / options.testClusterSize;
                        //cluster.LongWords = cluster.LongWords / options.testClusterSize;
                        //cluster.HapaxLegomena = cluster.HapaxLegomena / options.testClusterSize;
                        //cluster.StopWords = cluster.StopWords / options.testClusterSize;
                        //cluster.TotalWords = cluster.TotalWords / options.testClusterSize;

                        data.Add(cluster);
                    }

                    WriteCSV(data, work, writer);
                }
            }

            if (writeToConsole)
            {
                Console.WriteLine("");
                ConsoleHelper.Write(ConsoleColor.DarkGray,
                                    $"CSV-fil for TEST gemt som {ModelBuilder.TestCSVFile(options)}");
                Console.WriteLine("");
            }
        }
Esempio n. 4
0
        public static void writeDataset(List <TextInfo> works, string headers, Options options)
        {
            using (var trainWriter = new StreamWriter(ModelBuilder.TrainCSVFile(options), false, Encoding.UTF8))
            {
                trainWriter.WriteLine(headers);
                using (var testWriter = new StreamWriter(ModelBuilder.TestCSVFile(options), false, Encoding.UTF8))
                {
                    testWriter.WriteLine(headers);
                    foreach (TextInfo work in works)
                    {
                        List <string> trainList = new List <string>();
                        List <string> testList  = new List <string>();
                        ConsoleHelper.Write(ConsoleColor.DarkGray, $"---> {work.Title}");
                        var preProcessedText = Stylo.PreProcessText(Path.Combine(@"..\..\Tekster\", work.Filename), Encoding.UTF8,
                                                                    true);
                        work.TextSummary = new Stylo.TextSummary(work, preProcessedText);
                        int splitPart = 0;
                        if (options.importSplitPct > 0)
                        {
                            splitPart = (int)Math.Round(100 / options.importSplitPct, MidpointRounding.AwayFromZero) - 2;
                        }

                        switch (options.ImportFormat)
                        {
                        case TextImportFormat.SENTENCES:
                            var    sentences    = Stylo.GetSentences(preProcessedText, options.removeStopWords);
                            bool   fillTrainSet = true;
                            int    counter      = 1;
                            int    stepcounter  = 0;
                            string ss           = "";
                            for (int i = 0; i < sentences.Count; i++)
                            {
                                string sentence = sentences[i];
                                if (fillTrainSet)
                                {
                                    if (counter < options.trainClusterSize)
                                    {
                                        ss = ss + sentence + " ";
                                        counter++;
                                    }
                                    else
                                    {
                                        trainList.Add(ss);
                                        stepcounter++;
                                        ss      = sentence;
                                        counter = 1;
                                        if (stepcounter == splitPart)
                                        {
                                            fillTrainSet = false;
                                        }
                                    }
                                }
                                else
                                {
                                    if (counter < options.testClusterSize)
                                    {
                                        ss = ss + sentence + " ";
                                        counter++;
                                    }
                                    else
                                    {
                                        testList.Add(ss);
                                        ss           = sentence;
                                        counter      = 1;
                                        fillTrainSet = true;
                                        stepcounter  = 0;
                                    }
                                }
                            }
                            break;

                        case TextImportFormat.WORDS:
                            var words = Stylo.GetWords(preProcessedText, options.removeStopWords);
                            fillTrainSet = true;
                            counter      = 1;
                            stepcounter  = 0;
                            ss           = "";
                            for (int i = 0; i < words.Count; i++)
                            {
                                ss += words[i] + " ";
                                if (fillTrainSet)
                                {
                                    if (counter < options.trainClusterSize)
                                    {
                                        counter++;
                                    }
                                    else
                                    {
                                        trainList.Add(ss);
                                        ss = "";
                                        stepcounter++;
                                        counter = 1;
                                        if (stepcounter == splitPart)
                                        {
                                            fillTrainSet = false;
                                        }
                                    }
                                }
                                else
                                {
                                    if (counter < options.testClusterSize)
                                    {
                                        counter++;
                                    }
                                    else
                                    {
                                        testList.Add(ss);
                                        counter      = 1;
                                        fillTrainSet = true;
                                        stepcounter  = 0;
                                        ss           = "";
                                    }
                                }
                            }
                            break;
                        }


                        List <ImportDataModel> data = new List <ImportDataModel>();
                        foreach (string value in trainList)
                        {
                            ImportDataModel cluster = new ImportDataModel(work, value);
                            data.Add(cluster);
                        }
                        WriteCSV(data, work, trainWriter);

                        data = new List <ImportDataModel>();
                        foreach (string value in testList)
                        {
                            ImportDataModel cluster = new ImportDataModel(work, value);
                            data.Add(cluster);
                        }
                        WriteCSV(data, work, testWriter);
                    }
                }
            }
        }