Esempio n. 1
0
        public void VerbTableWithPhrasals()
        {
            var table = RantDictionaryTable.FromStream("verbs.table", File.Open("Tables/verbs.table", FileMode.Open));

            Assert.AreEqual("verb", table.Name);
            Assert.AreEqual(8, table.TermsPerEntry);
        }
Esempio n. 2
0
        private static void Pack(RantPackage package, string contentPath)
        {
            foreach (var path in Directory.EnumerateFiles(contentPath, "*.*", SearchOption.AllDirectories)
                     .Where(p =>
                            p.EndsWith(".rant", StringComparison.OrdinalIgnoreCase) ||
                            p.EndsWith(".rants", StringComparison.OrdinalIgnoreCase) ||
                            p.EndsWith(".rantpgm", StringComparison.OrdinalIgnoreCase)))
            {
                RantProgram pattern;
                switch (Path.GetExtension(path).ToLower())
                {
                case ".rantpgm":
                    pattern = RantProgram.LoadFile(path);
                    break;

                default:
                    pattern = RantProgram.CompileFile(path);
                    break;
                }
                string relativePath;
                TryGetRelativePath(contentPath, path, out relativePath, true);
                pattern.Name = relativePath;
                package.AddResource(pattern);
                Console.WriteLine("+ " + pattern.Name);
            }

            foreach (string path in Directory.GetFiles(contentPath, "*.table", SearchOption.AllDirectories))
            {
                Console.WriteLine("+ " + path);
                var table = RantDictionaryTable.FromStream(Path.GetFileNameWithoutExtension(path), File.Open(path, FileMode.Open));
                package.AddResource(table);
            }
        }
Esempio n. 3
0
        public void Emoji()
        {
            var table = RantDictionaryTable.FromStream("emoji.table", File.Open("Tables/emoji.table", FileMode.Open));

            Assert.AreEqual("emoji", table.Name);
            Assert.AreEqual(1, table.TermsPerEntry);
        }
Esempio n. 4
0
        public static List <string> GetEntries(RantDictionaryTable table)
        {
            List <string> entries = new List <string>();

            foreach (RantDictionaryEntry entry in table.GetEntries())
            {
                entries.Add(entry.ToString());
            }
            return(entries);
        }
Esempio n. 5
0
        public static List <string> GetClasses(RantDictionaryTable table)
        {
            List <string> classes = new List <string>();

            foreach (string className in table.GetClasses())
            {
                classes.Add(className);
            }
            return(classes);
        }
Esempio n. 6
0
        public void SimpleNounTable()
        {
            var table = RantDictionaryTable.FromStream("nouns.table", File.Open("Tables/nouns.table", FileMode.Open));

            Assert.AreEqual("noun", table.Name);
            Assert.AreEqual(2, table.TermsPerEntry);
            Assert.AreEqual(0, table.GetSubtypeIndex("singular"));
            Assert.AreEqual(0, table.GetSubtypeIndex("singular"));
            Assert.AreEqual(1, table.GetSubtypeIndex("plural"));
            Assert.AreEqual(1, table.GetSubtypeIndex("p"));
            Assert.AreEqual(-1, table.GetSubtypeIndex("nonexistent"));
        }
Esempio n. 7
0
        public override bool Test(RantDictionary dictionary, RantDictionaryTable table, RantDictionaryEntry entry, int termIndex, Query query)
        {
            bool match = query.Exclusive
                ? _items.Any() == entry.GetClasses().Any() &&
                         entry.GetClasses().All(c => _items.Any(item => item.Any(rule => rule.ShouldMatch && rule.Class == c)))
                : !_items.Any() || _items.All(set => set.Any(rule => entry.ContainsClass(rule.Class) == rule.ShouldMatch));

            // Enumerate hidden classes that aren't manually exposed or explicitly allowed by the filter
            var hidden = table.HiddenClasses.Where(c => !AllowsClass(c)).Except(dictionary.IncludedHiddenClasses);

            return(match && !hidden.Any(entry.ContainsClass));
        }
Esempio n. 8
0
        public static RantDictionaryTable FindTable(RantDictionary dictionary, string searchTerm)
        {
            RantDictionaryTable table = null;

            foreach (RantDictionaryTable _table in dictionary.GetTables())
            {
                if (_table.Name == searchTerm)
                {
                    table = _table;
                }
            }
            return(table);
        }
