Пример #1
0
        private async Task <NoteTemplateModel> FindBestNoteTemplate(string eventId, string message)
        {
            IEnumerable <NoteTemplateModel> noteTemplates = await _noteRestService.GetNoteTemplates(eventId);

            if (noteTemplates == null)
            {
                return(null);
            }

            NoteTemplateModel foundNote = noteTemplates.ToList().Find(p => p.Note.ToLower() == message.ToLower());

            if (foundNote != null)
            {
                return(foundNote);
            }

            Levenshtein       distanceTest                = new Levenshtein();
            double            minimumDistanceValue        = -1;
            NoteTemplateModel templateWithMinimumDistance = null;

            foreach (var noteTemplate in noteTemplates)
            {
                double currentDistance = distanceTest.Distance(message, noteTemplate.Note);
                if (minimumDistanceValue == -1 || minimumDistanceValue > currentDistance)
                {
                    minimumDistanceValue        = currentDistance;
                    templateWithMinimumDistance = noteTemplate;
                }
            }

            return(templateWithMinimumDistance);
        }
Пример #2
0
        private async Task <SkillResponse> HandleUnknownUser(IntentRequest intentRequest, AmazonDynamoDBClient dynamoDbClient, string name)
        {
            var allUsers = await dynamoDbClient.ScanAsync(new ScanRequest
            {
                TableName            = _TableName,
                ProjectionExpression = "Username"
            });

            if (allUsers != null && allUsers.Count > 0)
            {
                var l = new Levenshtein();

                var bestMatchedName = allUsers.Items
                                      .Select(x => x["Username"].S)
                                      .OrderBy(x => l.Distance(x, name))
                                      .FirstOrDefault();

                if (bestMatchedName != null)
                {
                    intentRequest.Intent.Slots.TryAdd("name", new Slot {
                        Value = bestMatchedName
                    });
                    intentRequest.Intent.Slots["name"].Value = bestMatchedName;
                    return(ResponseBuilder.DialogConfirmIntent(new PlainTextOutputSpeech
                    {
                        Text = $"I did not find {name}, do you mean {bestMatchedName}"
                    }, intentRequest.Intent));
                }
            }

            return(null);
        }
        public override async Task <bool> ProcessMessage(Friend friend, string message)
        {
            string      msg = message.ForMachineComparison();
            Levenshtein l   = new Levenshtein(msg);

            Freeform.FreeformPhrase closestPhrase = null;
            int closestDistance = int.MaxValue;

            foreach (var phrase in Overseer.FreeformPhrases.All)
            {
                string phr      = phrase.Phrase.ForMachineComparison();
                int    distance = l.Distance(phr);
                if (distance < closestDistance)
                {
                    closestDistance = distance;
                    closestPhrase   = phrase;
                }
            }
            if (closestDistance <= 2)
            {
                await closestPhrase.Action(friend, Overseer);

                return(true);
            }
            else if (closestDistance <= 4)
            {
                await Overseer.Speaking.SendMessage(friend,
                                                    "Did you mean '" + closestPhrase.Phrase + "'?",
                                                    new[] { new Conversation.QuickReply(closestPhrase.Phrase) });

                return(true);
            }
            return(false);
        }
