#region Using directives
using System;
using System.Collections.Generic;
using System.Windows.Forms;
#endregion
namespace FindMatches
{
partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private class SearchResult
{
public Collection<string> Missings;
public Dictionary<string, int> Matches;
public SearchResult()
{
Matches = new Dictionary<string, int>();
Missings = new Collection<string>();
}
}
private int FindMatchingWords( string[] pList, string pSearchWord )
{
int oResult = 0;
foreach ( string Word in pList )
{
if ( Word.Equals(pSearchWord, StringComparison.OrdinalIgnoreCase) )
{
oResult++;
}
}
return oResult;
}
private string[] splitWords( string pList )
{
return pList.Split(new char[] { ' ', '\t', '\n', '\r', '.', ',', '?', '!'
, ';', '/', '\\', ')', '(', '*','[', ']'
,'\"', '\'', '<', '>', '|' }
, StringSplitOptions.RemoveEmptyEntries);
}
private SearchResult Search( string pSearchWords, string pCompareWords )
{
string[] lpSearchWords = splitWords(pSearchWords);
string[] lCompareWords = splitWords(pCompareWords);
Dictionary<string, int> Occurances = new Dictionary<string, int>(lpSearchWords.GetLength(0));
foreach ( string SearchWord in lpSearchWords )
{
if ( !Occurances.ContainsKey(SearchWord) )
Occurances.Add(SearchWord
, FindMatchingWords(lCompareWords, SearchWord));
}
SearchResult oResults = new SearchResult();
foreach ( KeyValuePair<string, int> Entry in Occurances )
{
if ( Entry.Value == 0 )
{
oResults.Missings.Add(Entry.Key);
}
else
{
oResults.Matches.Add(Entry.Key, Entry.Value);
}
}
return oResults;
}
private void CountClick( object sender, EventArgs e )
{
SearchResult searchResults;
if ( sender == LeftInRightMenuItem )
searchResults = Search(txtLeft.Text, txtRight.Text);
else
searchResults = Search(txtRight.Text, txtLeft.Text);
LeftInRightMenuItem.Checked = ( sender == LeftInRightMenuItem );
RightInLeftMenuItem.Checked = !LeftInRightMenuItem.Checked;
#region Adding LV Items...
ListViewGroup lvGrpMiss = lvMatches.Groups[0];
ListViewGroup lvGrpMatch = lvMatches.Groups[1];
lvMatches.BeginUpdate();
lvMatches.Items.Clear();
foreach ( KeyValuePair<string, int> Match in searchResults.Matches )
{
lvMatches.Items.Add(new ListViewItem(new string[2] { Match.Key, Match.Value.ToString() }
, lvGrpMatch));
}
foreach ( string Miss in searchResults.Missings )
{
lvMatches.Items.Add(new ListViewItem(Miss, lvGrpMiss));
}
lvMatches.EndUpdate();
#endregion
#region StatusStripes...
statusStripCountMissings.Text = string.Format("{0} missing words"
, searchResults.Missings.Count);
statusStripCountMatches.Text = string.Format("{0} matching words"
, searchResults.Matches.Count);
#endregion
GC.Collect();
}
}
}