Пример #1
0
        public List <string> GetSimilarItem(string word, string mode)
        {
            var lev          = new Fastenshtein.Levenshtein(word.Format());
            var distancelist = new SortedSet <StringInfo>();
            var str          = word.Substring(0, 1);

            switch (mode)
            {
            case "wm":
                foreach (var sale in translateApi.Sale)
                {
                    if (sale.Zh.StartsWith(str))
                    {
                        var distance = lev.DistanceFrom(sale.Zh.Format());
                        distancelist.Add(new StringInfo {
                            LevDistance = distance, Name = sale.Zh
                        });
                    }
                }
                break;

            case "rm":
                foreach (var weapon in weapons)
                {
                    if (weapon.StartsWith(str))
                    {
                        var distance = lev.DistanceFrom(weapon.Format());
                        distancelist.Add(new StringInfo {
                            LevDistance = distance, Name = weapon
                        });
                    }
                }
                break;

                /*
                 * case "wiki":
                 *  foreach (var wiki in wikiwords)
                 *  {
                 *      if (wiki.StartsWith(str) || Regex.IsMatch(word, @"[a-z]"))
                 *      {
                 *          var distance = lev.DistanceFrom(wiki.Format());
                 *          distancelist.Add(new StringInfo { LevDistance = distance, Name = wiki });
                 *      }
                 *  }
                 *  break;
                 */
                //木大
            }


            return(distancelist.Where(dis => dis.LevDistance != 0).Take(5).Select(info => info.Name).ToList());
        }
Пример #2
0
        public Task <Guid> Handle(UpdateTreatmentCommand request, CancellationToken cancellationToken)
        {
            var currentTreatment = _databaseContext.Treatment.FirstOrDefault(x => x.Id == request.Id);

            var treatments = _mapper.Map <List <TreatmentModel> >(_databaseContext.Treatment.ToList());

            Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(request.Description);
            foreach (var treatmentItem in treatments)
            {
                treatmentItem.ClosedDescriptionProbability = lev.DistanceFrom(treatmentItem.Description);
            }

            var treatment = treatments.Where(x => x.ClosedDescriptionProbability <= 7).OrderByDescending(x => x.ClosedDescriptionProbability).FirstOrDefault();

            if (treatment == null)
            {
                treatment = new TreatmentModel()
                {
                    Type = request.Type, Description = request.Description, Name = request.Name,
                };
                _databaseContext.Treatment.Add(treatment);
                _databaseContext.SaveChanges();
            }

            //else
            //    _databaseContext.Relationship.Add(new Relationship()
            //    {
            //        FromType = ObjectType.Treatment,
            //        FromId = treatment.Id,
            //        ToType = ObjectType.TreatmentPayload,
            //        ToId = treatmentPayload.Id
            //    });
            _databaseContext.SaveChanges();
            return(Task.FromResult(treatment.Id));
        }
Пример #3
0
        private string GetClosestActualTemtemName(string input, List <string> list)
        {
            if (input.IndexOf("\n") != -1)
            {
                input = input.Substring(0, input.IndexOf("\n"));
            }

            if (input.IndexOf(" ") != -1)
            {
                input = input.Substring(0, input.IndexOf(" "));
            }

            //If our input is an empty string, return an empty string meaning the result is invalid
            if (input.Length == 0)
            {
                return("");
            }
            //These values here are purely nonsensical high values and serve no other purpose
            int minScore = 20000;
            int minIndex = 20000;

            Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(input);

            foreach (string element in list)
            {
                int score = lev.DistanceFrom(element);
                if (score < minScore)
                {
                    minScore = score;
                    minIndex = list.IndexOf(element);
                }
            }

            return(list[minIndex]);
        }
Пример #4
0
        private void HandleChatMessage(string message, string username, StreamWriter writer)
        {
            var   time  = DateTime.UtcNow;
            var   lev   = new Fastenshtein.Levenshtein(message);
            float score = 0f;

            for (int i = 0; i < _stringBuffer.Length; i++)
            {
                string item = _stringBuffer[i];

                if (string.IsNullOrEmpty(item))
                {
                    score += 100;
                }
                else
                {
                    score += lev.DistanceFrom(item);
                }
            }

            if (message.Length > _messageLength &&
                score >= _stringBuffer.Length &&
                score / _stringBuffer.Length - _outLiers < _granularity &&
                (time - _lastMessage).TotalMilliseconds < _seconds * 1000)
            {
                // We have a winner!
                Console.WriteLine($"Score : {score}");
                Suspend(username, writer);
            }

            _lastMessage = time;
            AddNewMessage(message);

            Console.WriteLine($"{username} : {message}");
        }
