def spellcheck(): handler = Lookup() corrections = [] words_processed = 0 text = request.form['text'] start_time = 0 end_time = 0 if (type(text) is str and len(text.strip()) > 0): start_time = time.time() fw = open('log.txt', 'a+', encoding='utf-8') fw.write("Proccessing Strated: {}\n".format(start_time)) lines = handler.load_raw_text(passage=text.strip()) fw.write("Lines fetched: {}\n".format(lines)) for line_of_words in lines: prev_word = (None, None) prev_status = None words_processed += len(line_of_words) for word in line_of_words: word_result = handler.validate_word(word, prev_word) fw.write("Validation Result: {}\n".format(word_result)) if (word_result['status'] != handler.CODE_CORRECT): #If previous word is non-word ignore real-word error if (prev_status == handler.CODE_NON_WORD_ERR and word_result['status'] in [ handler.CODE_REAL_WORD_ERR, handler.CODE_RWE_IGNORED ]): fw.write( "Included Status: Ignored (previous non-word)\n") pass #If previous word has real-word issue and also the current word has the same issue #ignore it to prevent chain errors elif (prev_status in [ handler.CODE_REAL_WORD_ERR, handler.CODE_RWE_IGNORED ] and word_result['status'] in [ handler.CODE_REAL_WORD_ERR, handler.CODE_RWE_IGNORED ]): fw.write( "Included Status: Ignored (previous real-word/rwe)\n" ) pass else: fw.write("Included Status: Included\n") corrections.append(word_result) prev_word = word prev_status = word_result['status'] end_time = time.time() fw.write("Proccessing Ended at {} in {} seconds\n".format( end_time, (end_time - start_time))) fw.write("----------------------------------------\n\n") fw.close() output = { 'processed_words': words_processed, 'duration': int(end_time) - int(start_time), 'corrections': corrections } return jsonify(output)
options_tuple[counter], c2 + 1), 'w+', encoding='utf-8') fw.write(validations_set[counter][c2]) fw.close() for counter in range(1, 4): for opt in options_tuple: file_s = "{}{}_{}.ready.txt".format(destination_validation, opt, counter) file_d = "{}{}_{}.result.txt".format(destination_validation, opt, counter) if (not os.path.exists(file_d) and os.path.exists(file_s)): print("Spell Check in Progress for {} ...".format(file_s)) lines = lookup.load_raw_text(file_path=file_s) corrections = [] corrected_words = [] for line_of_words in lines: prev_word = (None, None) for word in line_of_words: word_result = lookup.validate_word(word, prev_word) if (opt == 'NW' and word_result['status'] == lookup.CODE_NON_WORD_ERR): corrections.append(word_result) corrected_words.append(word_result['word'][0]) print("NW Correction added...") elif (opt == 'RW' and word_result['status'] in [ lookup.CODE_REAL_WORD_ERR, lookup.CODE_RWE_IGNORED