示例#1
0
    def to_canonical(self, operation, ignore_non_path_params):

        entity_params = list(filter(ParamUtils.is_entity_parameter, operation.params))
        path_params = list(filter(lambda p: p.location == 'path', entity_params))
        # non_path_params = list(filter(lambda p: p.location != 'path', entity_params))

        path_params, intent = self.to_intent(operation, path_params)
        if not intent:
            return None
        canonical = finalize_utterance(intent)

        if path_params:
            canonical += ' for ' + to_entities(path_params)

        ic = IntentCanonical(operation.intent, canonical, path_params)

        if ignore_non_path_params:
            return [ic]

        rest_params = []
        for p in entity_params:
            if p.location != "path":
                rest_params.append(p)

        ret = [ic]
        if rest_params:
            for p in rest_params:
                params = [p]
                params.extend(path_params)
                ret.append(IntentCanonical(operation.intent,
                                           canonical + to_parameters_postfix([p]),
                                           params))

        return ret
示例#2
0
    def translate(self, operation, sample_values=True, ignore_non_path_params=False):
        method = operation.verb
        resources = extract_resources(operation)

        if len(resources) == 0:
            return None

        for translator in self.translators:
            canonical = translator(method, resources, sample_values)
            if canonical:
                canonical = finalize_utterance(canonical)
                entity_params = list(filter(ParamUtils.is_entity_parameter, operation.params))
                path_params = list(filter(lambda p: p.location == 'path', entity_params))
                ic = IntentCanonical(operation.intent, canonical, path_params)

                if ignore_non_path_params:
                    return [ic]

                rest_params = []
                for p in entity_params:
                    if p.location != "path":
                        rest_params.append(p)

                ret = [ic]
                if rest_params:
                    for p in rest_params:
                        params = [p]
                        params.extend(path_params)
                        ret.append(IntentCanonical(operation.intent,
                                                   canonical + to_parameters_postfix([p]),
                                                   params))

                return ret

        return None
示例#3
0
    def extract_intent(text):

        if not text or text == 'None':
            return None

        # remove links in swagger []()
        if "](" in text or "] (" in text:
            text = replace_hyperlinks(text)

        text = BeautifulSoup(text, "lxml").text

        if ':' in text:
            text = text[:text.index(':')]

        if '(' in text:
            text = re.sub(r"\(.*\)", '', text)

        expr = None
        for sent in to_sentences(text):

            if len(sent) > 120:
                continue

            sent = sent.lower()

            tagged_sent = nlp.pos_tag(sent)

            count = 0
            for w, t in tagged_sent:
                if t in {'VB', 'VBZ'}:
                    count += 1

            if count > 1 and len(sent) > 80:
                # print("More than one verb: ", sent)
                continue

            if tagged_sent[0][1] == 'VB' or tagged_sent[0][0] in common_verbs or \
                    (tagged_sent[0][1] == 'RB' and tagged_sent[1][1] == 'VB'):
                expr = sent
            elif tagged_sent[0][1] == 'VBZ' or tagged_sent[0][0] in common_sverbs or \
                    (tagged_sent[0][1] == 'RB' and tagged_sent[1][1] == 'VB'):
                if tagged_sent[0][1] == 'RB' and tagged_sent[1][1] == 'VB':
                    verb = tagged_sent[1][0]
                else:
                    verb = tagged_sent[0][0]
                old_verb = verb
                if verb not in {"was", "is", "has"}:
                    verb = lemmatizer.lemmatize(verb, pos=VERB)

                expr = sent.replace(old_verb, verb)

            if expr and 'http' in expr:
                continue

            if expr and 'see' in expr and (':' in expr or 'please' in expr or 'href' in expr):
                continue

            if expr and ('<' in expr and '>' in expr or '<p>' in expr):
                continue

            if expr:
                expr = finalize_utterance(expr)
                if " by " in expr:
                    expr = expr[:expr.index(" by ")]
                return expr

        return None