Пример #1
0
def create_label(input_string):
    """
    Clean up a string and create a corresponding (shortened) label.

    Parameters
    ----------
    input_string : string
        arbitrary string

    Returns
    -------
    output_string : string
        stripped input_string
    label_string : string
        alphanumeric characters of input_string

    """
    from mhdb.spreadsheet_io import return_string
    from mhdb.spreadsheet_io import convert_string_to_label

    if input_string:
        if isinstance(input_string, str):
            output_string = return_string(input_string,
                                          replace=['"', '\n'],
                                          replace_with=['', ''])
            if output_string:
                label_string = convert_string_to_label(output_string)
                return output_string, label_string
            else:
                return '', ''
        else:
            raise Exception('input_string is not a string!')
    else:
        raise Exception('input_string is None!')
Пример #2
0
def check_iri(iri, label_type='delimited'):
    """
    Function to format IRIs by type, such as <iri> or prefix:iri

    Parameters
    ---------
    iri: string

    label_type: string
        'PascalCase', 'camelCase', or 'delimited'
        ('delimited' uses '_' delimiters and keeps hyphens)

    Removed:
    prefixes: set of 2-or-3-tuples
    prefixes={("mhdb", "mhdb-states", "mhdb-disorders", "mhdb-resources",
               "mhdb-assessments", "mhdb-measures")}

    Returns
    -------
    iri: string
    """

    #prefix_strings = {"","_"} if not prefixes else {
    #    "",
    #    "_",
    #    *[prefix[0] for prefix in prefixes]
    #}

    iri = str(iri).strip()

    if ":" in iri and not [x for x in iri if x.isspace()]:
        if iri.endswith(":"):
            return check_iri(iri[:-1], label_type) #, prefixes)
        elif ":/" in iri and \
                 not iri.startswith('<') and not iri.endswith('>'):
            return "<{0}>".format(convert_string_to_label(iri, label_type))
        # elif iri.split(":")[0] in prefix_strings:
        #     return iri
        else:
            return iri
    else:
        return ":" + convert_string_to_label(iri, label_type)
Пример #3
0
def mhdb_iri(label):
    """
    Function to prepend "mhdb:" to label or string

    Parameter
    ---------
    label: string

    Returns
    -------
    iri: string
    """
    return (":".join(["mhdb", convert_string_to_label(label)]))
Пример #4
0
def write_about_statement(subject, predicate, object, predicates):
    """
    Function to write one or more rdf statements in terse triple format.

    Parameters
    ----------
    subject: string
        subject of this statement

    predicate: string
        predicate of this statement

    object: string
        object of this statement

    predicates: iterable of 2-tuples
        predicate: string
            nth property

        object: string
            nth object

    Returns
    -------
    ttl_string: string
        Turtle string

    Example
    -------
    >>> statement = {"duck": {"continues": {"sitting"}}}
    >>> predicates = {
    ...     ("source", '"Duck Duck Goose"'),
    ...     ("statementType", "role")
    ... }
    >>> for subject in statement:
    ...     for predicate in statement[subject]:
    ...         for object in statement[subject][predicate]:
    ...             print(len(write_about_statement(
    ...                 subject, predicate, object, predicates
    ...             )))
    168
    """
    return (write_ttl(
        "_:{0}".format(
            convert_string_to_label("_".join([subject, predicate, object]))),
        [("rdf:type", "rdf:Statement"), ("rdf:subject", subject),
         ("rdf:predicate", predicate), ("rdf:object", object), *predicates]))