Code:
The Lion and the Mouse
Once when a lion, the king of the jungle, was asleep, a little mouse began running up and down on him.
This soon awakened the lion, who placed his huge paw on the mouse, and opened his big jaws to swallow him.
"Pardon, O King!" cried the little mouse.
"Forgive me this time.
I shall never repeat it and I shall never forget your kindness.
And who knows, I may be able to do you a good turn one of these days!”
The lion was so tickled by the idea of the mouse being able to help him that he lifted his paw and let him go.
Sometime later, a few hunters captured the lion, and tied him to a tree.
After that they went in search of a wagon, to take him to the zoo.
Just then the little mouse happened to pass by.
On seeing the lion’s plight, he ran up to him and gnawed away the ropes that bound him, the king of the jungle.
"Was I not right?" said the little mouse, very happy to help the lion.
MORAL: Small acts of kindness will be rewarded greatly.
here is my code so far;
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace project
{
class Program
{
public void article_2_step_2(string[] sentence_words, int counter)
{
string record_sentence = "";
int counter_2 = 0;
StreamReader article = new StreamReader("word_list.txt");
while ((record_sentence = article.ReadLine()) != null)
{
if (record_sentence == "") break;
sentence_words[counter_2] = record_sentence;
counter_2++;
}
article.Close();
}
public void article_2_step_1(ref int counter)
{
string record_sentence = "";
StreamReader article = new StreamReader("word_list.txt");
while ((record_sentence = article.ReadLine()) != null)
{
if (record_sentence == "") break;
counter++;
}
article.Close();
}
public void write_3(string some_word, string new_number, int counter)
{
int article_counter = 0;
article_2_step_1(ref article_counter);
string[] article_array = new string[article_counter];
article_2_step_2(article_array, article_counter); // the lines in word_list.txt
File.WriteAllText("word_list.txt", String.Empty);
StreamWriter solution_1 = new StreamWriter("word_list.txt", true);
for (int i = 0; i < article_counter; ++i)
{
if(i == counter)
{
solution_1.WriteLine(some_word + " " + new_number);
}
else
{
solution_1.WriteLine(article_array[i]);
}
}
solution_1.Close();
}
public void compare_3(string some_word)
{
string record_sentence = "";
string Text_3 = "";
int counter = 0;
StreamReader record = new StreamReader("word_list.txt");
while ((record_sentence = record.ReadLine()) != null)
{
if (record_sentence == null) continue;
if (record_sentence == "") continue;
string Text = record_sentence.Substring(0, record_sentence.IndexOf(" "));
if (Text == some_word)
{
string Text_2 = record_sentence.Split(' ')[1];
int new_value = Convert.ToInt32(Text_2);
new_value++;
Text_3 = Convert.ToString(new_value);
break;
}
counter++;
}
record.Close();
write_3(some_word, Text_3, counter);
}
public void compare_2(string some_word, ref int found)
{
string record_sentence = "";
StreamReader record = new StreamReader("word_list.txt");
while ((record_sentence = record.ReadLine()) != null)
{
if (record_sentence == null) continue;
if (record_sentence == "") continue;
string Text = record_sentence.Substring(0, record_sentence.IndexOf(" "));
if (Text == some_word) found = 1;
}
record.Close();
}
public void write_2(string some_word)
{
int found = 0;
compare_2(some_word, ref found);
if(found == 0)
{
StreamWriter solution_1 = new StreamWriter("word_list.txt", true);
solution_1.WriteLine(some_word + " 1");
solution_1.Close();
}
if(found == 1)
{
compare_3(some_word);
}
}
public void write_1(int counter)
{
StreamWriter solution_1 = new StreamWriter("paragraph_match_count.txt", true);
solution_1.WriteLine(counter);
solution_1.Close();
}
public void compare(string [] long_line, int counter)
{
int counter_2 = 0;
string temp = "";
StringBuilder s = new StringBuilder();
for (int i = 0; i < counter; i++) // go through all paragraphs
{
counter_2 = 0;
string[] compare_1 = long_line[i].Split(' '); // putting each sentence word into an array element
for (int j = 0; j < compare_1.Length; j++) // go through entire paragraph i
{
for (int k = 0; k < counter; k++) // go through all words in paragraph
{
if (i == k) break;
s.Append("\\b");
s.Append(compare_1[j]);
s.Append("\\b");
temp = s.ToString();
s.Clear();
bool result = Regex.IsMatch(long_line[k], temp);
if (result)
{
//Console.WriteLine(long_line[i] + "\r\n:\r\n");
//Console.WriteLine(long_line[j] + "\r\n:\r\n");
//Console.WriteLine(compare_1[k] + "\r\n/////\r\n");
write_2(compare_1[j]);
counter_2++;
}
}
//string[] compare_1 = long_line[i].Split(' '); // putting each sentence word into an array element
//for (int k = 0; k < compare_1.Length; k++) // go through entire paragraph i
//{
// if (long_line[i].Contains(compare_1[k]))
// {
// //Console.WriteLine(long_line[i] + "\r\n:\r\n");
// //Console.WriteLine(long_line[j] + "\r\n:\r\n");
// //Console.WriteLine(compare_1[k] + "\r\n/////\r\n");
// write_2(compare_1[k]);
// counter_2++;
// }
//}
}
write_1(counter_2);
}
}
static public string RemoveDuplicateWords(string v)
{
// 1
// Keep track of words found in this Dictionary.
var d = new Dictionary<string, bool>();
// 2
// Build up string into this StringBuilder.
StringBuilder b = new StringBuilder();
// 3
// Split the input and handle spaces and punctuation.
string[] a = v.Split(new char[] { ' ', ',', ';', '.' },
StringSplitOptions.RemoveEmptyEntries);
// 4
// Loop over each word
foreach (string current in a)
{
// 5
// Lowercase each word
string lower = current.ToLower();
// 6
// If we haven't already encountered the word,
// append it to the result.
if (!d.ContainsKey(lower))
{
b.Append(current).Append(' ');
d.Add(lower, true);
}
}
// 7
// Return the duplicate words removed
return b.ToString().Trim();
}
public void remove_Unwanted_chars(ref string text)
{
//Console.WriteLine("part_10 : step_3");
// remove these chars
// numbers
text = text.Replace("0", "");
text = text.Replace("1", "");
text = text.Replace("2", "");
text = text.Replace("3", "");
text = text.Replace("4", "");
text = text.Replace("5", "");
text = text.Replace("6", "");
text = text.Replace("7", "");
text = text.Replace("8", "");
text = text.Replace("9", "");
// brackets
text = text.Replace("(", "");
text = text.Replace(")", "");
text = text.Replace("{", "");
text = text.Replace("}", "");
text = text.Replace("[", "");
text = text.Replace("]", "");
text = text.Replace("<", "");
text = text.Replace(">", "");
// slash and pipe
text = text.Replace("/", "");
text = text.Replace("\\", "");
text = text.Replace("|", "");
// commas quotes periods
text = text.Replace("\"", "");
text = text.Replace("\'", "");
text = text.Replace(".", "");
text = text.Replace(",", "");
text = text.Replace("!", "");
text = text.Replace("?", "");
text = text.Replace(";", "");
text = text.Replace(":", "");
text = text.Replace(",", "");
text = text.Replace("’", "");
text = text.Replace("“", "");
text = text.Replace("”", "");
text = text.Replace("`", "");
// symbols
text = text.Replace("~", "");
text = text.Replace("!", "");
text = text.Replace("@", "");
text = text.Replace("#", "");
text = text.Replace("$", "");
text = text.Replace("%", "");
text = text.Replace("^", "");
text = text.Replace("&", "");
text = text.Replace("*", "");
text = text.Replace("-", "");
text = text.Replace("_", "");
text = text.Replace("+", "");
text = text.Replace("=", "");
// remove double space
while (text.IndexOf(" ") != -1)
{
text = text.Replace(" ", " ");
}
}
public void make_long_line(string[] long_line)
{
string record_sentence = "";
string temp = "";
string lower = "";
int counter = 0;
StringBuilder s = new StringBuilder();
StreamReader record = new StreamReader("article.txt");
while ((record_sentence = record.ReadLine()) != null)
{
if (record_sentence == "")
{
temp = s.ToString();
remove_Unwanted_chars(ref temp);
lower = temp.ToLower();
lower = RemoveDuplicateWords(lower);
long_line[counter] = lower;
//Console.WriteLine(long_line[counter] + "\r\n");
s.Clear();
counter++;
}
else
{
s.Append(record_sentence);
s.Append(" ");
}
}
record.Close();
temp = s.ToString();
remove_Unwanted_chars(ref temp);
lower = temp.ToLower();
lower = RemoveDuplicateWords(lower);
long_line[counter] = lower;
//Console.WriteLine(long_line[counter] + "\r\n");
s.Clear();
}
public void count_paragraphs(ref int counter)
{
string record_sentence = "";
StreamReader record = new StreamReader("article.txt");
while ((record_sentence = record.ReadLine()) != null)
{
if (record_sentence == "")
{
counter++;
}
}
record.Close();
counter++;
}
static void Main(string[] args)
{
File.WriteAllText("paragraph_match_count.txt", String.Empty);
File.WriteAllText("word_list.txt", String.Empty);
Program paragraphs = new Program();
int counter = 0;
paragraphs.count_paragraphs(ref counter);
string[] long_line = new string[counter];
paragraphs.make_long_line(long_line);
paragraphs.compare(long_line, counter);
}
}
}
the problem function is "public void compare(string [] long_line, int counter)". im lost as to how to get the proper count of the word "the" right now i count it 21 times but its in the text file 22 times.