def grade_essay(essay_tuple, essay_list): raw_body = essay_tuple[1] citation_type = essay_tuple[2] client = GrammarBotClient() edited_body = "" cursor = 0 citation_heading = "" if citation_type == "APA": citation_heading = "References" else: citation_heading = "Works Cited" if citation_heading not in raw_body: ret = "<p style=\"padding-top:1em\"><mark style=\"background-color:red;line-height:1.5em\">" + "ERROR: No reference list/works cited header found (this may be due to a typo in the word \"References\" or the word \"Works Cited\"). Unable to mark essay." + "</mark></p>" + raw_body return essay_tuple[0], ret body = check_citations(essay_tuple) body = body.split(citation_heading) raw_citations = "<p>" + citation_heading + "</p>" + body[-1] body = citation_heading.join(body[:-1]) plagiarism_ret = check_plagiarism.delay(essay_tuple[3], essay_tuple[4], body, essay_list) plagiarism = plagiarism_ret.get() result = None if len(body) > 6000: result = client.check(body[:6000]) else: result = client.check(body) for match in result.matches: # you also have access to match.category if you want offset = match.replacement_offset length = match.replacement_length edited_body += body[cursor:offset] if body[offset:(offset + length)] != "br" or body[offset:( offset + length)] != "&emsp": edited_body += "<mark style=\"background-color:red;\">" + body[ offset:(offset + length)] + "</mark>" cursor = offset + length # if cursor < text length, then add remaining text to new_text if cursor >= len(body): edited_body += body[cursor:] if edited_body == "": edited_body = body edited_body = plagiarism + edited_body + raw_citations return essay_tuple[0], edited_body
def checkGrammar(text): # Creating the client client = GrammarBotClient() # or, signup for an API Key to get higher usage limits here: https://www.grammarbot.io/ #client = GrammarBotClient(api_key='my_api_key_here') # GrammarBotClient(api_key=my_api_key_here) res = client.check(text) mistakes = [] for mistake in res.matches: value = {} value['offset'] = mistake.replacement_offset value['length'] = mistake.replacement_length value['message'] = mistake.message value['replacements'] = mistake.replacements value['corrections'] = mistake.corrections value['rule'] = mistake.rule value['category'] = mistake.category mistakes.append(value) ret = mistakes #language = res.detected_language #result_is_incomplete = res.result_is_incomplete #num_error = len(res.matches) #ret = {'language': language, 'result_is_incomplete': result_is_incomplete, 'errors':num_error, 'mistakes': mistakes} return json.dumps(ret)
def fix_tweet(text): # spell = SpellChecker() # list_text = list(text.split(" ")) # misspelled = spell.unknown(list_text) # print(text) # for word in misspelled: # print(word) # text.replace(word, spell.correction(word)) # print(text) # lang_tool = language_tool.LanguageTool("en-US") # matches = lang_tool.check(text) # tool = language_check.LanguageTool('en-US') # matches = tool.check(text) # print(language_check.correct(text, matches)) # print(res.raw_json) NEWLINE = '\n' # Creating the client client = GrammarBotClient() # check the text, returns GrammarBotApiResponse object res = client.check(text) tweets_to_make = [] correct_text = "" limit = "" # Construct tweet for match in res.matches: # Create messages if (match.category == 'TYPOS'): correct_text += "At the start of character number " + \ str(match.replacement_offset) + " did you mean any of the following instead: " + \ str(match.replacements[0:3]) + "." else: correct_text += match.message # Insert a new line after each recommendation correct_text += NEWLINE # Make sure it doesn't exceed the tweet character limit if (len(limit) + len(correct_text)) > 280: tweets_to_make.append(limit) limit = "" limit += correct_text correct_text = "" tweets_to_make.append(limit) return tweets_to_make
class Grammar(): def __init__(self): dotenv_path = os.path.join(os.path.dirname(__file__), '.env') load_dotenv(dotenv_path) rapidapi_key = os.environ.get('RAPIDAPI_KEY') self.client = GrammarBotClient(api_key=rapidapi_key) def check(self, text): message = '' res = self.client.check(text) for i, match in enumerate(res.matches): message += f'\n{i+1}. {match.message}, {match.replacements[:3]}\n' return message
class GrammarChecker: def __init__(self): self.client = GrammarBotClient(api_key='KS9C5N3Y') def check(self, text, file=None): res = self.client.check(text) for match in res.matches: if file: file.write("Position {} \n Replacements {} \n Rule {} Category {} Type {}\n".format(match.replacement_offset, match.replacements, match.rule, match.category, match.type)) else: print("Position {} \n Replacements {} \n Rule {} Category {} Type {}\n".format(match.replacement_offset, match.replacements, match.rule, match.category, match.type)) return res
def error_counter(msg_body): tokens = nlp(msg_body) err_count = 0 for sent in tokens.sents: x = sent.string.strip() #print(x) client = GrammarBotClient() res = client.check(x, 'en-GB') if len(res.matches) > 0: #print(res.matches) err_count += 1 if err_count > 5: return "\n\nDue to high grammatical errors this job is probably fake" else: return "\n\nDue to low grammatical errors this job is probably real"
def grammar_analysis(lines_of_text): client = GrammarBotClient(api_key='TODO: FILL IN API KEY') all_text = '' for txt in lines_of_text: all_text += (str(txt) + '/') # check the text, returns GrammarBotApiResponse object errors = client.check(text) # get error counts metadata = {} error_types = [] for err in errors.matches: start = err.replacement_offset end = start + err.replacement_length mistake = allText[start:end] # format suggestions suggestions = '' for suggestion in err.replacements: suggestions += suggestion + ', ' metadata[mistake] = suggestions[:-2] # take off last comma error_types.append(err.category) # get line numbers for help line_numbers = [] for mistake in metadata.keys(): for i, s in enumerate(linesOfText): if mistake in s: line_numbers.append(i) type_counts = dict(Counter(errorTypes)) type_counts_list = type_counts.items() total_errors = sum(types_counts.values()) result_string = json.dumps(res.raw_json, indent=4) mistake_info = [] for i, mistake in enumerate(metadata.keys()): mistake_info.append(( line_numbers[i], mistake, metadata[mistake])) flag = (total_errors / len(lines_of_text) > 1) return total_errors, type_counts_list, mistake_info, flag
def chekingCredibilitiy(self): domain = "" nameList = self.website.split('.') if nameList[0] == "www": domain = nameList[2] else: domain = nameList[1] if domain.lower() in CREDIBLE_DOMAINS: return True else: client = GrammarBotClient() text = self.getText() errors = client.check(text, "en-US") if len(errors.matches) < 4: return False else: return True
def grammar_checker(text): correct_texts = [] # collect possible ways of fixing text # spell checking & grammar checking with language_check module tool = language_check.LanguageTool('en-US') matches = tool.check(text) correct_texts.append(language_check.correct(text, matches)) # spell checking & grammar checking with grammarbot module client = GrammarBotClient() res = client.check(text) for match in res.matches: for correction in match.corrections: correct_texts.append(correction) print("grammar check") for text in correct_texts: print(text) return correct_texts
def output(): client = GrammarBotClient() client = GrammarBotClient( api_key='AF5B9M2X') # GrammarBotClient(api_key=my_api_key_here) res = client.check( text.get("1.0", "end-1c") ) # GrammarBotApiResponse(matches=[GrammarBotMatch(offset=2, length=4, rule={'CANT'}, category={'TYPOS'}), GrammarBotMatch(offset=26, length=5, rule={'CONFUSION_RULE'}, category={'TYPOS'})]) res.detected_language # "en-US" res.result_is_incomplete # False res.matches # [GrammarBotMatch(offset=2, length=4, rule={'CANT'}, category={'TYPOS'}), GrammarBotMatch(offset=26, length=5, rule={'CONFUSION_RULE'}, category={'TYPOS'})] match0 = res.matches[ 0] # GrammarBotMatch(offset=2, length=4, rule={'CANT'}, category={'TYPOS'}) match0.replacement_offset # 2 match0.replacement_length # 4 match0.replacements # ["can't", 'cannot'] match0.corrections # res.raw_json text2 = tk.Text(root, width=40, height=30) text2.place(x=500, y=50) text2.insert(tk.END, match0.corrections[0])
def compute_spell_grammar_score(text): client = GrammarBotClient(api_key='PLEASE_INSERT_API_KEY_TO_HERE') text = text.strip("\n\t\r .,?!;") res_js = client.check(text).raw_json tokens = text.count(" ") + 1 sens = text.count(".") + text.count("?") + text.count("!") + text.count( ";") + 1 result = [] cnt_spelling = 0 cnt_grammar = 0 warn_start = "<span style=\"background-color:yellow\">" error_start = "<span style=\"background-color:red\">" we_end = "</span>" conflict = 0 cnt_error = 0 for messeage in res_js['matches']: offset = messeage['offset'] + conflict leng = messeage['length'] if messeage['rule']['issueType'] == 'non-conformance': text = text[0:offset] + warn_start + text[ offset:offset + leng] + we_end + text[offset + leng:] conflict += 44 error = {} error['id'] = cnt_error error['id2'] = cnt_error * 1000 error['offset'] = messeage['offset'] error['length'] = messeage['length'] error['msg'] = messeage['message'] try: error['dst'] = messeage['description'] except: error['dst'] = "" error['rep'] = messeage['replacements'] cnt_error += 1 result.append(error) elif messeage['rule']['issueType'].find("grammar") != -1 or messeage[ 'rule']['issueType'].find("misspelling") != -1: if messeage['rule']['issueType'].find("grammar") != -1: cnt_grammar += 1 else: cnt_spelling += 1 text = text[0:offset] + error_start + text[ offset:offset + leng] + we_end + text[offset + leng:] conflict += 41 error = {} error['id'] = cnt_error error['id2'] = cnt_error * 1000 error['offset'] = messeage['offset'] error['length'] = messeage['length'] error['msg'] = messeage['message'] try: error['dst'] = messeage['description'] except: error['dst'] = "" error['rep'] = messeage['replacements'] cnt_error += 1 result.append(error) point = { 'grammar': (1 - (cnt_grammar / sens)) * 100, 'spelling': (1 - (cnt_spelling / tokens)) * 100, 'total': int((1 - 0.5 * (cnt_grammar / sens) - 0.5 * (cnt_spelling / tokens)) * 100) } return point, result
def writing(request, article_id): if request.method == "POST": form = WritingContent(request.POST) if form.is_valid(): text = form.cleaned_data['text'] text = text.strip("\n\t\r .,?!;") client = GrammarBotClient(api_key='KS9C5N3Y') res_js = client.check(text).raw_json tokens = text.count(" ") + 1 sens = text.count(".") + text.count("?") + text.count( "!") + text.count(";") + 1 result = [] cnt_spelling = 0 cnt_grammar = 0 warn_start = "<span style=\"background-color:yellow\">" error_start = "<span style=\"background-color:red\">" we_end = "</span>" conflict = 0 cnt_error = 0 for messeage in res_js['matches']: offset = messeage['offset'] + conflict leng = messeage['length'] if messeage['rule']['issueType'] == 'non-conformance': text = text[0:offset] + warn_start + text[ offset:offset + leng] + we_end + text[offset + leng:] conflict += 44 error = {} error['id'] = cnt_error error['id2'] = cnt_error * 1000 error['msg'] = messeage['message'] try: error['dst'] = messeage['description'] except: error['dst'] = "" error['rep'] = messeage['replacements'] cnt_error += 1 result.append(error) elif messeage['rule']['issueType'].find( "grammar") != -1 or messeage['rule']['issueType'].find( "misspelling") != -1: if messeage['rule']['issueType'].find("grammar") != -1: cnt_grammar += 1 else: cnt_spelling += 1 text = text[0:offset] + error_start + text[ offset:offset + leng] + we_end + text[offset + leng:] conflict += 41 error = {} error['id'] = cnt_error error['id2'] = cnt_error * 1000 error['msg'] = messeage['message'] try: error['dst'] = messeage['description'] except: error['dst'] = "" error['rep'] = messeage['replacements'] cnt_error += 1 result.append(error) point = { 'grammar': (1 - (cnt_grammar // sens)) * 100, 'spelling': (1 - (cnt_spelling // tokens)) * 100, 'total': int((1 - 0.5 * (cnt_grammar // sens) + 0.5 * (cnt_spelling // tokens)) * 100) } return render(request, 'writing_results.html', { 'errors': result, 'fixed': text, 'point': point }) else: conn = MongoClient() db = conn.MyProject collection = db.Writing records = collection.find() topics = list(records) context = {'topic': topics[article_id], 'form': WritingContent()} return render(request, 'writing.html', context) else: conn = MongoClient() db = conn.MyProject collection = db.Writing records = collection.find() topics = list(records) context = {'topic': topics[article_id], 'form': WritingContent()} return render(request, 'writing.html', context)
def get_essay(request): if request.user.is_authenticated: if request.method == 'POST': form = EssayForm(request.POST) if form.is_valid(): text_buffer = form.cleaned_data.get("essay_content").encode( 'utf-8') question_buffer = form.cleaned_data.get("question") result = model.calculate_score(text_buffer, question_buffer) from grammarbot import GrammarBotClient client = GrammarBotClient() sentences = text_buffer.split('.') content = '' for sentence in sentences: res = client.check(sentence) grm = [] counter = 0 sentence = sentence.replace("'", "\'") #print res.matches for each in res.matches: grm.append([ each.replacement_offset, each.replacement_length, each.message.replace('"', '\"').replace("'", '\"'), each.category.replace('"', '\"') ]) if len(grm) == 0: content += sentence + '.' continue for i in range(len(grm)): content += sentence[counter:grm[i][ 0]] + '<span class="mytooltip" style="color:white;background-color:#FB8C8C">' + sentence[ grm[i][0]:grm[i][0] + grm[i] [1]] + '<span class="mytooltiptext">' + grm[i][ 2].replace("'", "\'") + '</span>' + '</span>' counter = grm[i][0] + grm[i][1] content += '.' print content if result >= 0: Essays.objects.create(question = question_buffer, \ essay_content = text_buffer,\ predicted_score = result) result = round(result, 1) return render( request, 'main/result.html', { 'result': result, 'essay': json.dumps(content), 'grm': json.dumps(grm), 'original': json.dumps(text_buffer) }) else: result = 0 Essays.objects.create(question = question_buffer, \ essay_content = text_buffer,\ predicted_score = result) return render( request, 'main/result.html', { 'result': 'Sorry, this questions are not ready to be scored by system yet, but your submission will help the system developing.' }) else: form = EssayForm() return render(request, 'main/scoring.html', {'form': form}) else: return render(request, 'main/home.html')
class GrammarChecker: def __init__(self, text, max_char_count=17, sensitivity=1): # Initialize checker with text and preprocess text by removing unwanted sentences # Sensitivity (0, 1): how strict you are with typos # Sensitivity of 1 is strictest, 0 is most lenient self.text = text self.max_char_count = max_char_count self.sensitivity = sensitivity self.client = GrammarBotClient() self.sentences = self.get_sentences() self.num_words = 0 self.preprocess_text() def get_sentences(self): # Returns sentences from text nlp = English() nlp.add_pipe(nlp.create_pipe('sentencizer')) # updated doc = nlp(self.text) sentences = [sent.string.strip() for sent in doc.sents] return sentences def preprocess_text(self): # Remove sentences with words greater than max_char_count count = 0 for sent in self.sentences: for word in sent.split(): if len(word) > self.max_char_count: if sent in self.sentences: self.sentences.remove(sent) count += 1 self.num_words = count self.text = ' '.join(sent for sent in self.sentences) def measure_grammar(self, sensitivity=1, ignore_doublespace=True, ignore_punctuation=True): # Return a measure of typo appearance in text res = self.client.check(self.text) matches = res.matches results = [] num_typos = 0 if ignore_doublespace and ignore_punctuation: for match in matches: if match.category != 'TYPOGRAPHY' and match.category != 'PUNCTUATION': results.append(match) num_typos = len(results) elif ignore_doublespace: for match in matches: if match.category != 'TYPOGRAPHY': results.append(match) num_typos = len(results) elif ignore_punctuation: for match in matches: if match.category != 'PUNCTUATION': results.append(match) num_typos = len(results) num_sentences = len(self.sentences) measure = num_typos**1.5 * math.exp(num_typos - ( 0.25 * (num_sentences))) * sensitivity / self.num_words return measure
# -*- coding: utf-8 -*- """Untitled18.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1NC5k4CmrUhihDLYkQgiRyf0YSpxlLoz2 """ from grammarbot import GrammarBotClient import spacy nlp = spacy.load('en') text = input("Enter the body of the message: ") tokens = nlp(text) count = 0 for sent in tokens.sents: x = sent.string.strip() #print(x) client = GrammarBotClient() res = client.check(x, 'en-GB') if len(res.matches) > 0: #print(res.matches) count += 1 if count > 5: print("\n\nThis job is probably fake")
def check_grammar_bot(text): client = GrammarBotClient() res = client.check(text, 'en-US') if len(res.matches) == 0: return True return False
# or, signup for an API Key to get higher usage limits here: https://www.grammarbot.io/ client = GrammarBotClient(api_key='my_api_key_here') # GrammarBotClient(api_key=my_api_key_here) # you can even set the base URI to a different server # client = GrammarBotClient(base_uri='http://backup.grammarbot.io:80') # Analyzing the text # ================== # There is only one method to perform the analysis, viz. GrammarBotClient.check # method. text = 'I cant remember how to go their' # check the text, returns GrammarBotApiResponse object res = client.check(text) # GrammarBotApiResponse(matches=[GrammarBotMatch(offset=2, length=4, rule={'CANT'}, category={'TYPOS'}), GrammarBotMatch(offset=26, length=5, rule={'CONFUSION_RULE'}, category={'TYPOS'})]) # Inspecting the GrammarBotApiResponse object # =========================================== # check detected language res.detected_language # "en-US" # check if the result is incomplete res.result_is_incomplete # False # see the suggestions / corrections suggested by the GrammarBot API # returns a list of GrammarBotMatch objects describing each replacement res.matches # [GrammarBotMatch(offset=2, length=4, rule={'CANT'}, category={'TYPOS'}), GrammarBotMatch(offset=26, length=5, rule={'CONFUSION_RULE'}, category={'TYPOS'})]
# testing first = False # ^ testing for filename in files: if filename in blacklist: continue # testing if first: break else: first = True # ^ testing document = Document() file = root + '/' + filename #print(file) # read file content text = d2t.process(file) if len(text) <= 500000: # maximum length of text for api calls res = client.check(text) with open('test.txt', 'w+') as testf: testf.write(str(res.raw_json)) text_lines = text.split('\n') out_lines = [] for line in text_lines: words = line.split(' ') retLine = '' for line in out_lines: document.add_paragraph(line) document.save(cleaned_filepath + filename) del text del document
def bot_errors(line): client = GrammarBotClient() res = client.check(line) return len(res.matches)