C
C#3y ago
Hercules

Why am i not getting all the words? After fetching them with HttpClient and try to loop them through

I have a text and the word "the" occurs 11 557 time but when i run my code i only get 6414 times.
using HttpClient client = new();

var rawSource = await ProcessRepositoriesAsync(client);
static async Task<string> ProcessRepositoriesAsync(HttpClient client)
{
var source = await client.GetStringAsync(
"https://www.gutenberg.org/files/45839/45839.txt");
return source;
}

var dictionary = new Dictionary<string, long>();

string[] arr = rawSource.Split(' ');

foreach (var word in arr)
{
if (word.Length >= 3) // at least 3 letters
{
if (dictionary.ContainsKey(word)) //if it's in the dictionary
dictionary[word] = dictionary[word] + 1; //Increment the count
else
dictionary[word] = 1; //put it in the dictionary with a count 1
}
}

var frequentWord =
dictionary.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;
var frequentCount =
dictionary.Aggregate((x, y) => x.Value > y.Value ? x : y).Value;

Console.WriteLine("FrequentCount: " + frequentCount);
Console.WriteLine("Word Frequently used: " + frequentWord);
using HttpClient client = new();

var rawSource = await ProcessRepositoriesAsync(client);
static async Task<string> ProcessRepositoriesAsync(HttpClient client)
{
var source = await client.GetStringAsync(
"https://www.gutenberg.org/files/45839/45839.txt");
return source;
}

var dictionary = new Dictionary<string, long>();

string[] arr = rawSource.Split(' ');

foreach (var word in arr)
{
if (word.Length >= 3) // at least 3 letters
{
if (dictionary.ContainsKey(word)) //if it's in the dictionary
dictionary[word] = dictionary[word] + 1; //Increment the count
else
dictionary[word] = 1; //put it in the dictionary with a count 1
}
}

var frequentWord =
dictionary.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;
var frequentCount =
dictionary.Aggregate((x, y) => x.Value > y.Value ? x : y).Value;

Console.WriteLine("FrequentCount: " + frequentCount);
Console.WriteLine("Word Frequently used: " + frequentWord);
Is there anything wrong on how i'm looping the words in my text?
9 Replies
Saber
Saber3y ago
does it occur 11557 times, or does it occur 11557 times with 5000 of them being in the middle of other words
Hercules
HerculesOP3y ago
@Deluxe No you didn't I did a search again " the " and i got 6806 hits. I'm still missing around 400 hits
Saber
Saber3y ago
Theres a difference between The and the
Hercules
HerculesOP3y ago
This is also correct! I have to make sure to add the value with Pairs of ' the ' and ' The ' Im trying this
var dictionary = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
var dictionary = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
hope it works
MODiX
MODiX3y ago
Toshiya Joshima#3472
REPL Result: Failure
Dictionary<string, int> dictionary = new()
{
{ "life", 42 }
};

Console.WriteLine(dictionary["Life"]);
Dictionary<string, int> dictionary = new()
{
{ "life", 42 }
};

Console.WriteLine(dictionary["Life"]);
Exception: KeyNotFoundException
- The given key 'Life' was not present in the dictionary.
- The given key 'Life' was not present in the dictionary.
Compile: 602.803ms | Execution: 34.282ms | React with ❌ to remove this embed.
Servator
Servator3y ago
this doesnt work while with comparer like @Hercules said it works
MODiX
MODiX3y ago
Toshiya Joshima#3472
REPL Result: Success
Dictionary<string, int> dictionary = new(StringComparer.OrdinalIgnoreCase)
{
{ "life", 42 }
};

Console.WriteLine(dictionary["Life"]);
Dictionary<string, int> dictionary = new(StringComparer.OrdinalIgnoreCase)
{
{ "life", 42 }
};

Console.WriteLine(dictionary["Life"]);
Console Output
42
42
Compile: 594.756ms | Execution: 35.031ms | React with ❌ to remove this embed.
Hercules
HerculesOP3y ago
work while with comparer?
Servator
Servator3y ago
It works with comparer comparer makes ignore cases

Did you find this page helpful?