示例#1
0
        public async Task <PlaceTransporter> FindPerfectPlace(IPlacesProvider placesProvider, String[] placesTypes,
                                                              IDistanceResolver distanceResolver, GeoCoordinatesTransporter[] geoCoordinates, GeoCoordinatesTransporter leaderCoordinates)
        {
            var averageCoordinates = this.GetAverageLocation(geoCoordinates);
            var candidatePlaces    = await placesProvider.LookForNearbyPlacesAsync(new PlacesQueryTransporter
            {
                Latitude    = averageCoordinates.Y,
                Longitude   = averageCoordinates.X,
                Radius      = 1000,
                PlacesTypes = placesTypes
            });

            if (candidatePlaces.Count() == 0)
            {
                return(null);
            }
            double           minimumDistance = 0;
            PlaceTransporter finalPlace      = null;

            foreach (var item1 in candidatePlaces)
            {
                double distance = 0;
                distance += CalcDistance(item1.Latitude, item1.Longitude, double.Parse(averageCoordinates.Y.ToString("N4", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture),
                                         double.Parse(averageCoordinates.X.ToString("N4", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture));
                if (finalPlace == null || minimumDistance > distance)
                {
                    minimumDistance = distance;
                    finalPlace      = item1;
                }
            }


            return(finalPlace);
        }
示例#2
0
        public IList <Word> SemanticallyNear(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null)
            {
                distanceResolver = new LevenshteinDistanceResolver(word, maxEdits);
            }

            var words = new List <Word>();

            LcrsNode node;

            var prefix = word[0].ToString();

            if (TryFindDepthFirst(prefix, out node))
            {
                if (node.EndOfWord &&
                    distanceResolver.IsValid(node.Value, 0) &&
                    distanceResolver.GetDistance(word, prefix) <= maxEdits)
                {
                    words.Add(new Word(word[0].ToString()));
                }

                WithinEditDistanceDepthFirst(
                    word, word[0].ToString(), words, 1, maxEdits, distanceResolver);
            }

            return(words);
        }
 public PerfectPlaceController(IDistanceResolver distanceResolver, IPlacesProvider placesProvider, ProgramDbContext dbContext)
 {
     this.DistanceResolver = distanceResolver;
     this.PlacesProvider   = placesProvider;
     this.Algo             = new PerfectPlaceAlgorithm();
     this.DbContext        = dbContext;
 }
示例#4
0
 public Collector(string directory, IxInfo ix, IScoringScheme scorerFactory = null, IDistanceResolver distanceResolver = null, int documentCount = -1)
 {
     _directory        = directory;
     _ix               = ix;
     _scorerFactory    = scorerFactory;
     _distanceResolver = distanceResolver ?? new Levenshtein();
     _documentCount    = documentCount == -1 ? ix.DocumentCount : documentCount;
 }
示例#5
0
 public Collector(string directory, IxInfo ix, IScoringScheme scorerFactory = null, IDistanceResolver distanceResolver = null, int documentCount = -1)
 {
     _directory        = directory;
     _ix               = ix;
     _scorerFactory    = scorerFactory;
     _distanceResolver = distanceResolver ?? new Levenshtein();
     _documentCount    = documentCount == -1 ? ix.DocumentCount : documentCount;
     _scoreCache       = new Dictionary <SubQuery, IList <DocumentScore> >();
 }
示例#6
0
文件: LcrsTrie.cs 项目: mpvyard/resin
        private void WithinEditDistanceDepthFirst(
            string word, string state, List <Word> words, int depth, int maxEdits, IDistanceResolver distanceResolver, bool stop = false)
        {
            var reachedMin   = maxEdits == 0 || depth >= word.Length - 1 - maxEdits;
            var reachedDepth = depth >= word.Length - 1;
            var reachedMax   = depth >= word.Length + maxEdits;

            if (!reachedMax && !stop)
            {
                string test;

                if (depth == state.Length)
                {
                    test = state + Value;
                }
                else
                {
                    test = state.ReplaceOrAppend(depth, Value);
                }

                if (reachedMin)
                {
                    if (distanceResolver.IsValid(Value, depth))
                    {
                        if (EndOfWord)
                        {
                            if (distanceResolver.GetDistance(word, test) <= maxEdits)
                            {
                                words.Add(new Word(test, PostingsAddress));
                            }
                        }
                    }
                    else
                    {
                        stop = true;
                    }
                }
                else
                {
                    distanceResolver.Put(Value, depth);
                }

                // Go left (deep)
                if (LeftChild != null)
                {
                    LeftChild.WithinEditDistanceDepthFirst(word, test, words, depth + 1, maxEdits, distanceResolver, stop);
                }

                // Go right (wide)
                if (RightSibling != null)
                {
                    RightSibling.WithinEditDistanceDepthFirst(word, state, words, depth, maxEdits, distanceResolver);
                }
            }
        }
示例#7
0
文件: LcrsTrie.cs 项目: light88/resin
        public IEnumerable<Word> SemanticallyNear(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null) distanceResolver = new LevenshteinDistanceResolver(word, maxEdits);

             var compressed = new List<Word>();
            if (LeftChild != null)
            {
                LeftChild.WithinEditDistanceDepthFirst(word, string.Empty, compressed, 0, maxEdits, distanceResolver);
            }
            return compressed;
        }
示例#8
0
        public IEnumerable <Word> Near(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null)
            {
                distanceResolver = new Levenshtein();
            }

            var compressed = new List <Word>();

            WithinEditDistanceDepthFirst(word, string.Empty, compressed, 0, maxEdits, distanceResolver);

            return(compressed);
        }