Пример #5
0
        public static SearchResult <T> Find <T>(IEnumerable <T> collection, string input, Func <T, ISearchableProperty> searchProperties)
        {
            var normalizedInput = input.ToLower();

            var lev = new Fastenshtein.Levenshtein(normalizedInput);

            var lowestDistance = int.MaxValue;
            var currentWinner  = default(T);

            foreach (var item in collection)
            {
                foreach (var searchProperty in searchProperties(item).AsStrings)
                {
                    var termToMatchAgainst = searchProperty.ToLower();
                    if (termToMatchAgainst == normalizedInput)
                    {
                        return(new SearchResult <T>(item, 0));
                    }

                    var distance = lev.DistanceFrom(termToMatchAgainst);
                    if (distance >= lowestDistance)
                    {
                        continue;
                    }

                    lowestDistance = distance;
                    currentWinner  = item;
                }
            }

            return(new SearchResult <T>(currentWinner, lowestDistance));
        }
Пример #6
0
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(SearchTermBox.Text);
            List <Dive> sorted           = dives.OrderBy(o => lev.DistanceFrom(o.DiveData)).ToList();

            dives = sorted;
            DiveListBox.Items.Clear();
            foreach (Dive dive in dives)
            {
                DiveListBox.Items.Add(dive);
            }
        }
Пример #7
0
        public List <string> GetSimilarItem(string word)
        {
            var lev          = new Fastenshtein.Levenshtein(word);
            var distancelist = new SortedSet <StringInfo>();

            foreach (var sale in translateApi.Sale)
            {
                var distance = lev.DistanceFrom(sale.Zh.Format());
                distancelist.Add(new StringInfo {
                    LevDistance = distance, Name = sale.Zh
                });
            }

            return(distancelist.Where(dis => dis.LevDistance != 0).Take(5).Select(info => info.Name).ToList());
        }
Пример #8
0
        protected override void Execute(CodeActivityContext context)
        {
            var dicString    = new Dictionary <string, double>();
            var listStrSouce = CandidateStringList.Get(context);
            var strTarget    = TargetText.Get(context);
            var lev          = new Fastenshtein.Levenshtein(strTarget);
            var intStrLength = strTarget.Length;

            if (String.IsNullOrEmpty(strTarget))
            {
                throw (new ArgumentException("Target Text is null or empty"));
            }

            //Calculate Normalized Edit Distance
            foreach (string strItem in listStrSouce)
            {
                if (String.IsNullOrEmpty(strItem))
                {
                    throw (new ArgumentException("CandidateStringList contains null or empty"));
                }
                dicString[strItem] = ((double)lev.DistanceFrom(strItem) / (double)Math.Max(intStrLength, strItem.Length));
            }

            //Sort
            string[] sortedKeys   = new string[dicString.Count];
            double[] sortedValues = new double[dicString.Count];

            dicString.Keys.CopyTo(sortedKeys, 0);
            dicString.Values.CopyTo(sortedValues, 0);
            Array.Sort(sortedValues, sortedKeys);

            //Check if matched
            if (sortedKeys[0] == strTarget)
            {
                IsMatched.Set(context, true);
            }
            else
            {
                IsMatched.Set(context, false);
            }

            SortedTextArray.Set(context, sortedKeys);
            SortedNormalizedEditDistanceArray.Set(context, sortedValues);
        }
Пример #9
0
        protected override void OnPlayerCommandText(BasePlayer player, CommandTextEventArgs e)
        {
            base.OnPlayerCommandText(player, e);

            if (e.Success == false)
            {
                var commandManager = Services.GetService <ICommandsManager>();

                Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(e.Text);
                var similarCommands          = commandManager.Commands
                                               .OrderBy(command => lev.DistanceFrom(command.ToString()))
                                               .Take(3);

                player.SendClientMessage(
                    $"Nie znaleziono komendy: {e.Text}. Podobne komendy: {String.Join(", ", similarCommands)}?");
                e.Success = true;
                return;
            }
        }
Пример #10
0
        private string GetClosestActualTemtemName(string input, List <string> list)
        {
            //These values here are purely nonsensical high values and serve no other purpose
            int minScore    = 20000;
            int minDistance = 20000;

            Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(input);

            foreach (string element in list)
            {
                int score = lev.DistanceFrom(element);
                if (score < minScore)
                {
                    minScore    = score;
                    minDistance = list.IndexOf(element);
                }
            }

            return(list[minDistance]);
        }
