I have been trying to gain more understanding and experience in the NPL
area and to get some more practice. I decided to attempt to create a simple high-level analysis program.
Here's what I'm trying to do:
- Add a function that allows me to read in a full document (wordpad) instead of Console.Readline(); with CMD input
- Improve organization, cleaner, or shorter code
- Count words
The Code:
namespace HOMEWORK
{
class Program
{
static void Main(string[] args) //2 foreach() 2 for() 4 if() 28 .methods//
{
LinkedList<MatchCounter> matchCounters = new LinkedList<MatchCounter>();
string[] currentPats = new[] { "the", "in" };
StringBuilder SB = new StringBuilder();
for (int i = 0; i < currentPats.Length; i++)
{
SB.Append(@"(?=(?<p").Append(i).Append(">").Append(currentPats[i]).Append("))?");
}
Regex regexx = new Regex(SB.ToString(), RegexOptions.Compiled);
Console.WriteLine("CURRENT PATTERN: {0}", regexx);
string input = Console.ReadLine();
Console.WriteLine("input: {0} ", input);
foreach (Match match in regexx.Matches(input).Cast<Match>())
{
;
for (var i = 0; i < currentPats.Length; i++)
{
Group group = match.Groups["p" + i];
if (!group.Success)
continue;
if (group.Value != "")
{
bool isFound = false;
foreach (MatchCounter copy in matchCounters)
{
if (copy.GetSyllable() == group.Value)
{
copy.Increment();
isFound = true;
break;
}
}
if (!isFound)
matchCounters.AddLast(new MatchCounter(group.Value));
}
Console.WriteLine("Matched pattern #{0}: '{1}' at index {2}", i, group.Value, group.Index);
}
}
Console.ReadKey();
}
}
public class MatchCounter
{
int count;
string syllable;
public MatchCounter(string Syl)
{
count = 1;
syllable = Syl;
}
public string GetSyllable()
{
return syllable;
}
public void Increment()
{
count++;
}
public int GetCount()
{
return count;
}
}
}
I am still a learner and would like my code to be reviewed with feedback. I would greatly appreciate any feedback about the code quality, performance or any other possible corrections or how to do things differently.