gpt4 book ai didi

C# 字典允许看似相同的键

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:39 25 4
gpt4 key购买 nike

我创建了一个字典,并创建了代码来读取一个 txt 文件,并将文件中的每个单词输入到字典中。

        //Set up OpenFileDialog box, and prompt user to select file to open
DialogResult openFileResult;
OpenFileDialog file = new OpenFileDialog() ;
file.Filter = "txt files (*.txt)|*.txt";
openFileResult = file.ShowDialog();

if (openFileResult == DialogResult.OK)
{
//If user selected file successfully opened

//Reset form
this.Controls.Clear();
this.InitializeComponent();

//Read from file, split into array of words
Stream fs = file.OpenFile();
StreamReader reader;
reader = new StreamReader(fs);
string line = reader.ReadToEnd();
string[] words = line.Split(' ', '\n');

//Add each word and frequency to dictionary
foreach (string s in words)
{
AddToDictionary(s);
}

//Reset variables, and set-up chart
ResetVariables();
ChartInitialize();

foreach (string s in wordDictionary.Keys)
{
//Calculate statistics from dictionary
ComputeStatistics(s);

if (dpCount < 50)
{
AddToGraph(s);
}
}

//Print statistics
PrintStatistics();
}

AddToDictionary(s) 函数是:

public void AddToDictionary(string s)
{
//Function to add string to dictionary
string wordLower = s.ToLower();
if (wordDictionary.ContainsKey(wordLower))
{
int wordCount = wordDictionary[wordLower];
wordDictionary[wordLower] = wordDictionary[wordLower] + 1;
}
else
{
wordDictionary.Add(wordLower, 1);
txtUnique.Text += wordLower + ", ";
}
}

这个程序正在读取的文本文件是:

To be or not to be that is the question
Whether tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them To die to sleep
No more and by a sleep to say we end
The heartache and the thousand natural shocks
That flesh is heir to Tis a consummation
Devoutly to be wished To die to sleep
To sleep perchance to dream ay theres the rub
For in that sleep of death what dreams may come
When we **have** shuffled off this mortal coil
Must give us pause Theres the respect
That makes calamity of so long life
For who would bear the whips and scorns of time
The oppressors wrong the proud mans contumely
The pangs of despised love the laws delay
The insolence of office and the spurns
That patient merit of th unworthy takes
When he himself might his quietus make
With a bare bodkin Who would fardels bear
To grunt and sweat under a weary life
But that the dread of something after death
The undiscovered country from whose bourn
No traveller returns puzzles the will
And makes us rather bear those ills we **have**
Than fly to others that we know not of
Thus conscience does make cowards of us all
And thus the native hue of resolution
Is sicklied oer with the pale cast of thought
And enterprise of great pitch and moment
With this regard their currents turn awry
And lose the name of action Soft you now
The fair Ophelia Nymph in thy orisons
Be all my sins remembered

我遇到的问题是“有”这个词在字典中出现了两次。我知道这不会发生在字典中,但出于某种原因它出现了两次。有谁知道为什么会这样?

最佳答案

如果你运行:

var sb = new StringBuilder();
sb.AppendLine("test which");
sb.AppendLine("is a test");
var words = sb.ToString().Split(' ', '\n').Distinct();

在调试器中检查 words 显示,由于两字节的 CRLF 行终止符,“test”的某些实例已获得 \r - 这未被处理 split 。

要修复,请将拆分更改为:

Split(new[] {" ", Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

关于C# 字典允许看似相同的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114499/

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