Python inflection.singularize() Examples

The following are 6 code examples of inflection.singularize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module inflection , or try the search function .
Example #1
Source File: relations.py    From django-rest-framework-json-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_resource_type_from_included_serializer(self):
        """
        Check to see it this resource has a different resource_name when
        included and return that name, or None
        """
        field_name = self.field_name or self.parent.field_name
        parent = self.get_parent_serializer()

        if parent is not None:
            # accept both singular and plural versions of field_name
            field_names = [
                inflection.singularize(field_name),
                inflection.pluralize(field_name)
            ]
            includes = get_included_serializers(parent)
            for field in field_names:
                if field in includes.keys():
                    return get_resource_type_from_serializer(includes[field])

        return None 
Example #2
Source File: preprocessors.py    From recaptcha-cracker with GNU General Public License v3.0 5 votes vote down vote up
def depluralise_string(string):
        singular = inflection.singularize(string)
        return singular 
Example #3
Source File: item_rules.py    From combine-FEVER-NSMN with MIT License 5 votes vote down vote up
def singularize_rule(self):
        """Singularize words
        """
        item = self.item
        if len(item['prioritized_docids']) < 1:
            claim_tokens = item['claim_tokens']
            # finded_keys  = item['prioritized_docids']
            claim_tokens = [inflection.singularize(c) for c in claim_tokens]
            claim = ' '.join(claim_tokens)
            fk_new = self._keyword_match(claim)
            # finded_keys = set(finded_keys) | set(fk_new)
            item['prioritized_docids'] = list(fk_new)
        return self 
Example #4
Source File: item_rules.py    From combine-FEVER-NSMN with MIT License 5 votes vote down vote up
def singularize_rule(self):
        item = self.item
        if len(item['prioritized_docids']) < 1:
            claim_tokens = item['claim_tokens']
            # finded_keys  = item['prioritized_docids']
            claim_tokens = [inflection.singularize(c) for c in claim_tokens]
            claim = ' '.join(claim_tokens)
            fkd_new, fk_new = self._keyword_match(claim, raw_set=True)
            # finded_keys = set(finded_keys) | set(fk_new)
            item['prioritized_docids'] = list(fk_new)
            item['structured_docids'] = fkd_new
        return self 
Example #5
Source File: utils.py    From ramses with Apache License 2.0 5 votes vote down vote up
def generate_model_name(raml_resource):
    """ Generate model name.

    :param raml_resource: Instance of ramlfications.raml.ResourceNode.
    """
    resource_uri = get_resource_uri(raml_resource).strip('/')
    resource_uri = re.sub('\W', ' ', resource_uri)
    model_name = inflection.titleize(resource_uri)
    return inflection.singularize(model_name).replace(' ', '') 
Example #6
Source File: get.py    From grest with GNU General Public License v3.0 4 votes vote down vote up
def get(self, primary_id, secondary_model_name=None, secondary_id=None):
    try:
        # patch __log
        self.__log = self._GRest__log

        (primary, secondary) = validate_models(self,
                                               primary_id,
                                               secondary_model_name,
                                               secondary_id)

        primary_selected_item = primary.model.nodes.get_or_none(
            **{primary.selection_field: primary.id})

        if all([primary_selected_item,
                secondary.model,
                secondary.id]):
            # user selected a nested model with 2 keys
            # (from the primary and secondary models)
            # /users/user_id/roles/role_id -> selected role of this user
            # /categories/cat_id/tags/tag_id -> selected tag of this category

            # In this example, the p variable of type Post
            # is the secondary_item
            # (u:User)-[:POSTED]-(p:Post)
            secondary_item = primary_selected_item.get_all(
                secondary.model_name,
                secondary.selection_field,
                secondary.id,
                retrieve_relations=True)

            return serialize({singularize(secondary.model_name):
                              secondary_item})
        elif all([primary_selected_item, secondary.model]):
            # user selected a nested model with primary key
            # (from the primary and the secondary models)
            # /users/user_1/roles -> all roles for this user
            relationships = primary_selected_item.get_all(
                secondary.model_name,
                retrieve_relations=True)
            return serialize({pluralize(secondary.model_name):
                              relationships})
        else:
            # user selected a single item (from the primary model)
            if primary_selected_item:
                return serialize({primary.model_name:
                                  primary_selected_item.to_dict()})
            else:
                raise HTTPException(msg.MODEL_DOES_NOT_EXIST.format(
                    model=primary.model_name), 404)
    except (DoesNotExist, AttributeError) as e:
        self.__log.exception(e)
        raise HTTPException(msg.ITEM_DOES_NOT_EXIST, 404)