Example #1
0
        private static string LoadDataDir()
        {
            // LUCENENET specific - reformatted with :, renamed from "analysis.data.dir"
            string currentPath = SystemProperties.GetProperty("kuromoji:data:dir", AppDomain.CurrentDomain.BaseDirectory);

            // If a matching directory path is found, set our DATA_DIR static
            // variable. If it is null or empty after this process, we need to
            // load the embedded files.
            string candidatePath = System.IO.Path.Combine(currentPath, DATA_SUBDIR);

            if (System.IO.Directory.Exists(candidatePath))
            {
                return(candidatePath);
            }

            while (new DirectoryInfo(currentPath).Parent != null)
            {
                try
                {
                    candidatePath = System.IO.Path.Combine(new DirectoryInfo(currentPath).Parent.FullName, DATA_SUBDIR);
                    if (System.IO.Directory.Exists(candidatePath))
                    {
                        return(candidatePath);
                    }
                    currentPath = new DirectoryInfo(currentPath).Parent.FullName;
                }
                catch (SecurityException)
                {
                    // ignore security errors
                }
            }

            return(null); // This is the signal to load from local resources
        }
Example #2
0
        /// <summary>
        /// Get ICU configuration property value for the given name.
        /// </summary>
        /// <param name="name">The configuration property name.</param>
        /// <param name="def">The default value.</param>
        /// <returns>The configuration property value.  If the property does not
        /// exist, <paramref name="def"/> is returned.</returns>
        public static string Get(string name, string def)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(def);
            }

            string value = null;

            // Try to get an environment variable first
            value = SystemProperties.GetProperty(name, null);
            if (value != null)
            {
                return(value);
            }

            try
            {
                value = global::ICU4N.ICUConfig.ResourceManager.GetString(name.Replace(".", "_"));
                if (value != null)
                {
                    return(value);
                }
            }
            catch (MissingManifestResourceException)
            {
            }

            return(def);
        }
Example #3
0
        /// <summary>
        /// Read both algorithm and config properties.
        /// </summary>
        /// <param name="algReader">From where to read algorithm and config properties.</param>
        /// <exception cref="IOException">If there is a low-level I/O error.</exception>
        public Config(TextReader algReader)
        {
            // read alg file to array of lines
            IList <string> lines          = new List <string>();
            int            lastConfigLine = 0;
            string         line;

            while ((line = algReader.ReadLine()) != null)
            {
                lines.Add(line);
                if (line.IndexOf('=') > 0)
                {
                    lastConfigLine = lines.Count;
                }
            }
            algReader.Dispose();
            // copy props lines to string
            MemoryStream ms     = new MemoryStream();
            TextWriter   writer = new StreamWriter(ms);

            for (int i = 0; i < lastConfigLine; i++)
            {
                writer.WriteLine(lines[i]);
            }
            // read props from string
            this.props = new Dictionary <string, string>();
            writer.Flush();
            ms.Position = 0;
            props.Load(ms);

            // make sure work dir is set properly
            string temp;

            if (!props.TryGetValue("work.dir", out temp) || temp == null)
            {
                props["work.dir"] = SystemProperties.GetProperty("benchmark.work.dir", "work");
            }

            if (props.TryGetValue("print.props", out temp))
            {
                if (temp.Equals("true", StringComparison.OrdinalIgnoreCase))
                {
                    PrintProps();
                }
            }
            else if (DEFAULT_PRINT_PROPS)
            {
                PrintProps();
            }

            // copy algorithm lines
            var sb = new StringBuilder();

            for (int i = lastConfigLine; i < lines.Count; i++)
            {
                sb.Append(lines[i]);
                sb.Append(NEW_LINE);
            }
            algorithmText = sb.ToString();
        }
        public virtual void ReadPreconfiguredEnvironmentTest()
        {
            string testKey   = "tests:setup";
            string testValue = "setup";

            Assert.AreEqual(testValue, ConfigurationSettings.CurrentConfiguration[testKey]);
            Assert.AreEqual(testValue, SystemProperties.GetProperty(testKey));
        }
 public virtual void TestRuntimeEnviromentOverrideSetting()
 {
     Assert.AreEqual("fr", ConfigurationSettings.CurrentConfiguration["tests:locale"]);
     Assert.AreEqual("fr", SystemProperties.GetProperty("tests:locale"));
     ConfigurationSettings.CurrentConfiguration["tests:locale"] = "en";
     Assert.AreEqual("en", ConfigurationSettings.CurrentConfiguration["tests:locale"]);
     Assert.AreEqual("en", SystemProperties.GetProperty("tests:locale"));
 }
