c++ - 如何在没有分隔符的大文本文件中查找所有字典单词?

标签 c++ algorithm full-text-search

给定一个大文本文件(大约 500MB 的文本),我必须找出该文件中字典单词的数量。用于检查它是否是单词的词典是用于优化查找的 trie。

对于像“racecourse”这样的小输入,它应该返回 6 个词,因为 {“race”、“course”、“racecourse”、“a”、“our”、“ace”} 都是字典中的词。我目前的方法效率不高:

[删除的代码]

这会遍历字符串并检查每个部分,例如:

r

赛车

种族

赛车

赛科

赛马会

赛马场

赛马场

马场

在下一次迭代中,它将删除“r”并再次使用字符串“acecourse”重复。我有另一个 trie 来防止重复的字符串被计算在内。对于大文本文件来说,这是相当低效和错误的。有什么建议吗?

最佳答案

是的,你可以更快地做到这一点。假设字典已排序,您可以使用带有开始索引和结束索引的二进制搜索。索引确保您在字典中搜索最少的匹配项。我创建了索引器对象来跟踪和缩小每个搜索结果。当没有可搜索的内容时,它会删除索引器。

代码是 C#:

        public class Indexer
        {
            public int DictStartIndex { get; set; }
            public int DictEndIndex { get; set; }
            public int CharCount { get; set; }
            public int CharPos { get; set; }
        }

        public class WordDictionaryComparer : IComparer<string>
        {
            public int Compare(string x, string y)
            {
                return x.CompareTo(y);
            }
        }


        public static void Main(string[] args)
        {
            List<string> dictionary = new List<string>() { "race", "course", "racecourse", "a", "our", "ace", "butter", "rectangle", "round" };
            dictionary.Sort();

            List<Indexer> indexers = new List<Indexer>();
            WordDictionaryComparer wdc = new WordDictionaryComparer();
            List<string> wordsFound = new List<string>();

            string line = "racecourse";

            for (int i = 0; i < line.Length; i++)
            {
                Indexer newIndexer = new Indexer();
                newIndexer.CharPos = i;
                newIndexer.DictEndIndex = dictionary.Count;
                indexers.Add(newIndexer);
                for (int j = indexers.Count - 1; j >= 0; j--)

                {
                    var oneIndexer = indexers[j];
                    oneIndexer.CharCount++;
                    string lookFor = line.Substring(oneIndexer.CharPos, oneIndexer.CharCount);
                    int index = dictionary.BinarySearch(oneIndexer.DictStartIndex, oneIndexer.DictEndIndex - oneIndexer.DictStartIndex, lookFor, wdc);
                    if (index > -1) wordsFound.Add(lookFor);
                    else index = ~index;
                    oneIndexer.DictStartIndex = index;

                    //GetEndIndex
                    string lookEnd = lookFor.Substring(0, lookFor.Length - 1) + (char)(line[i] + 1);
                    int endIndex = dictionary.BinarySearch(oneIndexer.DictStartIndex, oneIndexer.DictEndIndex - oneIndexer.DictStartIndex, lookEnd, wdc);
                    if (endIndex < 0) endIndex = ~endIndex;
                    oneIndexer.DictEndIndex = endIndex;
                    if (oneIndexer.DictEndIndex == oneIndexer.DictStartIndex) indexers.RemoveAt(j);
                }

            }

        }

关于c++ - 如何在没有分隔符的大文本文件中查找所有字典单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55320949/

相关文章:

c++ - Pugixml:找不到文档元素

c++ - 删除 DirectShow 过滤器(未调用析构函数)

c++ - 尝试使用 Qt 库中的 QPixmap 将图像分成几个 block 。他的复制方法的工作方式有什么我不明白的吗?

algorithm - 在不计算凸包本身的情况下查找点是否在一组点的凸包内

algorithm - 在 p 组 q 成员中分配 n 个对象

mvvm - SwiftUI: View 模型不会更新 View

c++ - 如何使用 Windows 可移植设备 C++ API 获取 MTP 设备公开的文件夹中所有文件(对象)的列表?

c++ - 谁能帮我弄清楚我的着色器哪里出了问题?

c++ - 实现一个接收和处理客户端请求的服务器(cassandra 作为后端),Python 还是 C++?

python - 使用 PostgreSQL 索引的 Django 全文搜索