Пример #11
0
        private async Task EnrichWithSeriesDetailsId(List <EpisodeFile> newEpisodes)
        {
            var functionCache = new FunctionCache <string, Domain.Models.Details.Search.SeriesResult[]>(_detailsApi.SearchTvShows);

            foreach (var episodeFile in newEpisodes)
            {
                var searchResults = await functionCache.Invoke(episodeFile.Name);

                if (searchResults == null || !searchResults.Any())
                {
                    episodeFile.SeriesDetailsId = null;
                    continue;
                }

                var actual         = new Fastenshtein.Levenshtein(episodeFile.Name);
                var selectedResult = searchResults.OrderBy(sr => actual.DistanceFrom(sr.Name)).First();

                episodeFile.SeriesDetailsId = selectedResult.Id;
            }
        }
Пример #12
0
        public async Task <DbFilmDetails[]> FindFilmDetails(IEnumerable <FilmFile> newFilm, Library library)
        {
            var allDetails    = new Dictionary <int, DbFilmDetails>();
            var functionCache = new FunctionCache <int, string, DbFilmDetails>(FetchFilmDetails);

            foreach (var filmFile in newFilm)
            {
                var searchResults = await _detailsApi.SearchMovies(filmFile.Title, filmFile.Year);

                if (!searchResults.Any())
                {
                    searchResults = await _detailsApi.SearchMovies(filmFile.Title, 0);
                }

                if (!searchResults.Any())
                {
                    filmFile.FilmDetailsId = null;
                    continue;
                }

                var actual         = new Fastenshtein.Levenshtein(filmFile.Title);
                var selectedResult = searchResults.OrderBy(sr => actual.DistanceFrom(sr.Title)).First();

                var details = await functionCache.Invoke(selectedResult.Id, library.Language);

                if (details == null)
                {
                    continue;
                }

                filmFile.FilmDetailsId = details.Id;
                allDetails[details.Id] = details;
            }

            return(allDetails.Values.ToArray());
        }
        public static string BestGuess <TItem>(string target, IEnumerable <TItem> items, Func <TItem, string> selector, int maximumGuessDistance = 7, int maximumGuesses = 4)
        {
            var lev = new Fastenshtein.Levenshtein(target);

            var orderedItems = items
                               .Select(item => (Item: item, Text: selector(item), Distance: lev.DistanceFrom(selector(item))))
                               .OrderBy(tuple => tuple.Distance)
                               .ToList();

            if (!orderedItems.Any())
            {
                return(string.Empty);
            }

            var best = orderedItems.First();

            if (best.Distance > maximumGuessDistance)
            {
                return(string.Empty);
            }

            var guesses = orderedItems
                          .Where(tuple => tuple.Distance <= best.Distance + 1)
                          .OrderBy(tuple => tuple.Distance)
                          .ThenByDescending(tuple => tuple.Text.StartsWith(target, StringComparison.InvariantCultureIgnoreCase))
                          .ThenByDescending(tuple => tuple.Text.EndsWith(target, StringComparison.InvariantCultureIgnoreCase))
                          .ToList();

            if (guesses.Count > maximumGuesses)
            {
                guesses = orderedItems
                          .Where(tuple => tuple.Distance == best.Distance)
                          .OrderByDescending(tuple => tuple.Text.StartsWith(target, StringComparison.InvariantCultureIgnoreCase))
                          .ThenByDescending(tuple => tuple.Text.EndsWith(target, StringComparison.InvariantCultureIgnoreCase))
                          .ToList();
            }

            if (!guesses.Any() || guesses.Count > maximumGuesses)
            {
                return(string.Empty);
            }

            switch (guesses.Count)
            {
            case 1:
                return($"Did you mean '{guesses.Single().Text}'?");

            case 2:
                return($"Did you mean '{guesses[0].Text}' or '{guesses[1].Text}'?");

            default:
                return($"Did you mean '{string.Join("', or '", guesses.Select(tuple => tuple.Text))}'?");
            }
        }