Esempio n. 9
0
        public static RantEngine StartEngine()
        {
            RantDictionary dictionary = new RantDictionary();

            foreach (string fileName in fileNames)
            {
                string     filePath = folderPath + fileName + ".table";
                FileStream stream   = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                RantDictionaryTable table = RantDictionaryTable.FromStream(filePath, stream);
                dictionary.AddTable(table);
                stream.Close();
            }

            return(new RantEngine(dictionary));
        }
Esempio n. 10
0
        protected override void OnRun()
        {
            var cmdPaths   = CmdLine.GetPaths();
            var workingDir = cmdPaths.Length > 0 ? cmdPaths[0] : Environment.CurrentDirectory;
            var outputPath = CmdLine.Property("out", Path.Combine(workingDir, "dictionary.md"));
            var tables     = Directory.GetFiles(workingDir, "*.table", SearchOption.AllDirectories)
                             .Select(dir => RantDictionaryTable.FromStream(dir, File.Open(dir, FileMode.Open)))
                             .OrderBy(table => table.Name)
                             .ToList();

            if (tables.Count == 0)
            {
                Console.WriteLine("No tables found.");
                return;
            }

            using (var writer = new StreamWriter(outputPath))
            {
                foreach (var table in tables)
                {
                    writer.WriteLine($"## {table.Name}");
                    writer.WriteLine($"**Entries:** {table.EntryCount}\n");
                    if (table.HiddenClasses.Any())
                    {
                        writer.WriteLine($"**Hidden classes:** {table.HiddenClasses.Select(cl => $"`{cl}`").Aggregate((c, n) => $"{c}, {n}")}\n");
                    }

                    // Write subtype list
                    writer.WriteLine($"### Subtypes\n");
                    for (int i = 0; i < table.TermsPerEntry; i++)
                    {
                        writer.WriteLine($"{i + 1}. {table.GetSubtypesForIndex(i).Select(st => $"`{st}`").Aggregate((c, n) => $"{c}, {n}")}");
                    }
                    writer.WriteLine();

                    // Write classes
                    writer.WriteLine($"### Classes\n");
                    foreach (var cl in table.GetClasses().OrderBy(cl => cl))
                    {
                        writer.WriteLine($"- `{cl}` ({table.GetEntries().Count(e => e.ContainsClass(cl))})");
                    }

                    writer.WriteLine();
                }
            }
        }
Esempio n. 11
0
        private static void Pack(RantPackage package, string contentPath)
        {
            foreach (string path in Directory.EnumerateFiles(contentPath, "*.*", SearchOption.AllDirectories)
                     .Where(p => p.EndsWith(".rant") || p.EndsWith(".rants")))
            {
                var    pattern = RantProgram.CompileFile(path);
                string relativePath;
                TryGetRelativePath(contentPath, path, out relativePath, true);
                pattern.Name = relativePath;
                package.AddResource(pattern);
                Console.WriteLine("+ " + pattern.Name);
            }

            foreach (string path in Directory.GetFiles(contentPath, "*.table", SearchOption.AllDirectories))
            {
                Console.WriteLine("+ " + path);
                var table = RantDictionaryTable.FromStream(Path.GetFileNameWithoutExtension(path), File.Open(path, FileMode.Open));
                package.AddResource(table);
            }
        }
Esempio n. 12
0
        private static void Pack(RantPackage package, string contentPath)
        {
            foreach (var path in Directory.EnumerateFiles(contentPath, "*.*", SearchOption.AllDirectories)
                     .Where(p => p.EndsWith(".rant") || p.EndsWith(".rants")))
            {
                var    pattern = RantPattern.FromFile(path);
                string relativePath;
                TryGetRelativePath(contentPath, path, out relativePath, true);
                pattern.Name = relativePath;
                package.AddPattern(pattern);
                Console.WriteLine("+ " + pattern.Name);
            }

            foreach (var path in Directory.GetFiles(contentPath, "*.dic", SearchOption.AllDirectories))
            {
                Console.WriteLine("+ " + path);
                var table = RantDictionaryTable.FromFile(path);
                package.AddTable(table);
            }
        }
