gpt4 book ai didi

c# - 如何导入词典文本文件并检查单词匹配?

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:21 27 4
gpt4 key购买 nike

我生成了一个包含 500 个字符的随机字符串,并想检查单词。

bliduuwfhbgphwhsyzjnlfyizbjfeeepsbpgplpbhaegyepqcjhhotovnzdtlracxrwggbcmjiglasjvmscvxwazmutqiwppzcjhijjbguxfnduuphhsoffaqwtmhmensqmyicnciaoczumjzyaaowbtwjqlpxuuqknxqvmnueknqcbvkkmildyvosczlbnlgumohosemnfkmndtiubfkminlriytmbtrzhwqmovrivxxojbpirqahatmydqgulammsnfgcvgfncqkpxhgikulsjynjrjypxwvlkvwvigvjvuydbjfizmbfbtjprxkmiqpfuyebllzezbxozkiidpplvqkqlgdlvjbfeticedwomxgawuphocisaejeonqehoipzsjgbfdatbzykkurrwwtajeajeornrhyoqadljfjyizzfluetynlrpoqojxxqmmbuaktjqghqmusjfvxkkyoewgyckpbmismwyfebaucsfueuwgio

我导入一个 Dictionary Words txt file并检查 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>作为字典 ;) 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

字典下载为C:\dictionary.txt文件

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

我们有输出

  id, li, lid

关于c# - 如何导入词典文本文件并检查单词匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47676690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com