Пример #14
0
        public Phone[] QueryPhonesByName(string phoneName)
        {
            //List<Phone> similarPhones = new List<Phone> { };
            List <Phone> matchedPhones = new List <Phone> {
            };
            SortedDictionary <double, List <Phone> > similarPhones =
                new SortedDictionary <double, List <Phone> > {
            };

            Phone[] allPhones = this._context.Phones.ToArray();

            Fastenshtein.Levenshtein lev;
            foreach (Phone phone in allPhones)
            {
                double   minDistance = Math.Max(phoneName.Length, phone.Name.Length);
                string[] words       = phone.Name.Split(" ");
                foreach (string q in phoneName.Split(" "))
                {
                    lev = new Fastenshtein.Levenshtein(q);
                    foreach (string word in words)
                    {
                        int tempDistance = lev.DistanceFrom(word);
                        if (tempDistance == Math.Max(q.Length, word.Length))
                        {
                            continue;
                        }
                        minDistance = Math.Min(minDistance, tempDistance);
                    }
                }

                lev          = new Fastenshtein.Levenshtein(phoneName);
                minDistance += (lev.DistanceFrom(phone.Name) / 10.0);
                //Arbitrary number chosen for similarity necessary
                //create distance to list of phone mapping
                if (minDistance < 3)
                {
                    if (similarPhones.ContainsKey(minDistance))
                    {
                        similarPhones[minDistance].Add(phone);
                    }
                    else
                    {
                        similarPhones.Add(minDistance, new List <Phone> {
                            phone
                        });
                    }
                }
            }

            //foreach (Phone phone in matchedPhones)
            //{
            //    lev = new Fastenshtein.Levenshtein(phoneName);
            //    int distance = lev.DistanceFrom(phone.Name);

            //    if (similarPhones.ContainsKey(distance))
            //    {
            //        similarPhones[distance].Add(phone);
            //    }
            //    else
            //    {
            //        similarPhones.Add(distance, new List<Phone> { phone });
            //    }
            //}

            List <Phone> sortedPhones = new List <Phone> {
            };

            foreach (List <Phone> phones in similarPhones.Values)
            {
                sortedPhones.AddRange(phones);
            }

            return(sortedPhones.ToArray());
        }
Пример #15
0
        protected override void Execute(CodeActivityContext context)
        {
            List <string> listStrSrc = CandidateStringList.Get(context);

            int intListCount = listStrSrc.Count;

            if (intListCount % 2 == 0 || intListCount < 3)
            {
                throw (new ArgumentException("Number of list item must be odd number and no less than 3"));
            }

            Dictionary <string, int>    dicStringCount = new Dictionary <string, int>();
            Dictionary <string, double> dicRMSE        = new Dictionary <string, double>();

            //Check CandidateStringList
            int i = 0;

            while (i < listStrSrc.Count)
            {
                if (String.IsNullOrEmpty(listStrSrc[i]))
                {
                    throw (new ArgumentException("Empty string is not allowed in CandidateStringList."));
                }
                i++;
            }

            //Main Calculation loop
            i = 0;
            double[] dblSumOfSquare = new double[intListCount * intListCount];
            while (i < intListCount)
            {
                //For check matching
                if (dicStringCount.ContainsKey(listStrSrc[i]))
                {
                    dicStringCount[listStrSrc[i]]++;
                }
                else
                {
                    dicStringCount[listStrSrc[i]] = 1;
                }

                //Calculate Edit Distance
                int j = 0;
                Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(listStrSrc[i]);

                while (j < intListCount)
                {
                    if (i != j)
                    {
                        double dblNormalizedEditDistance = ((double)lev.DistanceFrom(listStrSrc[j])) / (double)Math.Max(listStrSrc[i].Length, listStrSrc[j].Length);
                        dblSumOfSquare[j + i * intListCount] = Math.Pow(dblNormalizedEditDistance, 2);
                    }
                    j++;
                }
                i++;
            }

            // Calculate RMSE
            i = 0;
            while (i < intListCount)
            {
                if (!dicRMSE.ContainsKey(listStrSrc[i]))
                {
                    dicRMSE[listStrSrc[i]] = 0;
                    int j = 0;
                    while (j < intListCount)
                    {
                        if (i != j)
                        {
                            dicRMSE[listStrSrc[i]] += dblSumOfSquare[j + i * intListCount];
                        }
                        j++;
                    }
                    dicRMSE[listStrSrc[i]] = Math.Sqrt(dicRMSE[listStrSrc[i]] / (double)(intListCount - 1));
                }
                i++;
            }



            //Check matching count

            string[] sortedKeysCount   = new string[dicStringCount.Count];
            int[]    sortedValuesCount = new int[dicStringCount.Count];
            dicStringCount.Keys.CopyTo(sortedKeysCount, 0);
            dicStringCount.Values.CopyTo(sortedValuesCount, 0);
            Array.Sort(sortedValuesCount, sortedKeysCount);

            Array.Reverse(sortedValuesCount);
            Array.Reverse(sortedKeysCount);

            int    intCountMode             = sortedValuesCount[0];
            string strCandidateFromMatching = sortedKeysCount[0];

            if (intCountMode == intListCount)
            {
                Unanimous.Set(context, strCandidateFromMatching);
            }
            else
            {
                Unanimous.Set(context, string.Empty);
            }

            if (intCountMode > (intListCount >> 1))
            {
                Majority.Set(context, strCandidateFromMatching);
            }
            else
            {
                Majority.Set(context, string.Empty);
            }

            //Sort RMSE
            string[] sortedKeys   = new string[dicRMSE.Count];
            double[] sortedValues = new double[dicRMSE.Count];

            dicRMSE.Keys.CopyTo(sortedKeys, 0);
            dicRMSE.Values.CopyTo(sortedValues, 0);
            Array.Sort(sortedValues, sortedKeys);

            SortedTextArray.Set(context, sortedKeys);
            RMSEArray.Set(context, sortedValues);
        }
