如何导入字典文本文件并检查单词匹配



i生成一个500个字符的随机字符串,并想检查单词。

bliduuwfhbgphwhsyzjnlfyizbjfeeepsbpgplpbhaegyepqcjhhotovnzdtlracxrwggbcmjiglasjvmscvxwazmutqiwppzcjhijjbguxfnduuphhsoffaqwtmhmensqmyicnciaoczumjzyaaowbtwjqlpxuuqknxqvmnueknqcbvkkmildyvosczlbnlgumohosemnfkmndtiubfkminlriytmbtrzhwqmovrivxxojbpirqahatmydqgulammsnfgcvgfncqkpxhgikulsjynjrjypxwvlkvwvigvjvuydbjfizmbfbtjprxkmiqpfuyebllzezbxozkiidpplvqkqlgdlvjbfeticedwomxgawuphocisaejeonqehoipzsjgbfdatbzykkurrwwtajeajeornrhyoqadljfjyizzfluetynlrpoqojxxqmmbuaktjqghqmusjfvxkkyoewgyckpbmismwyfebaucsfueuwgio

我导入一个字典单词txt文件,然后检查 string以查看它是否包含每个单词。如果找到匹配,则将其添加到列表中。


我使用 Dictionary<>读取的速度比 Array的单词列表快。

当我使用该方法时,我可以看到CPU在调试器中使用foreach循环,并且我的循环计数器会在10秒内大约10,000次以上,但是循环持续下去,并且不会返回任何结果。

当我将Array用于字典时,该程序可行,但在10秒内以500次速度慢。


不工作

使用Dictionary<>

// Random Message
public string message = Random(500);
// Dictionary Words Reference
public Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Matches Found
public static List<string> matches = new List<string>();

public MainWindow()
{
    InitializeComponent();
    // Import Dictionary File
    dictionary = File
                    .ReadLines(@"C:dictionary.txt")
                    .Select((v, i) => new { Index = i, Value = v })
                    .GroupBy(p => p.Index / 2)
                    .ToDictionary(g => g.First().Value, g => g.Last().Value);

    // If Message Contains word, add to Matches List
    foreach (KeyValuePair<string, string> entry in dictionary)
    {
        if (message.Contains(entry.Value))
        {
            matches.Add(entry.Value);
        }
    }
}

工作

使用Array

// Random Message
public string message = Random(500);
// Dictionary Words Reference
public string[] dictionary = File.ReadAllLines(@"C:dictionary.txt");
// Matches Found
public List<string> matches = new List<string>();

public MainWindow()
{
    InitializeComponent();
    // If Message Contains word, add to Matches List
    foreach (var entry in dictionary)
    {
        if (message.Contains(entry))
        {
            matches.Add(entry);
        }
    }
}

我怀疑您是否要Dictionary<string, string>作为 dictionary ;)HashSet<string>会足够:

  using System.Linq;
  ...
  string source = "bliduuwfhbgphwhsyzjnlfyizbj";
  HashSet<string> allWords = new HashSet<string>(File
    .ReadLines(@"C:dictionary.txt")
    .Select(line => line.Trim())
    .Where(line => !string.IsNullOrEmpty(line)), StringComparer.OrdinalIgnoreCase);
  int shortestWord = allWords.Min(word => word.Length);
  int longestWord = allWords.Max(word => word.Length);
  // If you want duplicates, change HashSet<string> to List<string>
  HashSet<string> wordsFound = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  for (int length = shortestWord; length <= longestWord; ++length) {
    for (int position = 0; position <= source.Length - length; ++position) {
      string extract = source.Substring(position, length);
      if (allWords.Contains(extract))
        wordsFound.Add(extract); 
    }
  }

测试:

https://raw.githubusercontent.com/dolph/dictionary/master/popular.txt

字典donwload作为 C:dictionary.txt文件

  Console.WriteLine(string.Join(", ", wordsFound.OrderBy(x => x)));      

我们有输出

  id, li, lid

在这种情况下使用字典使用字典是没有意义的。本质上,字典是存储变量名称和变量值的变量列表。

我可以有以下内容:

int age = 21;
int money = 21343;
int distance = 10;
int year = 2017;

并使用以下内容将其转换为字典:

Dictionary<string, int> numbers = new Dictionary<string, int>()
{
    { "age", 21 },
    { "money", 21343},
    { "distance", 10 },
    { "year", 2017 }
};

,然后我可以使用其键(第一个值)在字典中访问一个值。因此,例如,如果我想知道什么是"年龄",我会使用:

Console.Log(numbers["age"]);

这只是词典的力量的一个例子 - 他们可以做的更多,它们可以使您的生活更加轻松。但是,在这种情况下,他们不会做您期望的事情。我建议只使用数组或列表。

您正在滥用字典,您基本上将字典用作列表,因此它仅在程序中添加了一些开销。没有任何帮助。

如果您想对字典进行查询,而不是相反,那将是有用的。

在任何情况下,您想要的是标签,而不是词典,因为您的词典中的键不是您要查询的单词,而是无关紧要的int。

您可以在此处阅读有关字典和标签的更多信息:

字典: https://www.dotnetperls.com/dictionary
hashset: https://www.dotnetperls.com/hashset

最新更新