示例#9
0
        public Searcher(string directory, QueryParser parser, IScoringScheme scorerFactory, IDistanceResolver distanceResolver)
        {
            _directory     = directory;
            _parser        = parser;
            _scorerFactory = scorerFactory;

            _ixs = Util.GetIndexFileNamesInChronologicalOrder(directory).Select(IxInfo.Load).ToList();

            _documentCount = Util.GetDocumentCount(_ixs);

            _blockSize = Serializer.SizeOfBlock();

            _distanceResolver = distanceResolver;
        }
示例#10
0
        public IEnumerable <Word> SemanticallyNear(string word, int maxEdits, IDistanceResolver distanceResolver = null)
        {
            if (distanceResolver == null)
            {
                distanceResolver = new LevenshteinDistanceResolver(word, maxEdits);
            }

            var words = new List <Word>();

            while (true)
            {
                LcrsNode node;

                var prefix = word[0].ToString();

                if (TryFindDepthFirst(prefix, out node))
                {
                    if (node.EndOfWord &&
                        distanceResolver.IsValid(node.Value, 0) &&
                        distanceResolver.GetDistance(word, prefix) <= maxEdits)
                    {
                        words.Add(new Word(word[0].ToString()));
                    }

                    WithinEditDistanceDepthFirst(
                        word, word[0].ToString(), words, 1, maxEdits, distanceResolver);
                }

                if (HasMoreSegments())
                {
                    GoToNextSegment();
                }
                else
                {
                    break;
                }
            }

            return(words);
        }
示例#11
0
        private void WithinEditDistanceDepthFirst(
            string word, string state, IList <Word> words, int depth, int maxEdits, IDistanceResolver distanceResolver, bool stop = false)
        {
            var reachedMin = maxEdits == 0 || depth >= word.Length - 1 - maxEdits;
            var reachedMax = depth >= word.Length + maxEdits;

            var node = Step();

            if (node == LcrsNode.MinValue)
            {
                return;
            }
            else if (node.Value == Serializer.SegmentDelimiter)
            {
                return;
            }

            if (reachedMax || stop)
            {
                Skip(node.Weight - 1);
            }
            else
            {
                string test;

                if (depth == state.Length)
                {
                    test = state + node.Value;
                }
                else
                {
                    test = state.ReplaceOrAppend(depth, node.Value);
                }

                if (reachedMin)
                {
                    if (distanceResolver.IsValid(node.Value, depth))
                    {
                        if (node.EndOfWord)
                        {
                            if (distanceResolver.GetDistance(word, test) <= maxEdits)
                            {
                                words.Add(new Word(test, 1, node.PostingsAddress));
                            }
                        }
                    }
                    else
                    {
                        stop = true;
                    }
                }

                // Go left (deep)
                if (node.HaveChild)
                {
                    WithinEditDistanceDepthFirst(word, test, words, depth + 1, maxEdits, distanceResolver, stop);
                }

                // Go right (wide)
                if (node.HaveSibling)
                {
                    WithinEditDistanceDepthFirst(word, state, words, depth, maxEdits, distanceResolver);
                }
            }
        }
示例#12
0
        private void WithinEditDistanceDepthFirst(string word, string state, List <Word> compressed, int depth, int maxEdits, IDistanceResolver distanceResolver)
        {
            string test;

            if (depth == state.Length)
            {
                test = state + Value;
            }
            else
            {
                test = new string(state.ReplaceOrAppend(depth, Value).ToArray());
            }

            var edits = distanceResolver.Distance(word, test);

            if (edits <= maxEdits)
            {
                if (EndOfWord)
                {
                    compressed.Add(new Word(test, WordCount, PostingsAddress, Postings));
                }
            }

            if (edits <= maxEdits || test.Length < word.Length)
            {
                if (LeftChild != null)
                {
                    LeftChild.WithinEditDistanceDepthFirst(word, test, compressed, depth + 1, maxEdits, distanceResolver);
                }

                if (RightSibling != null)
                {
                    RightSibling.WithinEditDistanceDepthFirst(word, test, compressed, depth, maxEdits, distanceResolver);
                }
            }
        }
示例#13
0
        private void WithinEditDistanceDepthFirst(string word, string state, IList <Word> compressed, int depth, int maxErrors, IDistanceResolver distanceResolver, bool stop = false)
        {
            var reachedMin   = maxErrors == 0 || depth >= word.Length - 1 - maxErrors;
            var reachedDepth = depth >= word.Length - 1;
            var reachedMax   = depth >= word.Length + maxErrors;

            var node = Step();

            if (node == LcrsNode.MinValue)
            {
                return;
            }

            if (reachedMax || stop)
            {
                Skip(node.Weight - 1);
            }
            else
            {
                string test;

                if (depth == state.Length)
                {
                    test = state + node.Value;
                }
                else
                {
                    test = new string(state.ReplaceOrAppend(depth, node.Value).ToArray());
                }

                if (reachedMin)
                {
                    var edits = distanceResolver.Distance(word, test);

                    if (edits <= maxErrors)
                    {
                        if (node.EndOfWord)
                        {
                            compressed.Add(new Word(test, 1, node.PostingsAddress));
                        }
                    }
                    else if (edits > maxErrors && reachedDepth)
                    {
                        stop = true;
                    }
                    else if (reachedDepth)
                    {
                        stop = true;
                    }
                }

                // Go left (deep)
                if (node.HaveChild)
                {
                    WithinEditDistanceDepthFirst(word, test, compressed, depth + 1, maxErrors, distanceResolver, stop);
                }

                // Go right (wide)
                if (node.HaveSibling)
                {
                    WithinEditDistanceDepthFirst(word, state, compressed, depth, maxErrors, distanceResolver);
                }
            }
        }