try
{
int Count_Good = 0, Count_Bad = 0;
StreamReader reading = File.OpenText("out.txt");
string str;
while ((str = reading.ReadLine()) != null)
{
StreamReader sr = new StreamReader("slovar_good.txt");
string line;
while (!sr.EndOfStream)
{
line = sr.ReadLine();
if (str.Contains(line))
{
Count_Good++;
}
}
StreamReader sr1 = new StreamReader("slovar_bad.txt");
string line1;
while (!sr1.EndOfStream)
{
line1 = sr1.ReadLine();
if (str.Contains(line1))
{
Count_Bad++;
}
}
}
textBox1.Text = Convert.ToString("Плохих слов: " + Count_Bad + Environment.NewLine + "Хороших слов: " + Count_Good + Environment.NewLine);
if (Count_Good > Count_Bad)
{
textBox1.AppendText(" Хорошая организация");
}
else
{
textBox1.AppendText(" Плохая организация");
}
}
catch
{
textBox1.Text = "не работает";
}
}
}
int Count_Good = 0, Count_Bad = 0;
// получаем словари в память, чтобы не открывать файлы словарей повторно
// а также переводим слова в строчные
var goods = File.ReadAllLines("slovar_good.txt").Select(word => word.ToLower());
var bads = File.ReadAllLines("slovar_bad.txt").Select(word => word.ToLower());
var file = File.OpenText(@"C:\Users\pavlovaa\Desktop\hik.txt");
string str;
while ((str = file.ReadLine()) != null)
{
str = str.ToLower();
Count_Good += CountWords(str, goods);
Count_Bad += CountWords(str, bads);
}
int CountWords(string str, IEnumerable<string> words)
{
if (string.IsNullOrWhiteSpace(str)) return 0;
var count = 0;
foreach (var word in words)
{
if (string.IsNullOrWhiteSpace(word)) continue;
int pos;
while ((pos = str.IndexOf(word)) != -1)
{
count++;
str = str.Substring(pos + word.Length);
}
}
return count;
}
class Program
{
static void Main(string[] args)
{
var text_path = Environment.CurrentDirectory + @"/files/text.txt";
var text = File.ReadAllText(text_path);
var good_dict_path = Environment.CurrentDirectory + @"/files/good.txt";
var good_dict = File.ReadAllLines(good_dict_path).ToHashSet();
var bad_dict_path = Environment.CurrentDirectory + @"/files/bad.txt";
var bad_dict = File.ReadAllLines(bad_dict_path).ToHashSet();
var text_arr = GetTextAsArray(text);
var good_count = GetWordsCount(text_arr, good_dict);
var bad_count = GetWordsCount(text_arr, bad_dict);
Console.WriteLine(good_count);
Console.WriteLine(bad_count);
}
static IEnumerable<string> GetTextAsArray(string text)
{
return text.Split('.', ' ', ',', ':', ';', '!', '?', '\t', '\n', '\r', '(', ')')
.Where(x => x.Length > 0);
}
static int GetWordsCount(IEnumerable<string> text_arr, HashSet<string> dictionary)
{
var count = 0;
foreach (var str in text_arr)
if (dictionary.Contains(str))
count++;
return count;
}
}