Simple Responder By Using NLP (Natural Language Processing) With The Help of Jaccard Similarity Algorithm (C#)





Hello Everyone!
       Today i am going to show how you can create Simple Responder (Knowledge Re-presenter) by using NLP (Natural Language Processing).
       For the understanding of  computer NLP is use for understanding behavior and nature of our text. After applying NLP on  data, a program or application can easily respond to what ever we ask in form of text by using given data set.
       There are many algorithm Manhattan Distance, Cosine Angle, Jaccard Similarity etc. We will use Jaccard Similarity formula in this application
Formula for Jaccard Similarity
First of All Design User Interface.

Now create a method that will take whole paragraph (Data Set) and covert whole paragraph into lines and convert each line into array of word

        public List<String[]> convertToJaggedarray(string paragraph)
        {
            String[] lines = paragraph.Split('.');
            int length = lines.Length;
            List<String[]> list = new List<string[]>();
            for (int i = 0; i < length; i++)
            {
                String[] words = lines[i].Split(' ');
                list.Add(words);
            }
            return list;
        }

Now create a method that will return the UNION of query and Data Set

         public float Unioned(String[] Query, String[] Sentence)
        {
            int count = 0;
            List<String> Union = new List<string>();
            for (int i = 0; i < Query.Length; i++)
{
    Union.Add(Query[i]);
}
            for (int i = 0; i < Sentence.Length; i++)
            {
                for (int j = 0; j < Union.Count - 1; j++)
                {
                    if (Sentence[i].Equals(Union[j]))
                    {
                        count++;
                    }
                }
                if(count == 0)
                {
                    Union.Add(Sentence[i]);
                }
                count = 0;
            }

            return Union.Count;
        }

Now create a method that will return the INTERSECTION of query and Data Set

        public float Intersection(String[] Query, String[] Sentence)
        {
            List<String> intersected = new List<string>();

            for (int i = 0; i < Query.Length; i++)
            {
                for (int j = 0; j < Sentence.Length; j++)
                {
                    if (Query[i].Equals(Sentence[j]))
                    {
                        intersected.Add(Query[i]);
                    }
                }
            }
            return intersected.Count;
        }


Now create method that will calculate the value between Query and Data Set

        public float LenghtCardinality(String[] Query, String[] Sentence)
        {
            float Value1 = Intersection(Query,Sentence);
            float value2 = Unioned(Query,Sentence);

            float result = Value1 / value2;

            return result;
        }

We compare our query with each line of data Set and stored that value into list value with maximum value (Maximum Similarity) will be our answer or response.

Further code will be done on button click

            String Qline = textBox2.Text;
            String[] Query = Qline.Split(' ');
            string paragraph = richTextBox1.Text;

            List<String[]> list = convertToJaggedarray(paragraph);

       
            List<float> listWithValue = new List<float>();

            int length = list.Count - 1;
            for (int i = 0; i < length; i++)
            {
                float value = LenghtCardinality(Query, list[i]);
                listWithValue.Add(value);
            }

            float maximumValue = listWithValue.Max();
         
            int index = listWithValue.IndexOf(maximumValue);



            String[] answerLine = list[index];
            textBox1.Text = "";
            for (int i = 0; i < answerLine.Length; i++)
            {
                textBox1.Text = textBox1.Text + " " + answerLine[i];
            }

Now run the CODE!!!

That is how you can create a Simple responder. This is simple application you can create more application like this with the help of this application.

HERE IS SOURCE CODE

CLICK HERE TO DOWNLOAD

If you have any question feel free to ask in comment section.

THANK YOU!
Simple Responder By Using NLP (Natural Language Processing) With The Help of Jaccard Similarity Algorithm (C#) Simple Responder By Using NLP (Natural Language Processing) With The Help of Jaccard Similarity Algorithm (C#) Reviewed by Unknown on 12:44 AM Rating: 5

No comments:

Powered by Blogger.