Beispiel #1
0
def plural_to_singular_edit(canonical, resources=None):
    """
    corrects plural noun grammatical errors
    """

    for resource in resources:
        rname = resource.name
        if resource.resource_type != SINGLETON:
            continue

        canonical = canonical.replace(rname, "a {}".format(singular(rname)))
        canonical = canonical.replace(" the a ", " a ")
        canonical = canonical.replace(" a a ", " a ")
        canonical = canonical.replace(" an a ", " a ")
        canonical = canonical.replace(" a an ", " a ")

    canonical = LanguageChecker().grammar_corector(canonical,
                                                   categories=['MISC'
                                                               ]).lower()
    tokens = tokenize(canonical, normilize_text=False)

    ret = []
    seen_article = -20
    for token in tokens:

        if token in {"a", "an"}:
            seen_article = 0

        if 3 > seen_article > 0:
            if not is_singular(token):
                ret.append(singular(token))
                seen_article = 0
                continue

        ret.append(token)
        seen_article += 1

    tokens = []
    prev = False
    for token in reversed(ret):
        if not is_singular(token):
            if prev:
                token = singular(token)
            prev = True
        tokens.append(token)

    ret = reversed(tokens)

    return " ".join(ret).replace("< <", "<<").replace("> >", ">>")
Beispiel #2
0
def translate_collection_adjective(method, resources, sample_values):
    if method not in ['get', 'delete']:
        return None

    if len(resources) != 2:
        return None

    if resources[0].resource_type != ATTRIBUTE_RESOURCE or resources[1].resource_type != COLLECTION:
        return None

    adjective = ParamUtils.normalize(resources[0].name)
    resource = ParamUtils.normalize(resources[1].name)

    for key in ["get ", "set ", "create ", "put ", "delete "]:
        if resource.startswith(key):
            resource = resource[len(key):]

    if is_singular(resource) or ' ' in resource:
        ret = 'get the {} {}'
    else:
        ret = 'get the list of {} {}'

    if method == 'delete':
        ret = 'delete all {} {}'

    return ret.format(adjective, resource)
Beispiel #3
0
    def remove_non_informative_segments(url, base_path):
        """"
        Removes non informative segments of the given URL
        """

        if "?" in url:
            url = url[:url.index("?")]

        if base_path:
            parts = base_path.split('/')

            prev = None
            for i, p in enumerate(parts):
                if "{" in p and "}" in p or not is_singular(p) or p in ["search", "query", "count"] \
                        or ParamUtils.is_version(prev):
                    base_path = base_path[:base_path.index(p)]
                    break
                prev = p

        if base_path and base_path != '/':
            url = url.replace(base_path, "/")

        url = url.replace('http://', '/').replace('https://', '/')
        url = url.replace("{", "/{")
        url = url.replace("}", "}/")
        url = url.replace("//", "/")

        return url, base_path
Beispiel #4
0
    def filter_redundant_url_segments(parts):
        ret = []
        previous = None
        for word in parts:

            if not word:
                previous = word
                continue

            if previous and ParamUtils.normalize(
                    word[1:-1]) == ParamUtils.normalize(
                        previous) and is_singular(word[1:-1]):
                '''remove cases like: /countries/country_code/{country_code}'''
                ret = ret[:-1]
                ret.append(word)
                continue

            if word in []:
                '''ignore common URL prefixes like search/filter/...'''
                previous = word
                continue

            # tagged = nlp.pos_tag(word)
            # if tagged:
            #     '''Ignore cases like /items/latest'''
            #     if tagged[0][1].startswith('JJ'):
            #         previous = word
            #         continue

            previous = word
            ret.append(word)

        return ret