Esempio n. 13
0
        static void GenerateTable(string tablePath, RantDictionaryTable table, string rootDir, string entriesDir)
        {
            var tableClasses = new HashSet <string>();

            foreach (var entry in table.GetEntries())
            {
                foreach (var entryClass in entry.GetClasses())
                {
                    tableClasses.Add(entryClass);
                }
            }

            File.WriteAllText(rootDir + "/" + "table-" + table.Name + ".html", PageGenerator.GenerateTablePage(table, tablePath));

            foreach (var tableClass in tableClasses)
            {
                File.WriteAllText(entriesDir + "/" + table.Name + "-" + tableClass + ".html", PageGenerator.GenerateTableClassPage(table, tableClass));
            }

            File.WriteAllText(entriesDir + "/" + table.Name + ".html", PageGenerator.GenerateTableClassPage(table, ""));
        }
Esempio n. 14
0
        public void SaveLoadRun()
        {
            var package = new RantPackage
            {
                ID          = "TestPackage",
                Description = "This is a test.",
                Title       = "Test Package?!",
                Version     = new RantPackageVersion(1, 1, 0)
            };

            package.AddResource(RantDictionaryTable.FromStream("nouns", File.Open("Tables/nouns.table", FileMode.Open)));
            package.AddResource(RantProgram.CompileString("TestProgram", @"[case:title]<noun-food-fruit-yellow> [rs:5;,\s]{[rn]}"));
            package.Save("TestPackage.rantpkg");

            package = RantPackage.Load("TestPackage.rantpkg");
            rant.LoadPackage(package);

            Assert.AreEqual("Banana 1, 2, 3, 4, 5", rant.DoName("TestProgram").Main);
            Assert.AreEqual("TestPackage", package.ID);
            Assert.AreEqual("This is a test.", package.Description);
            Assert.AreEqual("Test Package?!", package.Title);
            Assert.AreEqual("1.1.0", package.Version.ToString());
        }
Esempio n. 15
0
 public Queries()
 {
     rant.Dictionary.AddTable(RantDictionaryTable.FromStream("verbs", File.Open("Tables/verbs.table", FileMode.Open)));
     rant.Dictionary.AddTable(RantDictionaryTable.FromStream("nouns", File.Open("Tables/nouns.table", FileMode.Open)));
     rant.Dictionary.AddTable(RantDictionaryTable.FromStream("nsfw", File.Open("Tables/nsfw.table", FileMode.Open)));
 }
Esempio n. 16
0
 /// <summary>
 /// Determines if the specified dictionary entry passes the filter.
 /// </summary>
 /// <param name="dictionary">The dictionary being queried.</param>
 /// <param name="table">The table being queried.</param>
 /// <param name="entry">The entry to test.</param>
 /// <param name="termIndex">The index of the term requested by the query.</param>
 /// <param name="query">The originating query.</param>
 /// <returns></returns>
 public abstract bool Test(RantDictionary dictionary, RantDictionaryTable table, RantDictionaryEntry entry, int termIndex, Query query);
