快速(子)字符串搜索大量数据

时间:2018-08-27 14:16:51

标签: c# algorithm search

给出一个城市:

 // Allow write files to the path "images/*", subject to the constraints:
 // 1) File is less than 5MB
 // 2) Content type is an image
 // 3) Uploaded content type matches existing content type
 // 4) File name (stored in imageId wildcard variable) is less than 32 characters
 match /{imageId} {
   allow write: if request.resource.size < 5 * 1024 * 1024
                && request.resource.contentType.matches('image/.*')
                && request.resource.contentType == resource.contentType
                && imageId.size() < 32

我在文件中列出了将近3,000,000个城市(以及城镇和村庄等)。该文件被读入内存;我一直在玩数组,列表,字典(键= {public class City { public int Id { get; set; } public string Name { get; set; } public string Country { get; set; } public LatLong Location { get; set; } } )等。

我想尽快找到所有匹配子字符串(不区分大小写)的城市。因此,当我搜索“ yor”时,我希望尽快获得所有匹配项(超过1000次)(匹配“ Yor k Town”,“ Villa Ma yor ”,“ New < strong>您 k',...)。

在功能上,您可以这样写:

Id

读取文件时,我不介意进行一些预处理;事实上:这就是我最想要的。读取文件,在数据上“咀嚼”,创建某种索引或...,然后准备回答诸如“ yor”之类的查询。

我希望它是独立的,独立的。我不想添加像RDBMS,ElasticSearch之类的依赖项。我不介意在内存中多次(部分)列表。我不介意在数据结构上花费一些内存来帮助我快速找到结果。我不要库或包。我想要一种可以自己实现的算法。

基本上我想要上面的LINQ语句,但是针对我的情况进行了优化;目前浏览大约3,000,000条记录大约需要+/- 2秒。我希望此时间少于0.1秒,因此我可以使用搜索并将其结果显示为“自动完成”。

创建“索引”(类似)结构可能是我所需要的。在撰写本文时,我还记得有关“ bloom过滤器”的内容,但不确定是否有助于甚至支持子字符串搜索。现在将对此进行调查。

非常感谢任何提示,指针,帮助您。

3 个答案:

答案 0 :(得分:1)

您可以使用后缀树:https://en.wikipedia.org/wiki/Suffix_tree

它需要足够的空间才能在内存中存储大约20倍的单词列表

后缀数组是节省空间的替代方法:https://en.wikipedia.org/wiki/Suffix_array

答案 1 :(得分:1)

我创建了一些基于后缀数组/字典的混合体。感谢saibot首先提出建议,并感谢其他所有人的帮助和建议。

这是我想出的:

public class CitiesCollection
{
    private Dictionary<int, City> _cities;
    private SuffixDict<int> _suffixdict;

    public CitiesCollection(IEnumerable<City> cities, int minLen)
    {
        _cities = cities.ToDictionary(c => c.Id);
        _suffixdict = new SuffixDict<int>(minLen, _cities.Values.Count);
        foreach (var c in _cities.Values)
            _suffixdict.Add(c.Name, c.Id);
    }

    public IEnumerable<City> Find(string find)
    {
        var normalizedFind = _suffixdict.NormalizeString(find);
        foreach (var id in _suffixdict.Get(normalizedFind).Where(v => _cities[v].Name.IndexOf(normalizedFind, StringComparison.OrdinalIgnoreCase) >= 0))
            yield return _cities[id];
    }
}


public class SuffixDict<T>
{
    private readonly int _suffixsize;
    private ConcurrentDictionary<string, IList<T>> _dict;

    public SuffixDict(int suffixSize, int capacity)
    {
        _suffixsize = suffixSize;
        _dict = new ConcurrentDictionary<string, IList<T>>(Environment.ProcessorCount, capacity);
    }

    public void Add(string suffix, T value)
    {
        foreach (var s in GetSuffixes(suffix))
            AddDict(s, value);
    }

    public IEnumerable<T> Get(string suffix)
    {
        return Find(suffix).Distinct();
    }

    private IEnumerable<T> Find(string suffix)
    {
        foreach (var s in GetSuffixes(suffix))
        {
            if (_dict.TryGetValue(s, out var result))
                foreach (var i in result)
                    yield return i;
        }
    }

    public string NormalizeString(string value)
    {
        return value.Normalize().ToLowerInvariant();
    }

    private void AddDict(string suffix, T value)
    {
        _dict.AddOrUpdate(suffix, (s) => new List<T>() { value }, (k, v) => { v.Add(value); return v; });
    }

    private IEnumerable<string> GetSuffixes(string value)
    {
        var nv = NormalizeString(value);
        for (var i = 0; i <= nv.Length - _suffixsize ; i++)
            yield return nv.Substring(i, _suffixsize);
    }
}

用法(我假设mycitiesIEnumerable<City>,并带有问题中给定的City对象)

var cc = new CitiesCollection(mycities, 3);
var results = cc.Find("york");

一些结果:

Find: sterda elapsed: 00:00:00.0220522 results: 32
Find: york   elapsed: 00:00:00.0006212 results: 155
Find: dorf   elapsed: 00:00:00.0086439 results: 6095

内存使用非常非常可以接受。只有650MB的内存可存储300万个城市的整个集合。

在上面的代码中,我将ID存储在“ SuffixDict”中,并且具有一定程度的间接性(字典查找以查找id => city)。可以进一步简化为:

public class CitiesCollection
{
    private SuffixDict<City> _suffixdict;

    public CitiesCollection(IEnumerable<City> cities, int minLen, int capacity = 1000)
    {
        _suffixdict = new SuffixDict<City>(minLen, capacity);
        foreach (var c in cities)
            _suffixdict.Add(c.Name, c);
    }

    public IEnumerable<City> Find(string find, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
    {
        var normalizedFind = SuffixDict<City>.NormalizeString(find);
        var x = _suffixdict.Find(normalizedFind).ToArray();
        foreach (var city in _suffixdict.Find(normalizedFind).Where(v => v.Name.IndexOf(normalizedFind, stringComparison) >= 0))
            yield return city;
    }
}

public class SuffixDict<T>
{
    private readonly int _suffixsize;
    private ConcurrentDictionary<string, IList<T>> _dict;

    public SuffixDict(int suffixSize, int capacity = 1000)
    {
        _suffixsize = suffixSize;
        _dict = new ConcurrentDictionary<string, IList<T>>(Environment.ProcessorCount, capacity);
    }

    public void Add(string suffix, T value)
    {
        foreach (var s in GetSuffixes(suffix, _suffixsize))
            AddDict(s, value);
    }

    public IEnumerable<T> Find(string suffix)
    {
        var normalizedfind = NormalizeString(suffix);
        var find = normalizedfind.Substring(0, Math.Min(normalizedfind.Length, _suffixsize));

        if (_dict.TryGetValue(find, out var result))
            foreach (var i in result)
                yield return i;
    }

    private void AddDict(string suffix, T value)
    {
        _dict.AddOrUpdate(suffix, (s) => new List<T>() { value }, (k, v) => { v.Add(value); return v; });
    }

    public static string NormalizeString(string value)
    {
        return value.Normalize().ToLowerInvariant();
    }

    private static IEnumerable<string> GetSuffixes(string value, int suffixSize)
    {
        var nv = NormalizeString(value);
        if (value.Length < suffixSize)
        {
            yield return nv;
        }
        else
        {
            for (var i = 0; i <= nv.Length - suffixSize; i++)
                yield return nv.Substring(i, suffixSize);
        }
    }
}

这将使加载时间从00:00:16.3899085增加到00:00:25.6113214,内存使用量从650MB减少到486MB。由于我们的间接访问级别较低,因此查找/搜索的性能要好一些。

Find: sterda elapsed: 00:00:00.0168616 results: 32
Find: york elapsed: 00:00:00.0003945 results: 155
Find: dorf elapsed: 00:00:00.0062015 results: 6095

到目前为止,我对结果感到满意。我会做一些修饰和重构,然后称之为一天!谢谢大家的帮助!

这就是它在2,972,036个城市中的表现:

Result

通过修改以下代码,它已演变为不区分大小写,不区分重音的搜索:

public static class ExtensionMethods
{
    public static T FirstOrDefault<T>(this IEnumerable<T> src, Func<T, bool> testFn, T defval)
    {
        return src.Where(aT => testFn(aT)).DefaultIfEmpty(defval).First();
    }

    public static int IndexOf(this string source, string match, IEqualityComparer<string> sc)
    {
        return Enumerable.Range(0, source.Length) // for each position in the string
                         .FirstOrDefault(i => // find the first position where either
                                              // match is Equals at this position for length of match (or to end of string) or
                             sc.Equals(source.Substring(i, Math.Min(match.Length, source.Length - i)), match) ||
                             // match is Equals to on of the substrings beginning at this position
                             Enumerable.Range(1, source.Length - i - 1).Any(ml => sc.Equals(source.Substring(i, ml), match)),
                             -1 // else return -1 if no position matches
                          );
    }
}

public class CaseAccentInsensitiveEqualityComparer : IEqualityComparer<string>
{
    private static readonly CompareOptions _compareoptions = CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth | CompareOptions.IgnoreSymbols;
    private static readonly CultureInfo _cultureinfo = CultureInfo.InvariantCulture;
    public bool Equals(string x, string y)
    {
        return string.Compare(x, y, _cultureinfo, _compareoptions) == 0;
    }

    public int GetHashCode(string obj)
    {
        return obj != null ? RemoveDiacritics(obj).ToUpperInvariant().GetHashCode() : 0;
    }

    private string RemoveDiacritics(string text)
    {
        return string.Concat(
            text.Normalize(NormalizationForm.FormD)
            .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)
        ).Normalize(NormalizationForm.FormC);
    }
}

public class CitiesCollection
{
    private SuffixDict<City> _suffixdict;
    private HashSet<string> _countries;
    private Dictionary<int, City> _cities;
    private readonly IEqualityComparer<string> _comparer = new CaseAccentInsensitiveEqualityComparer();

    public CitiesCollection(IEnumerable<City> cities, int minLen, int capacity = 1000)
    {
        _suffixdict = new SuffixDict<City>(minLen, _comparer, capacity);
        _countries = new HashSet<string>();
        _cities = new Dictionary<int, City>(capacity);
        foreach (var c in cities)
        {
            _suffixdict.Add(c.Name, c);
            _countries.Add(c.Country);
            _cities.Add(c.Id, c);
        }
    }

    public City this[int index] => _cities[index];

    public IEnumerable<string> Countries => _countries;

    public IEnumerable<City> Find(string find, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
    {
        foreach (var city in _suffixdict.Find(find).Where(v => v.Name.IndexOf(find, _comparer) >= 0))
            yield return city;
    }
}

public class SuffixDict<T>
{
    private readonly int _suffixsize;
    private ConcurrentDictionary<string, IList<T>> _dict;

    public SuffixDict(int suffixSize, IEqualityComparer<string> stringComparer, int capacity = 1000)
    {
        _suffixsize = suffixSize;
        _dict = new ConcurrentDictionary<string, IList<T>>(Environment.ProcessorCount, capacity, stringComparer);
    }

    public void Add(string suffix, T value)
    {
        foreach (var s in GetSuffixes(suffix, _suffixsize))
            AddDict(s, value);
    }

    public IEnumerable<T> Find(string suffix)
    {
        var find = suffix.Substring(0, Math.Min(suffix.Length, _suffixsize));

        if (_dict.TryGetValue(find, out var result))
        {
            foreach (var i in result)
                yield return i;
        }
    }

    private void AddDict(string suffix, T value)
    {
        _dict.AddOrUpdate(suffix, (s) => new List<T>() { value }, (k, v) => { v.Add(value); return v; });
    }

    private static IEnumerable<string> GetSuffixes(string value, int suffixSize)
    {
        if (value.Length < 2)
        {
            yield return value;
        }
        else
        {
            for (var i = 0; i <= value.Length - suffixSize; i++)
                yield return value.Substring(i, suffixSize);
        }
    }
}

还要感谢NetmageMitsugui。仍然存在一些问题/边缘情况,但是它还在不断改善!

答案 2 :(得分:-5)

查询基准中的

包含的索引要比indexOf> 0快得多

cities.Values.Where(c => c.Name.Contans("yor"))