Example #6
0
        /// <summary>
        /// Entry point to the DiffIt application.
        /// <para>
        /// This application takes one argument, the path to a file containing a
        /// stemmer table. The program reads the file and generates the patch commands
        /// for the stems.
        /// </para>
        /// </summary>
        /// <param name="args">the path to a file containing a stemmer table</param>
        public static void Main(string[] args)
        {
            int ins = Get(0, args[0]);
            int del = Get(1, args[0]);
            int rep = Get(2, args[0]);
            int nop = Get(3, args[0]);
            // LUCENENET specific - reformatted with :
            string charset       = SystemProperties.GetProperty("egothor:stemmer:charset", "UTF-8");
            var    stemmerTables = new List <string>();

            // LUCENENET specific
            // command line argument overrides environment variable or default, if supplied
            for (int i = 1; i < args.Length; i++)
            {
                if ("-e".Equals(args[i], StringComparison.Ordinal) || "--encoding".Equals(args[i], StringComparison.Ordinal))
                {
                    charset = args[i];
                }
                else
                {
                    stemmerTables.Add(args[i]);
                }
            }

            foreach (var stemmerTable in stemmerTables)
            {
                // System.out.println("[" + args[i] + "]");
                Diff diff = new Diff(ins, del, rep, nop);

                using (TextReader input = new StreamReader(new FileStream(stemmerTable, FileMode.Open, FileAccess.Read), Encoding.GetEncoding(charset)))
                {
                    string line;
                    while ((line = input.ReadLine()) != null)
                    {
                        try
                        {
                            line = line.ToLowerInvariant();
                            StringTokenizer st = new StringTokenizer(line);
                            st.MoveNext();
                            string stem = st.Current;
                            Console.WriteLine(stem + " -a");
                            while (st.MoveNext())
                            {
                                string token = st.Current;
                                if (token.Equals(stem, StringComparison.Ordinal) == false)
                                {
                                    Console.WriteLine(stem + " " + diff.Exec(token, stem));
                                }
                            }
                        }
                        catch (InvalidOperationException /*x*/)
                        {
                            // no base token (stem) on a line
                        }
                    }
                }
            }
        }
        public virtual void SetRuntimeEnvironmentTest()
        {
            string testKey   = "tests:setting";
            string testValue = "test.success";

            ConfigurationSettings.CurrentConfiguration[testKey] = testValue;
            Assert.AreEqual(testValue, ConfigurationSettings.CurrentConfiguration[testKey]);
            Assert.AreEqual(testValue, SystemProperties.GetProperty(testKey));
        }
Example #8
0
        // LUCENENET specific - changed the logic here to leave the
        // ANALYSIS_DATA_DIR an empty string if it is not found. This
        // allows us to skip loading files from disk if there are no files
        // to load (and fixes LUCENE-1817 that prevents the on-disk files
        // from ever being loaded).
        private static void Init()
        {
#if FEATURE_ENCODINGPROVIDERS
            // Support for GB2312 encoding. See: https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0
            var encodingProvider = System.Text.CodePagesEncodingProvider.Instance;
            System.Text.Encoding.RegisterProvider(encodingProvider);
#endif

            string dirName = "smartcn-data";
            //string propName = "analysis.properties";

            // Try the system property:-Danalysis.data.dir=/path/to/analysis-data
            //ANALYSIS_DATA_DIR = System.getProperty("analysis.data.dir", "");
            // LUCENENET specific - reformatted with :, renamed from "analysis.data.dir"
            ANALYSIS_DATA_DIR = SystemProperties.GetProperty("smartcn:data:dir", "");
            if (ANALYSIS_DATA_DIR.Length != 0)
            {
                return;
            }

#if FEATURE_APPDOMAIN_BASEDIRECTORY
            string currentPath = AppDomain.CurrentDomain.BaseDirectory;
#else
            string currentPath = System.AppContext.BaseDirectory;
#endif

            string candidatePath = System.IO.Path.Combine(currentPath, dirName);
            if (Directory.Exists(candidatePath))
            {
                ANALYSIS_DATA_DIR = candidatePath;
                return;
            }


            try
            {
                while (new DirectoryInfo(currentPath).Parent != null)
                {
                    candidatePath = System.IO.Path.Combine(new DirectoryInfo(currentPath).Parent.FullName, dirName);
                    if (Directory.Exists(candidatePath))
                    {
                        ANALYSIS_DATA_DIR = candidatePath;
                        return;
                    }
                    currentPath = new DirectoryInfo(currentPath).Parent.FullName;
                }
            }
            catch (SecurityException)
            {
                // ignore security errors
            }
        }
