Removing stop words using application form c# [duplicate] - c#

This question already has answers here:
How to make IEnumerable<string>.Contains case-insensitive?
(3 answers)
Closed 2 years ago.
I have wrote this code to add text from one richbox to another without the stop words but it is copying all the text from richtext1
string[] Stopwords = { "a", "about", "actually", "after", "also", "am", "an", "and",
"any", "are", "as", "at", "be", "because", "but", "by", "could", "do", "each", "either",
"en", "for", "from", "has", "have", "how","i", "if", "in", "is", "it", "its", "just",
"of", "or", "so", "some", "such", "that", "the", "their", "these", "thing", "this", "to",
"too", "very", "was", "we", "well", "what", "when", "where", "who", "will", "with",
"you", "your"
};
This the code in the button that should do it
private void button2_Click(object sender, EventArgs e)
{
string st = richTextBox1.Text;
string[] split = st.Split(' ');
richTextBox2.Text = "";
int c = 0;
foreach (string s in split)
{
if (!Stopwords.Contains(s))
{
richTextBox2.Text += s + " ";
}
else c++;
}
}
when I write if (Stopwords.Contains(s)) it prints all stop words in richtext1
here it shows the input and the output are the same

I would not recommend writing directly to the txt. Have you considered the following:
string st = richTextBox1.Text;
string[] split = st.Split(' ');
StringBuilder sb = new StringBuilder();
int c = 0;
foreach (string s in split)
{
if (!Stopwords.Contains(s))
sb.Append($"{s} ");
else
c++;
}
richTextBox2.Text = sb.ToString().Trim();

A simple version of code to do that, using LinQ
private void button2_Click(object sender, EventArgs e)
{
string st = richTextBox1.Text;
string[] split = st.Split(' ');
richTextBox2.Text = string.Join(' ', split.Where(x => !Stopwords.Contains(x.ToLower())).ToArray());
}

Related

How do i loop through the string and find out how many words are there?

I need help with my problem:
Right now I have a WPF application with a form that includes a button and a textbox.
Also, I have an open file directory - that opens .cs and .txt files.
I need to loop through the string of those files and display the most common words in them, starting from the largest to the smallest.
For example, a string would be:
"The sun is bright. The sun is yellow".
Would return:
The = 2;
sun = 2;
is = 2;
bright = 1;
yellow = 1;
My code right as of now:
private void btn1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false };
if (ofd.ShowDialog() == true)
rtb.Text = File.ReadAllText(ofd.FileName);
string[] userText = rtb.Text.ToLower().Split(new char[] { ' ', ',', '=', '+', '}', '{', '\r', '\n', '(', ')', ';' }, StringSplitOptions.RemoveEmptyEntries);
var frequencies = new Dictionary<string, int>();
foreach (string word in userText) //search words in our array userText that we declared at the beginning.
{
}
}
I am not sure how to continue from here... Help is appreciated.
Using the example and expected output that you have provided for us, I was able to accomplish this by using .GroupBy along with creating an anonymous class.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// example string
var myString = "The sun is bright. The sun is yellow";
// split the string into an array based on space characters and the period
var stringArray = myString.Split(new char[] {' ', '.'}, StringSplitOptions.RemoveEmptyEntries);
// group the items in the array based on the value, then create an anonymous class with the properties `Word` and `Count`
var groupedWords = stringArray.GroupBy(x => x).Select(x => new { Word = x.Key, Count = x.Count() }).ToList();
// print the properties based on order of largest to smallest count to screen
foreach(var item in groupedWords.OrderByDescending(x => x.Count))
{
Console.WriteLine(item.Word + " = " + item.Count);
}
// Output
---------
// The = 2
// sun = 2
// is = 2
// bright = 1
// yellow = 1
}
}
Let me know if this helps!
I would go with #GTown-Coder 's approach as the easiest. But if you really just want to know how to implement the same code using a dictionary as in your sample...
private void btn1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false };
if (ofd.ShowDialog() == true)
rtb.Text = File.ReadAllText(ofd.FileName);
string[] userText = rtb.Text.ToLower().Split(new char[] { ' ', ',', '=', '+', '}', '{', '\r', '\n', '(', ')', ';' }, StringSplitOptions.RemoveEmptyEntries);
var frequencies = new Dictionary<string, int>();
foreach (string word in userText) //search words in our array userText that we declared at the beginning.
{
// Sample here
if (frequencies.Contains(word))
{
frequencies[word]++;
}
else
{
frequencies.Add(word,1);
}
}
foreach (var kvp in frequencies)
Console.WriteLine("Word: {0} \t Frequency: {1}",kvp.Key, kvp.Value);
}
This almost sounds like the original definition of a dictionary, so that might be a good place to start:
IDictionary<string, int> actualDictionary = new Dictionary<string, int>();
You can place words in the dictionary, and increment their count each time you find them.
IDictionary<string, int> actualDictionary = new Dictionary<string, int>();
foreach (string word in userText) //search words in our array userText that we declared at the beginning.
{
if (!actualDictionary.ContainsKey(word)) {
actualDictionary[word] = 0;
}
actualDictionary[word]++;
}
foreach(var thing in actualDictionary) {
Console.WriteLine(thing.Key + " " + thing.Value);
}
See a running example on .NET Fiddle.

