def is_grammatical_sentence(sentence_text, language_dict, verbose=False): parse_options = ParseOptions(verbosity=0) parse_options.max_null_count = 999 # max number of words in single pass parse_options.linkage_limit = 100 # max number of linkages to generate parse_options.max_parse_time = 10 # in seconds sent = Sentence(str(sentence_text), language_dict, parse_options) wrong_sentences = [] linkages = None try: linkages = sent.parse() except LG_TimerExhausted: wrong_sentences.append(sentence_text) if verbose: print('Sentence too complex for parsing in {} seconds.'.format( parse_options.max_parse_time)) return False if not linkages or len(linkages) <= 0: wrong_sentences.append(sentence_text) if verbose: print('Error occurred - sentence ignored.') null_count = sent.null_count() if null_count == 0: return True else: wrong_sentences.append(sentence_text) return False
if sentence_text.strip() == '': continue sent = Sentence(str(sentence_text), lgdict, po) try: linkages = sent.parse() except LG_TimerExhausted: print('Sentence too complex for parsing in ~{} second{}.'.format( po.max_parse_time, nsuffix(po.max_parse_time))) continue if not linkages: print('Error occurred - sentence ignored.') continue if len(linkages) <= 0: print('Cannot parse the input sentence') continue null_count = sent.null_count() if null_count == 0: print("Sentence parsed OK") guess_found = False if DISPLAY_GUESSES: linkages, check_first = itertools.tee(linkages) # Check the first linkage for regexed/unknown words linkage = next(check_first) for word in list(linkage.words()): # search for something[x] if re.search(r'\S+\[[^]]+]', word): guess_found = True break # Show results with unlinked words or guesses
if sentence_text.strip() == '': continue sent = Sentence(str(sentence_text), lgdict, po) try: linkages = sent.parse() except LG_TimerExhausted: print('Sentence too complex for parsing in ~{} second{}.'.format( po.max_parse_time,nsuffix(po.max_parse_time))) continue if not linkages: print('Error occurred - sentence ignored.') continue if len(linkages) <= 0: print('Cannot parse the input sentence') continue null_count = sent.null_count() if null_count == 0: print("Sentence parsed OK", end='') linkages = list(linkages) correction_found = False # search for correction suggestions for l in linkages: for word in l.words(): if word.find(r'.#') > 0: correction_found = True break; if correction_found: break