Example #9
0
        public void TestCustomConfigurationSettings()
        {
            Assert.AreEqual("banana", ConfigurationSettings.CurrentConfiguration["fruit"]);
            Assert.AreEqual("banana", SystemProperties.GetProperty("fruit"));

            Assert.AreEqual("lettuce", ConfigurationSettings.CurrentConfiguration["vegetable"]);
            Assert.AreEqual("lettuce", SystemProperties.GetProperty("vegetable"));

            Assert.AreEqual("yogurt", ConfigurationSettings.CurrentConfiguration["tests:goo"]);
            Assert.AreEqual("yogurt", SystemProperties.GetProperty("tests:goo"));

            Assert.AreEqual("pizza", ConfigurationSettings.CurrentConfiguration["tests:junk"]);
            Assert.AreEqual("pizza", SystemProperties.GetProperty("tests:junk"));
        }
        public virtual void TestSetandUnset()
        {
            string testKey      = "tests:locale";
            string testValue_fr = "fr";
            string testValue_en = "en";

            //ConfigurationSettings.CurrentConfiguration[testKey] = testValue_fr;
            Assert.AreEqual(testValue_fr, ConfigurationSettings.CurrentConfiguration["tests:locale"]);
            Assert.AreEqual(testValue_fr, ConfigurationSettings.CurrentConfiguration[testKey]);
            Assert.AreEqual(testValue_fr, SystemProperties.GetProperty(testKey));
            ConfigurationSettings.CurrentConfiguration[testKey] = testValue_en;
            Assert.AreEqual(testValue_en, ConfigurationSettings.CurrentConfiguration[testKey]);
            Assert.AreEqual(testValue_en, SystemProperties.GetProperty(testKey));
            ConfigurationSettings.CurrentConfiguration.Reload();
            Assert.AreEqual(testValue_fr, ConfigurationSettings.CurrentConfiguration[testKey]);
            Assert.AreEqual(testValue_fr, SystemProperties.GetProperty(testKey));
        }
Example #11
0
        static ICUDebug()
        {
            // ICU4N TODO: Fix debugging so it makes sense in .NET
            parameters = SystemProperties.GetProperty("ICUDebug");
            debug      = parameters != null;
            help       = debug && (parameters.Equals("") || parameters.IndexOf("help", StringComparison.Ordinal) != -1);
            if (debug)
            {
                Console.Out.WriteLine("\nICUDebug=" + parameters);
            }
            // ICU4N TODO: Java? Need to translate
            javaVersionString = SystemProperties.GetProperty("java.version", "0");

            javaVersion = GetInstanceLenient(javaVersionString);

            VersionInfo java14Version = VersionInfo.GetInstance("1.4.0");

            isJDK14OrHigher = javaVersion.CompareTo(java14Version) >= 0;
        }
Example #12
0
        static BinaryDictionary()
        {
            // LUCENENET specific - reformatted with :, renamed from "analysis.data.dir"
            string currentPath = SystemProperties.GetProperty("kuromoji:data:dir",
#if NETSTANDARD1_6
                                                              System.AppContext.BaseDirectory
#else
                                                              AppDomain.CurrentDomain.BaseDirectory
#endif
                                                              );

            // If a matching directory path is found, set our DATA_DIR static
            // variable. If it is null or empty after this process, we need to
            // load the embedded files.
            string candidatePath = System.IO.Path.Combine(currentPath, DATA_SUBDIR);

            if (System.IO.Directory.Exists(candidatePath))
            {
                DATA_DIR = candidatePath;
                return;
            }

            while (new DirectoryInfo(currentPath).Parent != null)
            {
                try
                {
                    candidatePath = System.IO.Path.Combine(new DirectoryInfo(currentPath).Parent.FullName, DATA_SUBDIR);
                    if (System.IO.Directory.Exists(candidatePath))
                    {
                        DATA_DIR = candidatePath;
                        return;
                    }
                    currentPath = new DirectoryInfo(currentPath).Parent.FullName;
                }
                catch (SecurityException)
                {
                    // ignore security errors
                }
            }
        }