Esempio n. 17
0
        static void Main(string[] args)
        {
            //Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            Title = "Rant Console" + (Flag("nsfw") ? " [NSFW]" : "");

            var rant = new RantEngine();

#if !DEBUG
            try
            {
#endif
            if (!String.IsNullOrEmpty(DIC_PATH))
            {
                rant.Dictionary = RantDictionary.FromDirectory(DIC_PATH);
            }
            else
            {
                foreach (var dic in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dic", SearchOption.AllDirectories))
                {
                    rant.Dictionary.AddTable(RantDictionaryTable.FromFile(dic));
                }
            }

            if (!String.IsNullOrEmpty(PKG_PATH))
            {
#if DEBUG
                Stopwatch timer = Stopwatch.StartNew();
#endif
                rant.LoadPackage(PKG_PATH);
#if DEBUG
                timer.Stop();
                WriteLine($"Package loading: {timer.ElapsedMilliseconds}ms");
#endif
            }
            else
            {
                foreach (var pkg in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.rantpkg", SearchOption.AllDirectories))
                {
                    rant.LoadPackage(pkg);
                }
            }
#if !DEBUG
        }

        catch (Exception e)
        {
            ForegroundColor = ConsoleColor.Cyan;
            WriteLine($"Dictionary load error: {e.Message}");
        }
#endif
            if (Flag("nsfw"))
            {
                rant.Dictionary.IncludeHiddenClass("nsfw");
            }

            if (!String.IsNullOrEmpty(FILE))
            {
#if !DEBUG
                try
                {
#endif
                PrintOutput(rant, File.ReadAllText(FILE));
#if !DEBUG
            }
            catch (Exception ex)
            {
                ForegroundColor = ConsoleColor.Red;
                WriteLine(ex.Message);
                ResetColor();
            }
#endif

                if (Flag("wait"))
                {
                    ReadKey(true);
                }
                return;
            }

            while (true)
            {
                ForegroundColor = Flag("nsfw") ? ConsoleColor.DarkRed : ConsoleColor.Gray;
                Write("RANT> "); // real number symbol
                ForegroundColor = ConsoleColor.White;
                PrintOutput(rant, ReadLine());
            }
        }
Esempio n. 18
0
 public override bool Test(RantDictionary dictionary, RantDictionaryTable table, RantDictionaryEntry entry, int termIndex, Query query) => TestAgainst(entry[termIndex].SyllableCount);
Esempio n. 19
0
 public override bool Test(RantDictionary dictionary, RantDictionaryTable table, RantDictionaryEntry entry, int termIndex, Query query) => Regex.IsMatch(entry[termIndex].Value) == Outcome;
Esempio n. 20
0
        private static void ProcessDicFile(string path)
        {
            var table = RantDictionaryTable.FromFile(path);

            table.Save(path, Flag("diff"));
        }
Esempio n. 21
0
        public static string GenerateTableClassPage(RantDictionaryTable table, string tableClass)
        {
            bool all = String.IsNullOrEmpty(tableClass);

            var entries = all ? table.GetEntries() : table.GetEntries().Where(e => e.ContainsClass(tableClass));

            var text = new StringWriter();

            using (var writer = new HtmlTextWriter(text))
            {
                writer.WriteLine("<!DOCTYPE html>");

                writer.RenderBeginTag(HtmlTextWriterTag.Html);

                // Header
                writer.RenderBeginTag(HtmlTextWriterTag.Head);

                // Title
                writer.RenderBeginTag(HtmlTextWriterTag.Title);
                writer.WriteEncodedText((all ? table.Name : table.Name + ": " + tableClass) + " entries");
                writer.RenderEndTag();

                // Stylesheet
                writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "../dicdoc.css");
                writer.RenderBeginTag(HtmlTextWriterTag.Link);
                writer.RenderEndTag();

                writer.RenderEndTag(); // </head>

                // Body
                writer.RenderBeginTag(HtmlTextWriterTag.Body);

                // Heading
                writer.RenderBeginTag(HtmlTextWriterTag.H1);
                writer.WriteEncodedText("<" + table.Name + (all ? "" : "-" + tableClass) + ">");
                writer.RenderEndTag();

                // Entry list
                foreach (var e in entries)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "entry");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    // Terms
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "termset");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    for (int i = 0; i < e.Terms.Length; i++)
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "term");
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);
                        writer.WriteEncodedText(e.Terms[i].Value);

                        if (e.Terms[i].PronunciationParts.Length > 0)
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "terminfo");
                            writer.RenderBeginTag(HtmlTextWriterTag.Span);
                            writer.RenderBeginTag(HtmlTextWriterTag.I);
                            writer.WriteEncodedText(" [" + e.Terms[i].Pronunciation + "]");
                            writer.RenderEndTag(); // </i>
                            writer.RenderEndTag(); // </span>
                        }

                        // Subtype
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "subtype");
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);
                        writer.WriteEncodedText(i < table.Subtypes.Length ? table.Subtypes[i] : "???");
                        writer.RenderEndTag();

                        writer.RenderEndTag();
                    }

                    writer.RenderEndTag();

                    // Notes
                    var notes        = new List <string>();
                    var otherClasses = e.GetClasses().Where(cl => cl != tableClass);

                    if (e.Terms.All(t => t.PronunciationParts.Length > 0))
                    {
                        notes.Add("Full pronunciation");
                    }
                    else if (e.Terms.Any(t => t.PronunciationParts.Length > 0))
                    {
                        notes.Add("Partial pronunciation");
                    }

                    if (e.Weight != 1)
                    {
                        notes.Add("Weight: " + e.Weight);
                    }

                    if (all && e.GetClasses().Any())
                    {
                        notes.Add("Classes: " + String.Join(", ", e.GetClasses()));
                    }
                    else
                    {
                        if (otherClasses.Any())
                        {
                            notes.Add("Other classes: " + String.Join(", ", otherClasses));
                        }
                    }


                    if (e.NSFW)
                    {
                        notes.Add("NSFW");
                    }

                    GenerateUL(writer, notes);

                    writer.RenderEndTag();
                }

                writer.RenderEndTag(); // </body>

                writer.RenderEndTag(); // </html>
            }

            return(text.ToString());
        }