Index was outside the bounds of the array c#, morse converter

I was trying to build a Morse converter, the following is a part of my code of c#, but when I run it it tells me index out of the bounds, could anyone fix it? I'm new for programming:)
private void BTNconvert_Click(object sender, EventArgs e)
{
string input = TBinput.Text;
string[] output=new string[input.Length];
for (int index=0;index<input.Length;index++)
{
index=input.IndexOf('a',index);
output[index]=".-";
}
for (int index = 0; index < input.Length; index++)
{
index = input.IndexOf('b', index);
output[index] = "-...";
}
LBcodes.Text = string.Join(" ",output);
The reason you're seeing this error is that IndexOf() will return a -1 value if the given search term is not found in the string, so when you try to set output[-1] you end up with an invalid index.
private void BTNconvert_Click(object sender, EventArgs e)
{
int index;
string input = TBinput.Text;
string[] output=new string[input.Length];
index = -1;
do{
index=input.IndexOf('a',index+1);
if(index==-1)break;
output[index]=".-";
}while(true);
index = -1;
do{
index=input.IndexOf('b',index+1);
if(index==-1)break;
output[index]="-...";
}while(true);
}
LBcodes.Text = string.Join(" ",output);
also if you are going to make these loops for all of the characters I would recommand you to do this:
private void BTNconvert_Click(object sender, EventArgs e)
{
int index;
char[] source1 = {'a','b','c',....,'z'} //replace ... with characters
string[] source2 = {".-","-...",....} //replace .... with Morse equivalents
string input = TBinput.Text;
string[] output=new string[input.Length];
for(int i=0;i<26;i++){
index = -1;
do{
index=input.IndexOf(source1[i],index+1);
if(index==-1)break;
output[index]=source2[i];
}while(true);
}
}
LBcodes.Text = string.Join(" ",output);
IndexOf() will return -1 if specified text not found in the input string. Your code will work fine only if you entering specified character in the text box. That's why your code works fine when you removed the second loop and enter all a's to text box.
In addition to that why you need to use 2 loops. You are doing the same thing in both loops.
If it were me, I would keep a Dictionary mapping of each character and it's corresponding morse string. This will make it easy to convert back and forth.
For example:
private static Dictionary<char, string> MorseMap = new Dictionary<char, string>
{
{'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."},
{'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."},
{'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."},
{'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."},
{'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"},
{'Y', "-.--"}, {'Z', "--.."},{'1', ".----"}, {'2', "..---"},
{'3', "...--"}, {'4', "....-"},{'5', "....."}, {'6', "-...."},
{'7', "--..."}, {'8', "---.."},{'9', "----."}, {'0', "-----"},
{'.', ".-.-.-"}, {',', "--..--"},{'?', "..--.."}, {'\'', ".----."},
{'!', "-.-.--"}, {'/', "-..-."},{'(', "-.--."}, {')', "-.--.-"},
{'&', ".-..."}, {':', "---..."},{';', "-.-.-."}, {'=', "-...-"},
{'+', ".-.-."}, {'-', "-....-"},{'_', "..--.-"}, {'"', ".-..-."},
{'$', "...-..-"}, {'#', ".--.-."}
};
Now, using the keys and values from this map, it's fairly easy to encode and decode to morse:
private static string ConvertToMorse(string input)
{
var morse = new StringBuilder();
foreach (var character in input)
{
var upperCaseChar = char.ToUpper(character);
if (MorseMap.ContainsKey(upperCaseChar))
{
morse.Append(MorseMap[upperCaseChar]);
}
else
{
// If there's no mapping for this character, just add it
morse.Append(character);
}
// Add a space between each morse string.
morse.Append(' ');
}
return morse.ToString().Trim();
}
private static string ConvertToAlpha(string morse)
{
var alpha = new StringBuilder();
// Split words on double-spaces so we can add single spaces back where needed
var morseCodeWords = morse.Split(new[] {" "}, StringSplitOptions.None);
foreach (var morseCodeWord in morseCodeWords)
{
var morseCodeStrings = morseCodeWord.Split(' ');
foreach (var morseCodeString in morseCodeStrings)
{
if (MorseMap.ContainsValue(morseCodeString))
{
alpha.Append(MorseMap.First(item => item.Value == morseCodeString).Key);
}
else
{
// If there's no mapping for the string, just add it
alpha.Append(morseCodeString);
}
}
// Add a space between each word
alpha.Append(' ');
}
return alpha.ToString();
}
Usage Example:
private static void Main()
{
var test = "This is my test string.";
var morseVersion = ConvertToMorse(test);
var alphaVersion = ConvertToAlpha(morseVersion);
Console.WriteLine("Original string ... {0}", test);
Console.WriteLine("Morse version ..... {0}", morseVersion);
Console.WriteLine("Alpha version ..... {0}", alphaVersion);
}

How do I count vowels in a string C# [duplicate]

This question already has answers here:
C# Count Vowels
(20 answers)
Closed 8 years ago.
I am working on a program that is designed to count the vowels in a word, however I am having trouble counting the vowels per word. My current code looks like this:
string word;
string[] ca = { "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" };
int va = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (ca.Contains(word))
{
label1.Text = "Vowel Count: " + va;
}
else
{
label1.Text = "Vowel Count: " + va;
}
}
Thanks for the help!
The easiest way is to split the string into words to start with. This can be achieved with the help of the string Split() method:
// you need to decide what the word separators are:
var words = text.Split(new char[]{'.', ',', ':', ';', '\r', '\t', '\n'});
Once done it's just a for loop:
foreach (var word in words)
{
foreach (var character in word)
{
if (vowels.Any(x => x == character))
++count;
}
}
You could do it like:
string word = "myword";
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
int vowelCount = word.Count(x => vowels.Contains(Char.ToLower(x)));

Split a string base on multiple delimiters specified by user

Updated: Thank you for the answer, but I disagree that my question is answered by another thread. "Multiple delimiters" and "Multi-Character delimiters" are 2 different questions.
This is my code so far:
List<string> delimiters = new List<string>();
List<string> data = new List<string>
{
"Car|cBlue,Mazda~Model|m3",
//More data
};
string userInput = "";
int i = 1;
//The user can enter a maximum of 5 delimiters
while (userInput != "go" && i <= 5)
{
userInput = Console.ReadLine();
delimiters.Add(userInput);
i++;
}
foreach (string delimiter in delimiters)
{
foreach (string s in data)
{
//This split is not working
//string output[] = s.Split(delimiter);
}
}
So, if the user enters "|c" and "~", the expected output is: "Car", "Blue,Mazda", "Model|m3"
If the user enters "|c", "|m", and ",", then the expected output will be: "Car", "Blue", "Mazda~Model", "3"
Add the user input into the List delimiters.
string data = "Car|cBlue,Mazda~Model|m3";
List<string> delimiters = new List<string>();
delimiters.Add("|c");//Change this to user input
delimiters.Add("|m");//change this to user input
string[] parts = data.Split(delimiters.ToArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string item in parts)
{
Console.WriteLine(item);
}
String.Split has an overload that does exactly that - you just need to convert your List<string> to a string[] :
string input = "Car|cBlue,Mazda~Model|m3";
List<string> delims = new List<string> {"|c", "~"};
string[] out1 = input.Split(delims.ToArray(),StringSplitOptions.None);
//output:
// Car
// Blue,Mazda
// Model|m3
delims = new List<string> {"|c", "|m", ","};
string[] out2 = input.Split(delims.ToArray(),StringSplitOptions.None).Dump();
//output:
// Car
// Blue
// Mazda~Model
// 3
You can use SelectMany to get the result from all the data strings and ToArray() method to create an array from delimiters
var result = data.SelectMany(s => s.Split(delimiters.ToArray(), StringSplitOptions.None));

Array 11th index

Ok so I was reworking the current infrastructure for my AI and I created it into an array. But i'm not sure how to get the 11th value to be recognized in the array "dictonary"
Here is the code!
string name = "Jarvis";
string[] dictionary = new string[] { "hello", "how", "are", "you", "sup", "wake", "up", "daddys", "home", "what", "version", "whats", "your" };
bool helloflag = false;
void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string code = "";
//ResultText = "Hello how are you"
string[] words = e.Result.Text.Split(' ');
//words = ["Hello", "how", "are", "you"]
foreach (string word in words)
{
for (int index = 0; index < dictionary.Count(); index++)
{
if (word == dictionary[index])
{
code += index.ToString();
}
}
}
//code = "123"; //how are you
//code = "236"; //are you up
//HELLO
if ((code == "0") || (code == "4"))
{
Jarvis.Speak("Hello sir");
helloflag = true;
} else if ((helloflag == true ) && (code == "123"))
{
Jarvis.Speak("im good sir");
helloflag = false;
}
This is part of the .NET speech API, but is using an array to call the words you say.
When you're adding to code, you need to put a separator between the codes. Otherwise, you can't tell whether 12 is word 1 followed by word 2, or word 12. E.g.
code += index.toString() + " ";
Then you can do things like:
if (code == "10 9 2 3 ") // whats version are you
Don't forget the space at the end.
Prob it is more flexible to store your code values in array, e.g.:
var code = new List<int>();
....
code.Add(index);
...
if(code.SequenceEqual(new int[] {1, 2, 3})
{
...

Categories