Example #1
0
        public void TestDistance()
        {
            var instance = new RatcliffObershelp();

            NullEmptyTests.TestDistance(instance);

            /// TODO: regular (non-null/empty) distance tests
        }
Example #2
0
        public void TestSimilarity()
        {
            var instance = new RatcliffObershelp();

            /// test data from other algorithms
            /// "My string" vs "My tsring"
            /// Substrings:
            /// "ring" ==> 4, "My s" ==> 3, "s" ==> 1
            /// Ratcliff-Obershelp = 2*(sum of substrings)/(length of s1 + length of s2)
            ///                    = 2*(4 + 3 + 1) / (9 + 9)
            ///                    = 16/18
            ///                    = 0.888888888888889
            Assert.Equal(
                expected: 0.888888888888889,
                actual: instance.Similarity("My string", "My tsring"),
                precision: 15 /// 0.000000000000001
                );

            /// test data from other algorithms
            /// "My string" vs "My tsring"
            /// Substrings:
            /// "My " ==> 3, "tri" ==> 3, "g" ==> 1
            /// Ratcliff-Obershelp = 2*(sum of substrings)/(length of s1 + length of s2)
            ///                    = 2*(3 + 3 + 1) / (9 + 9)
            ///                    = 14/18
            ///                    = 0.777777777777778
            Assert.Equal(
                expected: 0.777777777777778,
                actual: instance.Similarity("My string", "My ntrisg"),
                precision: 15 /// 0.000000000000001
                );

            /// test data from essay by Ilya Ilyankou
            /// "Comparison of Jaro-Winkler and Ratcliff/Obershelp algorithms
            /// in spell check"
            /// https://ilyankou.files.wordpress.com/2015/06/ib-extended-essay.pdf
            /// p13, expected result is 0.857
            Assert.Equal(
                expected: 0.857,
                actual: instance.Similarity("MATEMATICA", "MATHEMATICS"),
                precision: 3 /// 0.001
                );

            /// test data from stringmetric
            /// https://github.com/rockymadden/stringmetric
            /// expected output is 0.736842105263158
            Assert.Equal(
                expected: 0.736842105263158,
                actual: instance.Similarity("aleksander", "alexandre"),
                precision: 15 /// 0.000000000000001
                );

            /// test data from stringmetric
            /// https://github.com/rockymadden/stringmetric
            /// expected output is 0.666666666666667
            Assert.Equal(
                expected: 0.666666666666667,
                actual: instance.Similarity("pennsylvania", "pencilvaneya"),
                precision: 15 /// 0.000000000000001
                );

            /// test data from wikipedia
            /// https://en.wikipedia.org/wiki/Gestalt_Pattern_Matching
            /// expected output is 14/18 = 0.777777777777778‬
            Assert.Equal(
                expected: 0.777777777777778,
                actual: instance.Similarity("WIKIMEDIA", "WIKIMANIA"),
                precision: 15 /// 0.000000000000001
                );

            /// test data from wikipedia
            /// https://en.wikipedia.org/wiki/Gestalt_Pattern_Matching
            /// expected output is 24/40 = 0.6
            Assert.Equal(
                expected: 0.6,
                actual: instance.Similarity("GESTALT PATTERN MATCHING", "GESTALT PRACTICE"),
                precision: 15 /// 0.000000000000001
                );

            NullEmptyTests.TestSimilarity(instance);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:AppFolderIcon.IconSelectionForm"/> class.
        /// </summary>
        /// <param name="iconDirectoryPath">Icon directory path.</param>
        public IconSelectionForm(string iconDirectoryPath)
        {
            // Declare reusable icon file variable
            string iconFilePath = string.Empty;

            // Set icon directory path
            this.iconDirectoryPath = iconDirectoryPath;

            // The InitializeComponent() call is required for Windows Forms designer support.
            this.InitializeComponent();

            /* Populate TreeView */

            // Assign image list
            this.iconTreeView.ImageList = this.iconImageList;

            // Clear image indexes
            this.iconTreeView.ImageIndex         = -1;
            this.iconTreeView.SelectedImageIndex = -1;

            // Set sbudirectories list
            var subdirectoryList = Directory.GetDirectories(iconDirectoryPath, "*", SearchOption.TopDirectoryOnly);

            // Iterate subdirectories
            foreach (var subdirectory in subdirectoryList)
            {
                // Error handling & logging
                try
                {
                    // Get .exe file(s)
                    List <string> exeFileList = Directory.GetFiles(subdirectory, "*.exe", SearchOption.TopDirectoryOnly).ToList();

                    // Check for no exe file
                    if (exeFileList.Count == 0)
                    {
                        // Halt flow
                        continue;
                    }

                    // Set name
                    var subdirectoryName = Path.GetFileName(subdirectory);

                    // Add to tree view
                    this.iconTreeView.Nodes.Add(subdirectory, subdirectoryName);

                    // Declare exe file icon list
                    var exeFileIconList = new List <string>();

                    // Add exe files list to tree view node
                    foreach (var exeFilePath in exeFileList)
                    {
                        // Set name
                        var exeFileName = Path.GetFileName(exeFilePath);

                        // Try to get icon
                        var exeFileIcon = Icon.ExtractAssociatedIcon(exeFilePath);

                        // Check there's an icon
                        if (exeFileIcon != null)
                        {
                            // Add icon to image list
                            this.iconImageList.Images.Add(exeFilePath, Icon.ExtractAssociatedIcon(exeFilePath));

                            // Add to subdirectory tree view node
                            this.iconTreeView.Nodes[subdirectory].Nodes.Add(exeFilePath, exeFileName, exeFilePath, exeFilePath);

                            // Add to exe file icon list
                            exeFileIconList.Add(exeFilePath);
                        }
                    }

                    // Check for only one file
                    if (exeFileIconList.Count == 1)
                    {
                        // Set icon file path
                        iconFilePath = exeFileIconList[0];
                    }
                    else
                    {
                        // String score dictionary
                        var exeScoreDictionary = new Dictionary <double, string>();

                        // Get score for each exe file with icon
                        foreach (var exeFileIcon in exeFileIconList)
                        {
                            // Set RatcliffObershelp
                            var ratcliffObershelp = new RatcliffObershelp();

                            // Set score
                            var score = ratcliffObershelp.Similarity(iconDirectoryPath, exeFileIcon);

                            // Add unique score only (user can select manually)
                            if (!exeScoreDictionary.ContainsKey(score))
                            {
                                exeScoreDictionary.Add(score, exeFileIcon);
                            }
                        }

                        // Set icon file path to the best score
                        iconFilePath = exeScoreDictionary[exeScoreDictionary.Keys.Min()];
                    }

                    // Set program's node icon
                    this.iconTreeView.Nodes[subdirectory].ImageKey         = iconFilePath;
                    this.iconTreeView.Nodes[subdirectory].SelectedImageKey = iconFilePath;
                }
                catch (Exception ex)
                {
                    // Declare string builder
                    StringBuilder eventBodyStringBuilder = new StringBuilder();

                    // Event body
                    eventBodyStringBuilder.AppendLine($"Folder path: {iconDirectoryPath}");
                    eventBodyStringBuilder.AppendLine($"Message: {ex.Message}");

                    // Log to disk
                    this.logEvent.WriteEvent("Folder processing error", eventBodyStringBuilder.ToString());
                }
            }
        }