Esempio n. 22
0
        public static string GenerateTablePage(RantDictionaryTable table, string filename)
        {
            int entryCount = table.GetEntries().Count();

            // Get all the classes
            var tableClasses = new HashSet <string>();

            foreach (var entry in table.GetEntries())
            {
                foreach (var entryClass in entry.GetClasses())
                {
                    tableClasses.Add(entryClass);
                }
            }

            var text = new StringWriter();

            using (var writer = new HtmlTextWriter(text))
            {
                writer.WriteLine("<!DOCTYPE html>");

                writer.RenderBeginTag(HtmlTextWriterTag.Html);

                // Header
                writer.RenderBeginTag(HtmlTextWriterTag.Head);

                // Title
                writer.RenderBeginTag(HtmlTextWriterTag.Title);
                writer.WriteEncodedText(table.Name);
                writer.RenderEndTag();

                // Stylesheet
                writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "dicdoc.css");
                writer.RenderBeginTag(HtmlTextWriterTag.Link);
                writer.RenderEndTag();

                writer.RenderEndTag(); // </head>

                writer.RenderBeginTag(HtmlTextWriterTag.Body);

                // Header
                writer.RenderBeginTag(HtmlTextWriterTag.H1);
                writer.WriteEncodedText("<" + table.Name + ">");
                writer.RenderEndTag();

                // Description
                writer.RenderBeginTag(HtmlTextWriterTag.P);
                writer.WriteEncodedText("The ");
                writer.RenderBeginTag(HtmlTextWriterTag.B);
                writer.WriteEncodedText(table.Name);
                writer.RenderEndTag(); // </b>
                writer.WriteEncodedText(" table ("
                                        + Path.GetFileName(filename)
                                        + ") contains "
                                        + entryCount + (entryCount == 1 ? " entry" : " entries ")
                                        + " and " + tableClasses.Count + (tableClasses.Count == 1 ? " class" : " classes")
                                        + ".");
                writer.RenderEndTag(); // </p>

                // Subtypes
                if (table.Subtypes.Length == 1)
                {
                    writer.WriteEncodedText("It has one subtype: ");
                    writer.RenderBeginTag(HtmlTextWriterTag.B);
                    writer.WriteEncodedText(table.Subtypes[0]);
                    writer.RenderEndTag();
                    writer.WriteEncodedText(".");
                }
                else
                {
                    writer.WriteEncodedText("It has " + table.Subtypes.Length + (table.Subtypes.Length == 1 ? " subtype" : " subtypes")
                                            + ": ");
                    for (int i = 0; i < table.Subtypes.Length; i++)
                    {
                        if (i == table.Subtypes.Length - 1)
                        {
                            writer.WriteEncodedText(table.Subtypes.Length == 2 ? " and " :  "and ");
                        }

                        writer.RenderBeginTag(HtmlTextWriterTag.B);
                        writer.WriteEncodedText(table.Subtypes[i]);
                        writer.RenderEndTag();

                        if (i < table.Subtypes.Length - 1 && table.Subtypes.Length > 2)
                        {
                            writer.WriteEncodedText(", ");
                        }
                    }
                    writer.WriteEncodedText(".");
                }

                // Separator
                writer.RenderBeginTag(HtmlTextWriterTag.Hr);
                writer.RenderEndTag();

                // "View All" link
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "entries/" + table.Name + ".html");
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                writer.WriteEncodedText("Browse All Entries");

                writer.RenderEndTag(); // </a>

                // Class list
                writer.RenderBeginTag(HtmlTextWriterTag.H2);
                writer.WriteEncodedText("Classes");
                writer.RenderEndTag(); // </h2>

                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                foreach (var tableClass in tableClasses)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);

                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "entries/" + table.Name + "-" + tableClass + ".html");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.WriteEncodedText(tableClass);
                    writer.RenderEndTag(); // </a>

                    writer.RenderEndTag(); // </li>
                }

                writer.RenderEndTag(); // </ul>

                writer.RenderEndTag(); // </body>

                writer.RenderEndTag(); // </html>
            }

            return(text.ToString());
        }