Пример #16
0
        public static byte[] ImageByWord(string id)
        {
            // Поиск в базе
            string pathToImgBase = @"Images\ElementsImages";

            Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(id.ToLower());
            var fls = Directory.GetFiles(pathToImgBase).Select(n => new { name = Path.GetFileNameWithoutExtension(n), path = n, dist = lev.DistanceFrom(Path.GetFileNameWithoutExtension(n).ToLower()) }).OrderBy(n => n.dist).ToList();

            var fod = fls.FirstOrDefault();

            if (fod?.dist <= 2)
            {
                return(StaticMetods.pathToImage(fod.path));
            }
            else
            {
                var    tryes     = 0;
                bool   isSuccess = false;
                byte[] img       = null;

                while (!isSuccess && tryes < 10)
                {
                    try
                    {
                        string        html = GetHtmlCode(id);
                        List <string> urls = GetUrls(html).ToList();

                        int randomUrl = rnd.Next(0, urls.Count - 1);

                        string luckyUrl = urls[randomUrl];

                        img = GetImage(luckyUrl);

                        isSuccess = true;
                    }
                    catch
                    {
                        tryes++;
                    }
                }

                return(img);
            }
        }
Пример #17
0
        public Task <TreatmentModel> Handle(CreateTreatmentCommand request, CancellationToken cancellationToken)
        {
            var requestPayload = JObject.Parse(request.Payload ?? "{}");

            requestPayload.Add(new JProperty("Status", "pending-approval"));

            if (requestPayload.ContainsKey("database"))
            {
                var query = requestPayload.SelectToken("database").SelectToken("quert").Value <string>();
                if (query.Contains("UPDATE") || query.Contains("TRUNCATE") || query.Contains("DELETE") || query.Contains("ALTER") || query.Contains("CREATE") || query.Contains("INSERT") || query.Contains("DROP"))
                {
                    throw new Exception("FORBIDDEN_KEY");
                }
            }

            var treatmentPayload = new TreatmentPayload()
            {
                Payload = JsonConvert.SerializeObject(requestPayload)
            };

            _databaseContext.TreatmentPayload.Add(treatmentPayload);
            _databaseContext.SaveChanges();

            var treatments = _mapper.Map <List <TreatmentModel> >(_databaseContext.Treatment.ToList());

            Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein(request.Description);
            foreach (var treatmentItem in treatments)
            {
                treatmentItem.ClosedDescriptionProbability = lev.DistanceFrom(treatmentItem.Description);
            }

            var treatment = treatments.Where(x => x.ClosedDescriptionProbability <= 7 && x.Type?.ToLower() == request.Type.ToLower()).OrderByDescending(x => x.ClosedDescriptionProbability).FirstOrDefault();

            if (treatment == null)
            {
                treatment = new TreatmentModel()
                {
                    Type = request.Type, Description = request.Description, Name = request.Name
                };
                _databaseContext.Treatment.Add(treatment);
                _databaseContext.SaveChanges();
            }

            var treatmentModel = _mapper.Map <TreatmentModel>(treatment);

            treatmentModel.Payload = treatmentPayload;

            if (request.RiskId.HasValue)
            {
                _databaseContext.Relationship.Add(new Relationship()
                {
                    FromType = ObjectType.Risk,
                    FromId   = request.RiskId.Value,
                    ToType   = ObjectType.TreatmentPayload,
                    ToId     = treatmentPayload.Id
                });
            }

            _databaseContext.Relationship.Add(new Relationship()
            {
                FromType = ObjectType.Treatment,
                FromId   = treatment.Id,
                ToType   = ObjectType.TreatmentPayload,
                ToId     = treatmentPayload.Id
            });
            _databaseContext.SaveChanges();
            return(Task.FromResult(treatmentModel));
        }