Пример #4
0
 // Test
 static void Main(string[] args)
 {
     Console.WriteLine("Test Levenshtein:");
     Console.WriteLine("Hello World -> Hello world :" + Levenshtein.Distance("Hello World", "Hello world"));
     Console.WriteLine("Gorilla -> Guerilla :" + Levenshtein.Distance("Gorilla", "Guerilla"));
     Console.WriteLine("foo -> bar :" + Levenshtein.Distance("foo", "bar"));
 }
            public override async Task <(Archive?Archive, TempFile NewFile)> FindUpgrade(Archive a, Func <Archive, Task <AbsolutePath> > downloadResolver)
            {
                var files = await GetFilesInGroup();

                var nl = new Levenshtein();

                foreach (var newFile in files.OrderBy(f => nl.Distance(a.Name.ToLowerInvariant(), f.Name.ToLowerInvariant())))
                {
                    /*
                     * var existing = await downloadResolver(newFile);
                     * if (existing != default) return (newFile, new TempFile());*/

                    var tmp = new TempFile();
                    await DownloadDispatcher.PrepareAll(new[] { newFile.State });

                    if (await newFile.State.Download(newFile, tmp.Path))
                    {
                        newFile.Size = tmp.Path.Size;
                        newFile.Hash = await tmp.Path.FileHashAsync();

                        return(newFile, tmp);
                    }

                    await tmp.DisposeAsync();
                }
                return(default);
Пример #6
0
        public async Task <ActionResult <GidHelper> > SearchGidHelpers(string searchText)
        {
            GidHelper currentGidHelper = null;
            var       results          = await _context.GidHelpers.ToListAsync();

            foreach (var result in results)
            {
                if (result.Question.ToLower().Contains(searchText.ToLower()))
                {
                    currentGidHelper = result;
                    break;
                }

                if (Levenshtein.Distance(result.Question.ToLower(), searchText.ToLower()) < 10)
                {
                    currentGidHelper = result;
                    break;
                }
            }

            if (currentGidHelper == null)
            {
                return(new GidHelper());
            }

            return(currentGidHelper);
        }
Пример #7
0
        private static void ThrowInvalidVerb(string param)
        {
            int           threshold = param.Length / 2;
            List <string> similar   = new List <string>();

            foreach (string verb in Config.Default.Providers.Keys)
            {
                int distance = Levenshtein.DamerauLevenshteinDistance(param.ToLower(), verb, threshold);
                if (distance <= threshold)
                {
                    similar.Add(verb);
                }
            }

            StringBuilder msg = new StringBuilder($"gx: '{param}' is not a gx command.{Environment.NewLine}");

            if (similar.Count > 0)
            {
                msg.AppendLine("");
                msg.AppendLine("The most similar commands are");
                foreach (string cmd in similar)
                {
                    msg.AppendLine($"\t{cmd}");
                }
            }
            msg.AppendLine("");
            msg.Append("See 'gx help'.");

            throw new ApplicationException(msg.ToString());
        }
Пример #8
0
        public static List <ParallelSearchResult> ArrayThreadTask(object paramObj)
        {
            Levenshtein L1 = new Levenshtein();
            ParallelSearchThreadParam param = (ParallelSearchThreadParam)paramObj;

            string wordUpper = param.wordPattern.Trim().ToUpper();

            List <ParallelSearchResult> Result = new List <ParallelSearchResult>();

            foreach (string str in param.tempList)
            {
                int dist = L1.Distance(str.ToUpper(), wordUpper);

                if (dist <= param.maxDist)
                {
                    ParallelSearchResult temp = new ParallelSearchResult()
                    {
                        word      = str,
                        dist      = dist,
                        ThreadNum = param.ThreadNum
                    };
                    Result.Add(temp);
                }
            }
            return(Result);
        }
Пример #9
0
        public void PassingNullSecondStringComputeTest()
        {
            System.ArgumentNullException ex =
                Assert.Throws <System.ArgumentNullException>(() => Levenshtein.Compute("sddss", null));

            Assert.Contains("Value cannot be null.", ex.Message);
        }
Пример #10
0
        public void GenerateMappingNode()
        {
            if (oldClassModel == null || newClassModel == null)
            {
                throw new NullReferenceException(
                          "Can't call an empty checkHasBeenMapped without knowing the oldClassData and the newClassData");
            }

            foreach (FieldModel field in oldClassModel.Fields)
            {
                MergeNode mergeNode = new MergeNode()
                {
                    OriginalValue = field.Name
                };

                FieldModel mergeNodeType = oldClassModel.Fields
                                           .First(data => data.Name == mergeNode.OriginalValue);

                mergeNode.Type       = mergeNodeType.Type?.FullName;
                mergeNode.IsIterable = mergeNodeType.IsIterable;

                mergeNode.Options = newClassModel.Fields?
                                    .OrderBy(newField =>
                                             Levenshtein.Compute(
                                                 field.Name,
                                                 newField.Name))
                                    .Select(data => data.Name).ToArray() ?? new string[0];


                mergeNode.NameToExportTo = mergeNode.Options.Length > 0 ? mergeNode.Options[0] : "";

                MergeNodes.Add(mergeNode);
            }
        }
Пример #11
0
        public void PassingTwoDiferenceStringComputeTest(string s1, string s2)
        {
            var result = Levenshtein.Compute(s1, s2);

            Assert.NotEqual(s1, s2);
            Assert.Equal(result, 11);
        }
Пример #12
0
        public void PassingEmptyFirstStringComputeTest()
        {
            System.ArgumentException ex =
                Assert.Throws <System.ArgumentException>(() => Levenshtein.Compute(string.Empty, "sddss"));

            Assert.Contains("Input cannot be empty", ex.Message);
        }
Пример #13
0
 /**
  * FIND PEOPLE WITH NAME OR STRING
  * @param string ppl
  * @return void
  * */
 public void FindPeople(string ppl)
 {
     foreach (var data in dataSource)
     {
         foreach (var x in data.m_People)
         {
             int dist = Levenshtein.Distance(ppl.ToLower(), x.ToLower());
             if (dist <= 3)
             {
                 Monitor.Enter(dgrLock);
                 try {
                     //TODO: CHECK FOR DOUBLE ENTRIES BEFORE ADD
                     //IF PERSON  SKIP ENTRY
                     dataGroupResults.Add(data);
                 }
                 catch (Exception c) {
                     //LOG EXCEPTION
                 }
                 finally {
                     Monitor.Exit(dgrLock);
                 }
             }
         }
     }
 }
Пример #14
0
        private void SearchButton_Click(object sender, EventArgs e)
        {
            SuspendLayout();
            listView1.Items.Clear();
            toolStripTextBox1.Clear();
            double relevance = Double.Parse(RelevanceBox.Text) / 100;
            int    limit     = Int32.Parse(LimitBox.Text);
            var    results   = files.Select(p => new
            {
                Value     = p,
                Revelance = Math.Max(Levenshtein.Percentage(p.Key, SearchBox.Text),
                                     Levenshtein.Percentage(p.Value.Name, SearchBox.Text))
            }).OrderByDescending(p => p.Revelance).Where(p => p.Revelance > relevance).Select(p => p.Value.Value);


            foreach (var result in results.Where((p, i) => i < limit))
            {
                var lvi = new ListViewItem(result.Path, 2);
                lvi.SubItems.Add(result.OriginalSize.ToString());
                lvi.SubItems.Add(result.CompressedSize.ToString());
                listView1.Items.Add(lvi);
            }

            ResumeLayout();
        }
Пример #15
0
        static int RunWithOptions(CalculationOptions calculationOptions)
        {
            Console.WriteLine("========================");
            Console.WriteLine($"EnableThreadingAfterXCharacters: {calculationOptions.EnableThreadingAfterXCharacters}");
            Console.WriteLine($"MinimumCharactersPerThread: {calculationOptions.MinimumCharactersPerThread}");
            Console.WriteLine("========================");

            var numberOfFailures = 0;

            for (var i = 0; i < NUMBER_OF_CHECKS; i++)
            {
                var source = WordGenerator.GenerateWord(WORD_LENGTH);
                var target = WordGenerator.GenerateWord(WORD_LENGTH);

                var baseline      = Benchmarks.LevenshteinBaseline.GetDistance(source, target);
                var quickenshtein = Levenshtein.GetDistance(source, target);

                if (baseline != quickenshtein)
                {
                    Console.WriteLine($"FAILED ({i + 1}): Expected {baseline}, Actual {quickenshtein}");
                    Console.WriteLine($"Source: {source}");
                    Console.WriteLine($"Target: {target}");
                    numberOfFailures++;
                }
            }

            Console.WriteLine("========================");
            Console.WriteLine($"Number of Failures: {numberOfFailures}");
            Console.WriteLine("========================");
            Console.WriteLine();

            return(numberOfFailures);
        }
Пример #16
0
        public async Task <List <Synopsis> > GetSynopsesAsync(string query)
        {
            var tracker = await GetTrackerAsync();

            var rows        = new List <RowData>();
            var levenshtein = new Levenshtein();

            foreach (var sheet in tracker.Sheets)
            {
                var data = sheet.Data;

                var results = data
                              .Select(x => x.RowData)
                              .First()
                              .Where(x => x.Values.Count >= 11)
                              .Where(x => x.Values[1].FormattedValue != null);

                rows.AddRange(results);
            }

            return(rows
                   .OrderByDescending(x => levenshtein.Distance(query, x.Values[1].FormattedValue))
                   .Select(x => x.ToSynopsis())
                   .Reverse()
                   .ToList());
        }
Пример #17
0
        protected override void OnMatch(string sender, string nick, string host, string filter, string message, string target)
        {
            var ip = filter;

            nick = bracketsRegex.Replace(nick, string.Empty);

            var nicks    = nicksOnIps.GetOrAdd(ip, new HashSet <NickInfo>());
            var nickInfo = new NickInfo(nick);

            nicks.Add(nickInfo);

            var previousNicks = nicks.Where(x => Levenshtein.Distance(nickInfo.LowerCaseNick, x.LowerCaseNick) >= 4 && x.Used == false).ToArray();

            if (previousNicks.Length == 0)
            {
                return;
            }

            ReportToIrc($@"{"CLONES".WrapWithColour(Colours.LightRed)} {nick} has connected from {ip}. Previous names: {string.Join(", ", previousNicks.Select(x => x.Nick))}");

            foreach (var info in nicks)
            {
                info.Used = true;
            }
        }
Пример #18
0
        /// <summary>
        /// Initializes a new instance of the App class.
        /// </summary>
        public App()
        {
            try
            {
                var assembly = Assembly.GetEntryAssembly();
                IWindowInitiator initiator = new WindowInitiator();
                IMetric          metric    = new Levenshtein();

                // Add services to the Ioc container.
                Ioc.Current.RegisterAsSingleton <IWindowLocator>(new WindowLocator(assembly, initiator, metric));
                //Ioc.Current.RegisterAsSingleton<IEventAggregator>(new EventAggregator());

                // Register windows in the WindowLocator service.
                var windowLocator = Ioc.Current.Resolve <IWindowLocator>();
                windowLocator.RegisterAll();

                // Shows the main window.
                windowLocator.ShowWindow(new MainViewModel());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
            }
        }
        private async Task Search(string navigationParameter)
        {
            if (DataSource == null || DataSource.CurrentStorageFolder == null)
            {
                return;
            }
            ObservableCollection <ExplorerItem> explorerGroups = new ObservableCollection <ExplorerItem>();
            var queryText = navigationParameter;
            var query     = queryText.ToLower();
            var items     = await DataSource.CurrentStorageFolder.GetItemsAsync();

            var itemsFilter = items.Select(item => new
            {
                Distance = item.Name.ToLower().Contains(query) ? 1 : Levenshtein.Distance(item.Name.ToLower(), query),
                Item     = item
            }
                                           ).Where(newItem => newItem.Distance >= 0.8).OrderByDescending(newItem => newItem.Distance);

            foreach (var item in itemsFilter)
            {
                if (item.Item is StorageFolder)
                {
                    explorerGroups.AddStorageItem(item.Item as StorageFolder);
                }
                else if (item.Item is StorageFile)
                {
                    explorerGroups.AddFileItem(item.Item as StorageFile);
                }
            }


            DataSource.FromSearch    = true;
            DataSource.SearchedItems = explorerGroups;
        }
Пример #20
0
        public Task <IResult <Unit> > Handle(PerformAppend request, CancellationToken cancellationToken)
        {
            var match = GetAppend(request.MatchId);

            if (match == null)
            {
                throw new InvalidOperationException();
            }

            match.Start();
            UpdateAppend(match);

            var levenshtein = new Levenshtein();
            var source      = GetDataset(match.SourceId);
            var lookup      = GetDataset(match.LookupId);

            var sourceData = GetData(source.FileId).ToArray();
            var lookupData = GetData(lookup.FileId).ToArray();

            try
            {
                var sourceRecordId = 0;
                foreach (var sourceRecord in sourceData)
                {
                    var lookupRecordId = 0;
                    foreach (var lookupRecord in lookupData)
                    {
                        var ratio = levenshtein.GetRatio(sourceRecord, lookupRecord);
                        if (ratio >= match.Threshold)
                        {
                            match.Results.Add(new AppendResult
                            {
                                SourceRecordId = sourceRecordId,
                                SourceRecord   = sourceRecord,
                                LookupRecordId = lookupRecordId,
                                LookupRecord   = lookupRecord,
                                Ratio          = ratio
                            });
                        }

                        lookupRecordId++;
                    }

                    sourceRecordId++;
                    match.Progress = sourceRecordId * 100.0 / sourceData.Length;
                    UpdateAppend(match);
                }

                match.Finish();
                UpdateAppend(match);
            }
            catch (Exception ex)
            {
                match.Error(ex);
                UpdateAppend(match);
                throw;
            }

            return(Task.FromResult(Result.Success()));
        }
Пример #21
0
        public void ZeroDistance_512_Characters()
        {
            var value    = Utilities.BuildString("abcd", 512);
            var distance = Levenshtein.GetDistance(value, value, CalculationOptions);

            Assert.AreEqual(0, distance);
        }
Пример #22
0
        private static Backstory FindBestMatch(HashSet <Backstory> backstories, Backstory backstory)
        {
            string    id        = Backstory.GetIdentifier(backstory);
            Backstory matchById = backstories.FirstOrDefault(bs => Backstory.GetIdentifier(bs) == id);

            if (matchById != null)
            {
                return(matchById);
            }

            if (Backstory.IsSolid(backstory))
            {
                Backstory matchBySolidName = backstories.FirstOrDefault(bs => bs.FirstName == backstory.FirstName && bs.LastName == backstory.LastName && bs.Slot == backstory.Slot);

                if (matchBySolidName != null)
                {
                    return(matchBySolidName);
                }
            }

            Levenshtein backstoryLevenshtein = new Levenshtein(backstory.Description);

            Backstory matchByDescription = backstories.MinValue(bs => backstoryLevenshtein.DistanceFrom(bs.Description), out int minDist);

            if (matchByDescription != null && minDist < 0.5f * backstory.Description.Length)
            {
                return(matchByDescription);
            }

            return(null);
        }
Пример #23
0
        private static CountryTagResult FindCountryTag(string countryName, IReadOnlyDictionary <string, string> countryNamesAndTags)
        {
            int minimumDistanceFound = int.MaxValue;

            (string, string)bestCountryFound = (null, null);
            foreach (var nameAndTag in countryNamesAndTags)
            {
                int distance = Levenshtein.Distance(countryName.ToUpperInvariant(), nameAndTag.Key.ToUpperInvariant());
                if (distance == 0)
                {
                    return(new CountryTagResult
                    {
                        LevenshteinDistance = 0,
                        Name = nameAndTag.Key,
                        Tag = nameAndTag.Value
                    });
                }
                if (distance < minimumDistanceFound)
                {
                    minimumDistanceFound = distance;
                    bestCountryFound     = (nameAndTag.Key, nameAndTag.Value);
                }
            }

            if (bestCountryFound.Item1 != null && bestCountryFound.Item2 != null)
            {
                return(new CountryTagResult
                {
                    LevenshteinDistance = minimumDistanceFound,
                    Name = bestCountryFound.Item1,
                    Tag = bestCountryFound.Item2
                });
            }
            return(null);
        }
Пример #24
0
        public int Compare(Candidate x, Candidate y)
        {
            if (x is null && y is null)
            {
                return(0);
            }

            if (y is null)
            {
                return(1);
            }

            if (x is null)
            {
                return(-1);
            }
            var compareTo = y.Score.CompareTo(x.Score);

            if (compareTo != 0)
            {
                return(compareTo);
            }

            var weight = y.Weight.CompareTo(x.Weight);

            if (weight != 0)
            {
                return(weight);
            }

            var xDistance = Levenshtein.Distance(_address, x.Address);
            var yDistance = Levenshtein.Distance(_address, y.Address);

            return(xDistance.CompareTo(yDistance));
        }
Пример #25
0
        public static async Task <List <StringMetrics> > GetClosestNames(string id, int numberOfRecommendations)
        {
            return(await Task.Run(() =>
            {
                List <string> closest = new List <string>();

                var twoDigit = Regex.Match(id, @"\d{2}").Value;
                var fourDigit = Regex.Match(id, @"\d{4}").Value;

                var dict = TG263Dictionary.Instance.GetDictionary();

                var targetWords = WordSplitter.Split(id);

                var commentPermutations = GetCommentedPermutations(id);
                var commented = commentPermutations
                                .Where(c =>
                {
                    var isOkay = TG263Dictionary.Instance.GetNameCompliance(c, false).GetAwaiter().GetResult();
                    return isOkay.IsNameOk;
                })
                                .Select(c =>
                {
                    var commentWords = WordSplitter.Split(c);
                    return new ConcreteStructureId()
                    {
                        StructureId = c,
                        StructIdWords = commentWords
                    };
                }).ToList();

                var concreteIds = dict.Where(d => d.StructureIds != null && d.StructureIds.Any())
                                  .SelectMany(s => s.StructureIds)
                                  .Concat(commented).ToList();

                var candidates = concreteIds.Select(s =>
                {
                    var strId = s.StructureId;
                    var words = s.StructIdWords.ToArray();
                    if (strId.Contains("00") && !string.IsNullOrEmpty(twoDigit))
                    {
                        strId = strId.Replace("00", twoDigit);
                        words = words.Select(w => w.Replace("00", twoDigit)).ToArray();
                    }
                    var metric = new StringMetrics()
                    {
                        Id = strId,
                        Dice = Dice.ComputeDistance(strId.ToUpper(), id.ToUpper()),
                        Levenshtein = Levenshtein.ComputeDistance(strId.ToUpper(), id.ToUpper())
                    };
                    return metric;
                }).ToList();

                //SORT
                var ordered = candidates
                              .OrderBy(s => (s.Dice *s.Levenshtein));

                return ordered.Take(numberOfRecommendations).ToList();
            }));
        }
Пример #26
0
        public void CompletelyDifferent_128_Characters()
        {
            var firstArg  = Utilities.BuildString("abcd", 128);
            var secondArg = Utilities.BuildString("wxyz", 128);
            var distance  = Levenshtein.GetDistance(firstArg, secondArg, CalculationOptions);

            Assert.AreEqual(128, distance);
        }
Пример #27
0
        public void Levenshtein_distance_throws_exception_if_input_string__is_null()
        {
            var levenshtein = new Levenshtein();

            Assert.ThrowsException <ArgumentNullException>(() => levenshtein.CalculateDistance("test", null));
            Assert.ThrowsException <ArgumentNullException>(() => levenshtein.CalculateDistance(null, "test"));
            Assert.ThrowsException <ArgumentNullException>(() => levenshtein.CalculateDistance(null, null));
        }
Пример #28
0
 /// <summary>
 /// Returns a list of fields that are close enough to <paramref ref="fieldName" />
 /// for us to suggest them as alternatives.
 /// </summary>
 /// <param name="fieldName">
 /// The name of a field that doesn't exist that we want to find suggestions for.
 /// </param>
 public IReadOnlyCollection <string> GetSuggestions(string fieldName)
 {
     return(_fields
            .Select(field => field.Key)
            .Union(_ignoredFields)
            .Where(suggestion => Levenshtein.Distance(suggestion, fieldName) < MaxSuggestionDistance)
            .ToList());
 }
Пример #29
0
        public static List <T> Search <T>(this IEnumerable <T> items, string searchTerm, Func <T, string> keyAction)
        {
            Levenshtein searchObject = new Levenshtein(searchTerm);

            var searchResults = items.Select(i => keyAction(i)).Select(i => searchObject.DistanceFrom(i)).Zip(items);

            return(searchResults.OrderBy(i => i.First).Select(i => i.Second).ToList());
        }
 public unsafe void CalculateRow_Sse41()
 {
     fixed(int *previousRowPtr = PreviousRow)
     fixed(char *targetPtr = TargetString)
     {
         Levenshtein.CalculateRow_Sse41(previousRowPtr, targetPtr, NumberOfCharacters, SourcePrevChar, LastInsertionCost, LastSubstitutionCost);
     }
 }
Пример #31
0
        public void Repeated_Distance_Calls_Return_Correct_Distances()
        {
            string[] testData = RandomWords.Create(100000, 20);
            int[] expected = new int[testData.Length];

            // create an instance every time
            for (int i = 0; i < testData.Length; i++)
            {
                expected[i] = this.CalculateDistance(testData[0], testData[i]);
            }

            // reuse the same instance
            Levenshtein levenshteinInstance = new Levenshtein(testData[0]);
            for (int i = 0; i < testData.Length; i++)
            {
                int actual = levenshteinInstance.Distance(testData[i]);
                Assert.AreEqual(expected[i], actual);
            }
        }
 public FastenshteinLevenshtein(string value1)
 {
     this.levenshtein = new Levenshtein(value1);
 }
Пример #33
0
 protected override int CalculateDistance(string value1, string value2)
 {
     Levenshtein lev = new Levenshtein(value1);
     return lev.Distance(value2);
 }
Пример #34
0
        void compareFiles(int progress)
        {
            if (bitmaps == null)
                return;
            comparisions = new Dictionary<ComparableBitmap, List<ComparableBitmap>>();
            processed = new List<int>();
            double p = 0;

            for (int i = 0; i < bitmaps.Count; i++)
            {
                if (bw.CancellationPending)
                    break;

                if (!processed.Contains(i) && !comparisions.ContainsKey(bitmaps[i]))
                {

                    comparisions.Add(bitmaps[i], new List<ComparableBitmap>());
                    processed.Add(i);
                    ThreadQueue q = new ThreadQueue(bitmaps.Count / 10);
                    Levenshtein alg = new Levenshtein();

                    for (int j = 0; j < bitmaps.Count; j++)
                    {
                        if (bw.CancellationPending)
                            break;
                        q.QueueUserWorkItem((WaitCallback)delegate(object a)
                        {
                            int r = (int)a;
                            if (i != r && !processed.Contains(r))
                            {
                                int k = alg.GetDistance<byte>(bitmaps[i], bitmaps[r]);
                                if (k <= Settings.Default.SimilarityTreshold)
                                {
                                    bitmaps[r].Similarity = k;
                                    comparisions[bitmaps[i]].Add(bitmaps[r]);
                                    processed.Add(r);
                                    if (OnNewDuplicateFound != null)
                                    {

                                        OnNewDuplicateFound(this, null);
                                    }
                                }
                            }

                        }, j);
                        bw.ReportProgress((int)(++p / (double)(bitmaps.Count * bitmaps.Count) * 100), string.Format(Resources.strProceessing,
                            bitmaps[i].Path.Substring(bitmaps[i].Path.LastIndexOf('\\')+1)));

                    }
                    q.WaitAll();

                }

            }
        }
Пример #35
0
        public void TestLevenshtein()
        {
            const string sNew = @"GAMBOL
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
";
            const string sOld = @"GUMBO
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
";
            try
            {
                var l = new Levenshtein();

                while (true)
                {
                    /// Original version
                    /// 
                    ///
                    /* Read the initial time. */
                    DateTime startTime = DateTime.Now;

                    l.LD(sNew, sOld);

                    /* Read the end time. */
                    DateTime stopTime = DateTime.Now;

                    /* Compute the duration between the initial and the end time. */
                    TimeSpan duration = stopTime - startTime;
                    Debug.WriteLine("Original:" + duration);

                    /// New version
                    /// 
                    ///
                    /* Read the initial time. */
                    startTime = DateTime.Now;

                    l.iLD(sNew, sOld);

                    /* Read the end time. */
                    stopTime = DateTime.Now;

                    /* Compute the duration between the initial and the end time. */
                    duration = stopTime - startTime;
                    Debug.WriteLine("New     :" + duration);

                    Debug.WriteLine("----------------");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }

        }
 protected void Button1_Click(object sender, EventArgs e)
 {
     Levenshtein l = new Levenshtein();
     this.Button1.Text = Levenshtein.EditDistance(sNew, sOld).ToString();
 }
Пример #37
0
        private static int LevenshteinDistance(string TextA, string TextB)
        {
            //Utils.DebugLogger("Comparing:\t" + TextA);
            //Utils.DebugLogger("\tto:\t" + TextB);

            Levenshtein l = new Levenshtein();

            return l.iLD(TextA, TextB);
        }