1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I downvoted your question because you did not explain how the algorithm works and what you are doing exactly and how. You also did not provide any examples. \$\endgroup\$
    – t3chb0t
    Dec 9, 2017 at 22:29
  • \$\begingroup\$ You should know that in most regex flavours look arounds are not quantifiable. The .Net implementation is one of the only ones I know of that allows for an optional lookahead. There's also no point in it. Just make your group optional. Can you explain what you're trying to achieve? \$\endgroup\$
    – RobH
    Dec 11, 2017 at 10:27
  • \$\begingroup\$ Please edit where you say NPL to NLP, I would but I can't change that few characters a.k.a. 2 \$\endgroup\$
    – 13ros27
    Dec 17, 2017 at 14:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.