Example #13
0
        /// <summary>
        /// Entry point to the Compile application.
        /// <para/>
        /// This program takes any number of arguments: the first is the name of the
        /// desired stemming algorithm to use (a list is available in the package
        /// description) , all of the rest should be the path or paths to a file or
        /// files containing a stemmer table to compile.
        /// </summary>
        /// <param name="args">the command line arguments</param>
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }

            // LUCENENET NOTE: This line does nothing in .NET
            // and also does nothing in Java...what?
            //args[0].ToUpperInvariant();

            // Reads the first char of the first arg
            backward = args[0][0] == '-';
            int  qq        = (backward) ? 1 : 0;
            bool storeorig = false;

            if (args[0][qq] == '0')
            {
                storeorig = true;
                qq++;
            }

            multi = args[0][qq] == 'M';
            if (multi)
            {
                qq++;
            }
            // LUCENENET specific - reformatted with :
            string charset       = SystemProperties.GetProperty("egothor:stemmer:charset", "UTF-8");
            var    stemmerTables = new List <string>();

            // LUCENENET specific
            // command line argument overrides environment variable or default, if supplied
            for (int i = 1; i < args.Length; i++)
            {
                if ("-e".Equals(args[i], StringComparison.Ordinal) || "--encoding".Equals(args[i], StringComparison.Ordinal))
                {
                    charset = args[i];
                }
                else
                {
                    stemmerTables.Add(args[i]);
                }
            }

            char[] optimizer = new char[args[0].Length - qq];
            for (int i = 0; i < optimizer.Length; i++)
            {
                optimizer[i] = args[0][qq + i];
            }

            foreach (var stemmerTable in stemmerTables)
            {
                // System.out.println("[" + args[i] + "]");
                Diff diff = new Diff();
                //int stems = 0; // not used
                int words = 0;


                AllocTrie();

                Console.WriteLine(stemmerTable);
                using (TextReader input = new StreamReader(
                           new FileStream(stemmerTable, FileMode.Open, FileAccess.Read), Encoding.GetEncoding(charset)))
                {
                    string line;
                    while ((line = input.ReadLine()) != null)
                    {
                        try
                        {
                            line = line.ToLowerInvariant();
                            StringTokenizer st = new StringTokenizer(line);
                            st.MoveNext();
                            string stem = st.Current;
                            if (storeorig)
                            {
                                trie.Add(stem, "-a");
                                words++;
                            }
                            while (st.MoveNext())
                            {
                                string token = st.Current;
                                if (token.Equals(stem, StringComparison.Ordinal) == false)
                                {
                                    trie.Add(token, diff.Exec(token, stem));
                                    words++;
                                }
                            }
                        }
                        catch (InvalidOperationException /*x*/)
                        {
                            // no base token (stem) on a line
                        }
                    }
                }

                Optimizer  o  = new Optimizer();
                Optimizer2 o2 = new Optimizer2();
                Lift       l  = new Lift(true);
                Lift       e  = new Lift(false);
                Gener      g  = new Gener();

                for (int j = 0; j < optimizer.Length; j++)
                {
                    string prefix;
                    switch (optimizer[j])
                    {
                    case 'G':
                        trie   = trie.Reduce(g);
                        prefix = "G: ";
                        break;

                    case 'L':
                        trie   = trie.Reduce(l);
                        prefix = "L: ";
                        break;

                    case 'E':
                        trie   = trie.Reduce(e);
                        prefix = "E: ";
                        break;

                    case '2':
                        trie   = trie.Reduce(o2);
                        prefix = "2: ";
                        break;

                    case '1':
                        trie   = trie.Reduce(o);
                        prefix = "1: ";
                        break;

                    default:
                        continue;
                    }
                    trie.PrintInfo(Console.Out, prefix + " ");
                }

                using (DataOutputStream os = new DataOutputStream(
                           new FileStream(stemmerTable + ".out", FileMode.OpenOrCreate, FileAccess.Write)))
                {
                    os.WriteUTF(args[0]);
                    trie.Store(os);
                }
            }
        }
