def edit_grammar(text): ret = [] words = text.split() prev = None sndPre = None for t in words: if prev == t: continue # size = len(ret) if sndPre in {'a', 'an'} and is_plural(t): ret.append(singular(t)) elif prev in {'a', 'an'} and is_plural(t): ret.append(singular(t)) elif prev == 'a' and t[0].lower() in {'a', 'e', 'i', 'o'}: ret = ret[:-1] ret.append('an') ret.append(singular(t)) elif prev == 'an' and t[0].lower() not in {'a', 'e', 'i', 'o'}: ret = ret[:-1] ret.append('a') ret.append(singular(t)) elif t == 's': ret = ret[:-1] ret.append(langtool.singleWordCorrection(prev + "s")) elif t == "'s": ret = ret[:-2] ret.append(langtool.singleWordCorrection(prev + "'s")) else: ret.append(t) sndPre = prev prev = t return " ".join(ret)
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)
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("> >", ">>")
def translate_singleton_action(method, resources, sample_values): if method not in ['get', 'patch', 'delete', 'put']: return None if len(resources) != 2: return None if ACTION_RESOURCE not in resources[0].resource_type or resources[1].resource_type != SINGLETON: return None resource = ParamUtils.normalize(resources[1].name) resource = singular(resource) if sample_values: vals = param_sampler.sample(resources[1].param.name, 1) val = vals[0] if vals else resources[1].param.name else: val = resources[1].param.name parameter_value = '<< {} >>'.format(val) verb = ParamUtils.normalize(resources[0].name) if verb.endswith('ed'): verb = verb[:-1] ret = '{} a {} with {} being {}' # if method == 'patch': # ret = 'update the {} with {} being {}' # elif method == 'delete': # ret = 'delete the {} with {} being {}' # elif method == 'put': # ret = 'replace the {} with {} being {}' return ret.format(verb, resource, ParamUtils.normalize(resources[1].param.name), parameter_value)
def translate_singleton(method, resources, sample_values): if method not in ['get', 'patch', 'delete', 'put']: return None if len(resources) != 1: return None # if ':' in resources[0].name: # //v1/{name}:setDefault # return None if resources[0].resource_type != SINGLETON: return None resource = ParamUtils.normalize(resources[0].name) resource = singular(resource) # print(resources) # print(resource) if sample_values: vals = param_sampler.sample(resources[0].param, 1) val = vals[0] if vals else resources[0].param.name else: val = resources[0].param.name parameter_value = '<< {} >>'.format(val) ret = 'get a {} with {} being {}' if method == 'patch': ret = 'update a {} with {} being {}' elif method == 'delete': ret = 'delete a {} with {} being {}' elif method == 'put': ret = 'replace a {} with {} being {}' return ret.format(resource, ParamUtils.normalize(resources[0].param.name), parameter_value)
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)
def translate_singleton_singleton_action(method, resources, sample_values): if method not in ['get', 'patch', 'delete', 'put']: return None if len(resources) != 3: return None if resources[2].resource_type != SINGLETON or resources[1].resource_type != SINGLETON \ or "Controller" not in resources[0].resource_type: return None resource2 = ParamUtils.normalize(singular(resources[2].name)) resource1 = ParamUtils.normalize(singular(resources[1].name)) if sample_values: vals = param_sampler.sample(resources[1].param.name, 1) val = vals[0] if vals else resources[1].param.name else: val = resources[1].param.name parameter_value1 = '<< {} >>'.format(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 parameter_value2 = '<< {} >>'.format(val) verb = ParamUtils.normalize(resources[1].name) if verb.endswith('ed'): verb = verb[:-1] ret = '{} a {} with {} being {} for a {} with {} being {}' return ret.format(verb, resource1, ParamUtils.normalize(resources[1].param.name), parameter_value1, resource2, ParamUtils.normalize(resources[1].param.name), parameter_value2)
def __replacements(resource): ret = [] ids = resource.ids r_name = resource.name if len(ids) == 1: pairs = [(r_name, ids[0])] else: pairs = [(r_name, ids[0]), (resource.param.name, ids[1]), (resource.param.name.replace(singular(r_name), ""), ids[1])] if resource.resource_type == COUNT_RESOURCE: pairs.append(("count", COUNT_RESOURCE)) elif resource.resource_type == SEARCH_RESOURCE: pairs.append(("search", SEARCH_RESOURCE)) pairs.append(("query", SEARCH_RESOURCE)) for name, id in pairs: if not name: continue names = set() for n in [ name, name.replace('get', '').replace('create', '').replace('remove', '') ]: names.add(n) names.add(n.lower()) names.add(n.replace("_", " ").lower()) names.add(ParamUtils.normalize(n)) names.add( ParamUtils.normalize(ParamUtils.normalize(n), lemmatize=True)) names.add(ParamUtils.normalize(n, lemmatize=True)) for str in combinations(ParamUtils.normalize(n).split()): names.add(str) for str in combinations( ParamUtils.normalize(n, lemmatize=True).split()): names.add(str) names = list(names) names.sort(key=lambda s: len(s), reverse=True) for name in names: ret.append((name, id)) return ret
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)
def __resource_type(previous, segment, current_tag, url): """ :return: is singleton, Collection, sub-Collection """ current = segment is_param = ParamUtils.is_param(current) if is_param: current = current[1:-1] current = ParamUtils.normalize(current) if not is_param: if current.startswith("by"): return FILTER_RESOURCE if current.startswith("search") or current.endswith("search") or current.startswith( "query") or current.endswith("query"): return SEARCH_RESOURCE if "count" == current: return COUNT_RESOURCE if "all" == current: return ALL_RESOURCE if ParamUtils.is_authentication(current): return AUTH_RESOURCE if current in {"swagger", "yaml"}: return SWAGGER_RESOURCE if current in {'pdf', 'json', 'xml', 'txt', 'doc', 'docx', 'jpeg', 'jpg', 'gif', 'png', 'xls', 'tsv', 'csv', 'fmw'}: return FILE_EXTENSION_RESOURCE if is_param and current in {"format"}: return FILE_EXTENSION_RESOURCE if is_param and previous: if current in previous: return SINGLETON if (current_tag.startswith('NNS') or is_plural(previous)) and ParamUtils.is_identifier(current): return SINGLETON if (current.endswith('name') or current.endswith('type')) and "{}.".format(segment) not in url: return SINGLETON if singular(previous) in current: return SINGLETON if editdistance.eval(current, previous) / (len(current) + len(previous)) < 0.4: return SINGLETON if current_tag.startswith('NNS') or is_plural(current): return COLLECTION if current_tag.startswith('jj') or is_adjective(current) or \ current.endswith('ed') or (current_tag.startswith('VB') and current.startswith('is')): return ATTRIBUTE_RESOURCE if (current_tag.startswith('VB') or is_verb(current)) and not is_param: return ACTION_RESOURCE words = current.split() if len(words) > 1 and is_verb(words[0]) and not is_param: return METHOD_NAME_RESOURCE if is_param: return UNKNOWN_PARAM_RESOURCE if ParamUtils.is_version(current): return VERSION_RESOURCE return UNKNOWN_RESOURCE