Пример #1
0
 /// <summary>
 /// Creates a new RantEngine object that loads vocabulary from the specified path.
 /// </summary>
 /// <param name="dictionaryPath">The path to the dictionary files to load.</param>
 public RantEngine(string dictionaryPath)
 {
     if (!String.IsNullOrEmpty(dictionaryPath))
     {
         _dictionary = RantDictionary.FromDirectory(dictionaryPath);
     }
 }
Пример #2
0
        /// <summary>
        /// Adds the tables from the specified dictionary to the package.
        /// </summary>
        /// <param name="dictionary">The dictionary to add.</param>
        public void AddDictionary(RantDictionary dictionary)
        {
            if (_tables == null)
            {
                _tables = new HashSet <RantDictionaryTable>();
            }

            foreach (var table in dictionary.GetTables())
            {
                _tables.Add(table);
            }
        }
Пример #3
0
        private void LoadVocab(string path, NsfwFilter filter)
        {
            if (_dictionary != null)
            {
                return;
            }

            if (!String.IsNullOrEmpty(path))
            {
                _dictionary = RantDictionary.FromDirectory(path, filter);
            }
        }
Пример #4
0
        /// <summary>
        /// Loads the specified package into the engine.
        /// </summary>
        /// <param name="package">The package to load.</param>
        /// <param name="mergeBehavior">The table merging strategy to employ.</param>
        public void LoadPackage(RantPackage package, TableMergeBehavior mergeBehavior = TableMergeBehavior.Naive)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }
            if (_loadedPackages.Contains(RantPackageDependency.Create(package)))
            {
                return;
            }

            var patterns = package.GetPatterns();
            var tables   = package.GetTables();

            if (patterns.Any())
            {
                foreach (var pattern in patterns)
                {
                    _patternCache[pattern.Name] = pattern;
                    if (pattern.Module != null)
                    {
                        PackageModules[pattern.Name] = pattern.Module;
                    }
                }
            }

            if (tables.Any())
            {
                if (_dictionary == null)
                {
                    _dictionary = new RantDictionary(tables, mergeBehavior);
                }
                else
                {
                    foreach (var table in tables)
                    {
                        _dictionary.AddTable(table, mergeBehavior);
                    }
                }
            }

            _loadedPackages.Add(RantPackageDependency.Create(package));

            foreach (var dependency in package.GetDependencies())
            {
                RantPackage pkg;
                if (!_resolver.TryResolvePackage(dependency, out pkg))
                {
                    throw new FileNotFoundException($"Package '{package}' was unable to resolve dependency '{dependency}'");
                }
                LoadPackage(pkg, mergeBehavior);
            }
        }
Пример #5
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));
        }
Пример #6
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);
        }
Пример #7
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));
        }
Пример #8
0
 public override bool Test(RantDictionary dictionary, RantDictionaryTable table, RantDictionaryEntry entry, int termIndex, Query query) => TestAgainst(entry[termIndex].SyllableCount);
Пример #9
0
 /// <summary>
 /// Creates a new RantEngine object with the specified vocabulary.
 /// </summary>
 /// <param name="dictionary">The vocabulary to load in this instance.</param>
 public RantEngine(RantDictionary dictionary)
 {
     _dictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary));
 }
Пример #10
0
 /// <summary>
 /// Creates a new RantEngine object with the specified vocabulary.
 /// </summary>
 /// <param name="dictionary">The vocabulary to load in this instance.</param>
 public RantEngine(RantDictionary dictionary)
 {
     _dictionary = dictionary;
 }
Пример #11
0
 public override bool Test(RantDictionary dictionary, RantDictionaryTable table, RantDictionaryEntry entry, int termIndex, Query query) => Regex.IsMatch(entry[termIndex].Value) == Outcome;
Пример #12
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);
Пример #13
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());
            }
        }