def getAllConnects(self, sentence): logging.debug('getAllConnects start') result = [] options = ParseOptions() try: s = sentence.encode('ascii', 'ignore') except: return result sent = Sentence(s, self.dictionary) lc = sent.parse(options) logging.debug('checkSummary sent parsed') if lc == 0: clg.parse_options_set_min_null_count(options._po, 1) clg.parse_options_set_max_null_count( options._po, clg.sentence_length(sent._sent)) lc = sent.parse(options) if lc > 0: linkage = Linkage(0, sent, options) num_links = clg.linkage_get_num_links(linkage._link) for i in range(num_links): label = clg.linkage_get_link_label(linkage._link, i) lword = clg.linkage_get_link_lword(linkage._link, i) rword = clg.linkage_get_link_rword(linkage._link, i) lw = clg.sentence_get_word(sent._sent, lword) rw = clg.sentence_get_word(sent._sent, rword) result.append((label, lword, rword, lw, rw)) del linkage del options del sent logging.debug('getAllConnects end') return result
def checkSummary(self, sentence): logging.debug('checkSummary start') result = "" s = sentence.encode('ascii') sent = Sentence(s, self.dictionary) lc = sent.parse(self.parse_options) logging.debug('checkSummary sent parsed') if lc > 0: linkage = Linkage(0, sent, self.parse_options) result = linkage.get_diagram() logging.debug('checkSummary OK') del linkage else: options = ParseOptions() clg.parse_options_set_min_null_count(options._po, 1) clg.parse_options_set_max_null_count( options._po, clg.sentence_length(sent._sent)) lc = sent.parse(options) if lc > 0: linkage = Linkage(0, sent, options) result = linkage.get_diagram() logging.debug('checkSummary OK') del linkage del options del sent logging.debug('checkSummary end') return result
def set_max_null_count(self,max_null_count): clg.parse_options_set_max_null_count(self._po,max_null_count)
def parse(self, dic): pErr = None szSent = self.sText sent = clg.sentence_create(self.sText, dic) self.nwords = clg.sentence_length(sent) opts = clg.parse_options_create() clg.parse_options_set_disjunct_cost(opts, 2) clg.parse_options_set_min_null_count(opts, 0) clg.parse_options_set_max_null_count(opts, 0) clg.parse_options_set_islands_ok(opts, 0) clg.parse_options_set_panic_mode(opts, 1) clg.parse_options_reset_resources(opts) num_linkages = clg.sentence_parse(sent, opts) res = (num_linkages >= 1) if (not res and (num_linkages == 0)): clg.parse_options_set_min_null_count(opts, 1) clg.parse_options_set_max_null_count(opts, clg.sentence_length(sent)) clg.parse_options_set_islands_ok(opts, 1) clg.parse_options_reset_resources(opts) num_linkages = clg.sentence_parse(sent, opts) self.bGrammarChecked = 1 self.bGrammarOK = res if (not res): if (num_linkages > 0): linkage = clg.linkage_create(0, sent, opts) if (linkage != None): i = 0 iLow = 0 iHigh = 0 iOff = self.iInLow totlen = len(self.sText) i = 1 while (i < clg.sentence_length(sent) and (iLow < totlen)): while ((szSent[iLow] == ' ') and (iLow < totlen)): iLow = iLow + 1 if (iLow >= totlen): break iHigh = iLow + len(clg.sentence_get_nth_word(sent, i)) self.addVecMapOfWords( iLow, iHigh, i, clg.sentence_get_nth_word(sent, i)) bNew = 0 if (not clg.sentence_nth_word_has_disjunction(sent, i)): if (not pErr): pErr = {} bNew = 1 if (bNew or (pErr["iWordNum"] + 1 < i)): if (not bNew): if (pErr): pErr = None pErr = {} iHigh = iLow + len( clg.sentence_get_nth_word(sent, i)) pErr["iErrLow"] = iLow + iOff - 1 pErr["iErrHigh"] = iHigh + iOff - 1 if (pErr["iErrLow"] < 0): pErr["iErrLow"] = 0 if (pErr["iErrHigh"] < totlen - 1): pErr["iErrHigh"] += 1 pErr["word"] = clg.sentence_get_nth_word( sent, i) pErr["iWordNum"] = i self.grammarErrors.append(pErr) pErr = None else: iHigh = iLow + strlen( clg.sentence_get_nth_word(sent, i)) + iOff pErr["iErrHigh"] = iHigh if (pErr["iErrHigh"] < totlen - 1): pErr["iErrHigh"] += 1 pErr["iWordNum"] = i iLow += len(clg.sentence_get_nth_word(sent, i)) i = i + 1 if (len(self.grammarErrors) == 0): if (pErr): pErr = None pErr = {} pErr["iErrLow"] = self.iInLow pErr["iErrHigh"] = self.iInHigh pErr["iWordNum"] = -1 pErr["sErrorDesc"] = clg.linkage_get_violation_name( linkage) if (pErr["iErrLow"] < 0): pErr["iErrLow"] = 0 self.grammarErrors.append(pErr) pErr = None sErr = clg.linkage_get_violation_name(linkage) clg.linkage_delete(linkage) else: if (pErr): pErr = None pErr = {} pErr["iErrLow"] = self.iInLow pErr["iErrHigh"] = self.iInHigh if (pErr["iErrLow"] < 0): pErr["iErrLow"] = 0 self.grammarErrors.append(pErr) pErr = None if (pErr): pErr = None clg.parse_options_delete(opts) clg.sentence_delete(sent)
def set_max_null_count(self, max_null_count): clg.parse_options_set_max_null_count(self._po, max_null_count)