Beispiel #5
0
def translate_singleton_collection_adjective(method, resources, sample_values):
    if method not in ['get', 'post', 'delete']:
        return None

    if len(resources) != 3:
        return None

    if resources[2].resource_type != SINGLETON or resources[1].resource_type != COLLECTION or \
                    resources[0].resource_type != ATTRIBUTE_RESOURCE:
        return None

    resource = ParamUtils.normalize(singular(resources[2].name))
    resource2 = ParamUtils.normalize(resources[1].name)
    # param_value = '<<{}>>'.format(resources[2][1]['val'])

    if sample_values:
        vals = param_sampler.sample(resources[2].param.name, 1)
        val = vals[0] if vals else resources[2].param.name
    else:
        val = resources[2].param.name

    param_value = '<< {} >>'.format(val)

    adjective = ParamUtils.normalize(resources[0].name)

    # ret = '{} the {} of the {} with {} being {}'

    # if method == 'post':
    #     resource = singular(resource)
    #     resource2 = singular(resource2)
    #     ret = '{} a {} for the {} with {} being {}'
    # elif method == 'delete':
    #     resource = singular(resource)
    #     ret = '{} the {} of the {} with {} being {}'

    if is_singular(resource2):
        ret = 'get the {} {} of a {} with {} being {}'
    else:
        ret = 'get the list of {} {} of a {} with {} being {}'

    if method == 'post':
        resource = singular(resource)
        resource2 = singular(resource2)
        ret = 'create all {} {} for a {} with {} being {}'
    elif method == 'delete':
        resource = singular(resource)
        ret = 'delete all {} {} of a {} with {} being {}'

    return ret.format(adjective, resource2, resource, ParamUtils.normalize(resources[2].param.name), param_value)
Beispiel #6
0
def translate_collection(method, resources, sample_values):
    if method not in ['get', 'post', 'delete']:
        return None

    if len(resources) != 1:
        return None

        # if ':' in resources[0].name:
        # //v1/{name}:setDefault
        # return None

    if resources[0].resource_type == ACTION_RESOURCE:
        ret = ParamUtils.normalize(resources[0].name)

        for w in ret.split():
            if not ParamUtils.is_necessary_param(w):
                return None

        if not ret or not is_verb(ret.split()[0]) or ret.endswith('ing') or ret.endswith('s'):
            return None

        return ret

    if resources[0].resource_type != COLLECTION:
        return None

    resource = ParamUtils.normalize(resources[0].name)

    for key in ["get ", "set ", "create ", "put ", "delete "]:
        if resource.startswith(key):
            resource = resource[len(key):]

    if is_singular(resource) or ' ' in resource:
        ret = 'get the {}'
    else:
        ret = 'get the list of {}'

    if method == 'post':
        resource = singular(resource)
        ret = 'create a {}'
    elif method == 'delete':
        ret = 'delete all {}'

    return ret.format(resource)
Beispiel #7
0
def translate_singleton_collection(method, resources, sample_values):
    if method not in ['get', 'post', 'delete']:
        return None

    if len(resources) != 2:
        return None

        # if ':' in resources[0].name:
        # //v1/{name}:setDefault
        # return None

    if resources[0].resource_type != COLLECTION or resources[1].resource_type != SINGLETON:
        return None

    resource = ParamUtils.normalize(singular(resources[1].name))
    resource2 = ParamUtils.normalize(resources[0].name)

    if sample_values:
        vals = param_sampler.sample(resources[1].param, 1)
        val = vals[0] if vals else resources[1].param.name
    else:
        val = resources[1].param.name

    param_value = '<< {} >>'.format(val)

    # param_value = '<<{}>>'.format(resources[1].param.name)

    if is_singular(resource2):
        ret = 'get the {} of a {} with {} being {}'
    else:
        ret = 'get the list of {} of a {} with {} being {}'

    if method == 'post':
        resource = singular(resource)
        resource2 = singular(resource2)
        ret = 'create a {} for a {} with {} being {}'
    elif method == 'delete':
        resource = singular(resource)
        ret = 'delete the {} of a {} with {} being {}'

    return ret.format(resource2, resource, ParamUtils.normalize(resources[1].param.name), param_value)