Example #14
0
        // LUCENENET specific - changed the logic here to leave the
        // ANALYSIS_DATA_DIR an empty string if it is not found. This
        // allows us to skip loading files from disk if there are no files
        // to load (and fixes LUCENE-1817 that prevents the on-disk files
        // from ever being loaded).
        private static void Init()
        {
#if NETSTANDARD
            // Support for GB2312 encoding. See: https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0
            var encodingProvider = System.Text.CodePagesEncodingProvider.Instance;
            System.Text.Encoding.RegisterProvider(encodingProvider);
#endif

            string dirName = "analysis-data";
            //string propName = "analysis.properties";

            // Try the system property:-Danalysis.data.dir=/path/to/analysis-data
            //ANALYSIS_DATA_DIR = System.getProperty("analysis.data.dir", "");
            ANALYSIS_DATA_DIR = SystemProperties.GetProperty("analysis.data.dir", "");
            if (ANALYSIS_DATA_DIR.Length != 0)
            {
                return;
            }

#if NETSTANDARD1_6
            string currentPath = System.AppContext.BaseDirectory;
#else
            string currentPath = AppDomain.CurrentDomain.BaseDirectory;
#endif

            //FileInfo[] cadidateFiles = new FileInfo[] { new FileInfo(currentPath + "/" + dirName),
            //    new FileInfo(currentPath + "/bin/" + dirName)/*, new FileInfo("./" + propName),
            //    new FileInfo("./lib/" + propName)*/ };
            //for (int i = 0; i < cadidateFiles.Length; i++)
            //{
            //    FileInfo file = cadidateFiles[i];
            //    if (file.Exists)
            //    {
            //        ANALYSIS_DATA_DIR = file.FullName;

            //        //if (file.isDirectory())
            //        //{
            //        //    ANALYSIS_DATA_DIR = file.getAbsolutePath();
            //        //}
            //        //else if (file.isFile() && GetAnalysisDataDir(file).Length != 0)
            //        //{
            //        //    ANALYSIS_DATA_DIR = GetAnalysisDataDir(file);
            //        //}
            //        break;
            //    }
            //}

            string candidatePath = System.IO.Path.Combine(currentPath, dirName);
            if (Directory.Exists(candidatePath))
            {
                ANALYSIS_DATA_DIR = candidatePath;
                return;
            }


            try
            {
                while (new DirectoryInfo(currentPath).Parent != null)
                {
                    candidatePath = System.IO.Path.Combine(new DirectoryInfo(currentPath).Parent.FullName, dirName);
                    if (Directory.Exists(candidatePath))
                    {
                        ANALYSIS_DATA_DIR = candidatePath;
                        return;
                    }
                    currentPath = new DirectoryInfo(currentPath).Parent.FullName;
                }
            }
            catch (SecurityException)
            {
                // ignore security errors
            }


            //for (int i = 0; i < cadidateDirectories.Count; i++)
            //{
            //    DirectoryInfo dir = cadidateDirectories[i];
            //    if (dir.Exists)
            //    {
            //        ANALYSIS_DATA_DIR = dir.FullName;
            //        break;
            //    }
            //}

            //if (ANALYSIS_DATA_DIR.Length == 0)
            //{
            //    // Dictionary directory cannot be found.
            //    throw new Exception("WARNING: Can not find lexical dictionary directory!"
            //     + " This will cause unpredictable exceptions in your application!"
            //     + " Please refer to the manual to download the dictionaries.");
            //}
        }
 public virtual void TestXMLConfiguration()
 {
     // TODO - not working with XML.
     Assert.AreEqual("0x00000010", ConfigurationSettings.CurrentConfiguration["tests:seed"]);
     Assert.AreEqual("0x00000010", SystemProperties.GetProperty("tests:seed"));
 }