<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
r'''
Namespace management
====================

.. autosummary::
    :toctree: generated/

    add_namespace
    schema
    is_dense
'''

import json
import os
import copy

__all__ = ['add_namespace', 'schema', 'is_dense']

__NAMESPACE__ = dict()


def add_namespace(filename):
    '''Add a namespace definition to our working set.

    Namespace files consist of partial JSON schemas defining the behavior
    of the `value` and `confidence` fields of an Annotation.

    Parameters
    ----------
    filename : str
        Path to json file defining the namespace object
    '''
    with open(filename, mode='r') as fileobj:
        __NAMESPACE__.update(json.load(fileobj))


def schema(namespace, default=None):
    '''Construct a validation schema for a given namespace.

    Parameters
    ----------
    namespace : str
        Namespace key identifier (eg, 'beat' or 'segment_tut')

    default : schema
        A pre-existing schema to append into

    Returns
    -------
    schema : dict
        JSON schema of `namespace`
    '''

    if default is None:
        default = dict(type='object', properties=dict())

    sch = copy.deepcopy(default)

    for key in ['value', 'confidence']:
        try:
            sch['properties'][key] = __NAMESPACE__[namespace][key]
        except KeyError:
            pass

    return sch


def is_dense(namespace):
    '''Determine whether a namespace has dense formatting.

    Parameters
    ----------
    namespace : str
        Namespace key identifier

    Returns
    -------
    dense : bool
        True if `namespace` has a dense packing
        False otherwise.
    '''

    return __NAMESPACE__[namespace]['dense']


# Populate the schemata
SCHEMA_DIR = os.path.join('schema', 'namespaces')<commit_msg>added namespace checks, fixes #47<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
r'''
Namespace management
====================

.. autosummary::
    :toctree: generated/

    add_namespace
    schema
    is_dense
'''

import json
import os
import copy

from .exceptions import NamespaceError

__all__ = ['add_namespace', 'schema', 'is_dense']

__NAMESPACE__ = dict()


def add_namespace(filename):
    '''Add a namespace definition to our working set.

    Namespace files consist of partial JSON schemas defining the behavior
    of the `value` and `confidence` fields of an Annotation.

    Parameters
    ----------
    filename : str
        Path to json file defining the namespace object
    '''
    with open(filename, mode='r') as fileobj:
        __NAMESPACE__.update(json.load(fileobj))


def schema(namespace, default=None):
    '''Construct a validation schema for a given namespace.

    Parameters
    ----------
    namespace : str
        Namespace key identifier (eg, 'beat' or 'segment_tut')

    default : schema
        A pre-existing schema to append into

    Returns
    -------
    schema : dict
        JSON schema of `namespace`
    '''

    if namespace not in __NAMESPACE__:
        raise NamespaceError('Unknown namespace: {:s}'.format(namespace))

    if default is None:
        default = dict(type='object', properties=dict())

    sch = copy.deepcopy(default)

    for key in ['value', 'confidence']:
        try:
            sch['properties'][key] = __NAMESPACE__[namespace][key]
        except KeyError:
            pass

    return sch


def is_dense(namespace):
    '''Determine whether a namespace has dense formatting.

    Parameters
    ----------
    namespace : str
        Namespace key identifier

    Returns
    -------
    dense : bool
        True if `namespace` has a dense packing
        False otherwise.
    '''

    if namespace not in __NAMESPACE__:
        raise NamespaceError('Unknown namespace: {:s}'.format(namespace))

    return __NAMESPACE__[namespace]['dense']


# Populate the schemata
SCHEMA_DIR = os.path.join('schema', 'namespaces')
{'fraction_non_alphanumeric': 0.12354134956874682, 'fraction_numerical': 0.0010147133434804667, 'mean_word_length': 2.536322869955157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5138938', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1048, 'n_words': 353}