Esempio n. 23
0
        /// <summary>
        /// Loads a package from the specified stream and returns it as a RantPackage object.
        /// </summary>
        /// <param name="source">The stream to load the package data from.</param>
        /// <returns></returns>
        public static RantPackage Load(Stream source)
        {
            using (var reader = new EasyReader(source))
            {
                var magic = Encoding.ASCII.GetString(reader.ReadBytes(4));
                if (magic == OLD_MAGIC)
                {
                    return(LoadOldPackage(reader));
                }
                if (magic != MAGIC)
                {
                    throw new InvalidDataException("File is corrupt.");
                }
                var package = new RantPackage();
                var version = reader.ReadUInt32();
                if (version != PACKAGE_VERSION)
                {
                    throw new InvalidDataException("Invalid package version: " + version);
                }
                var compress = reader.ReadBoolean();
                var size     = reader.ReadInt32();
                var data     = reader.ReadBytes(size);
                if (compress)
                {
                    data = EasyCompressor.Decompress(data);
                }
                var doc = BsonDocument.Read(data);

                var info = doc["info"];
                if (info == null)
                {
                    throw new InvalidDataException("Metadata is missing from package.");
                }

                package.Title       = info["title"];
                package.ID          = info["id"];
                package.Version     = RantPackageVersion.Parse(info["version"]);
                package.Description = info["description"];
                package.Authors     = (string[])info["authors"];
                package.Tags        = (string[])info["tags"];
                var deps = info["dependencies"];
                if (deps != null && deps.IsArray)
                {
                    for (int i = 0; i < deps.Count; i++)
                    {
                        var  dep           = deps[i];
                        var  depId         = dep["id"].Value;
                        var  depVersion    = dep["version"].Value;
                        bool depAllowNewer = (bool)dep["allow-newer"].Value;
                        package.AddDependency(new RantPackageDependency(depId.ToString(), depVersion.ToString())
                        {
                            AllowNewer = depAllowNewer
                        });
                    }
                }

                var patterns = doc["patterns"];
                if (patterns != null)
                {
                    var names = patterns.Keys;
                    foreach (string name in names)
                    {
                        package.AddPattern(new RantPattern(name, RantPatternOrigin.File, patterns[name]));
                    }
                }

                var tables = doc["tables"];
                if (tables != null)
                {
                    var names = tables.Keys;
                    foreach (string name in names)
                    {
                        var      table         = tables[name];
                        string   tableName     = table["name"];
                        string[] tableSubs     = (string[])table["subs"];
                        string[] hiddenClasses = (string[])table["hidden"];

                        var entries   = new List <RantDictionaryEntry>();
                        var entryList = table["entries"];
                        for (var i = 0; i < entryList.Count; i++)
                        {
                            var loadedEntry = entryList[i];
                            int weight      = 1;
                            if (loadedEntry.HasKey("weight"))
                            {
                                weight = (int)loadedEntry["weight"].Value;
                            }
                            string[] requiredClasses = (string[])loadedEntry["classes"];
                            string[] optionalClasses = (string[])loadedEntry["optional_classes"];
                            var      terms           = new List <RantDictionaryTerm>();
                            var      termList        = loadedEntry["terms"];
                            for (var j = 0; j < termList.Count; j++)
                            {
                                var t = new RantDictionaryTerm(termList[j]["value"], termList[j]["pron"]);
                                terms.Add(t);
                            }
                            var entry = new RantDictionaryEntry(
                                terms.ToArray(),
                                requiredClasses.Concat(optionalClasses.Select(x => x + "?")),
                                weight
                                );
                            entries.Add(entry);
                        }
                        var rantTable = new RantDictionaryTable(
                            tableName,
                            tableSubs,
                            entries,
                            hiddenClasses
                            );
                        package.AddTable(rantTable);
                    }
                }

                return(package);
            }
        }
Esempio n. 24
0
 /// <summary>
 /// Adds the specified table to the package.
 /// </summary>
 /// <param name="table">The table to add.</param>
 public void AddTable(RantDictionaryTable table)
 => (_tables ?? (_tables = new HashSet <RantDictionaryTable>())).Add(table);