signature:
def cube ( width , height , depth , center = ( 0.0 , 0.0 , 0.0 ) , normals = True , uvs = True ) -> VAO :
    """Creates a cube VAO with normals and texture coordinates
 Args :
 width ( float ) : Width of the cube
 height ( float ) : Height of the cube
 depth ( float ) : Depth of the cube
 Keyword Args :
 center : center of the cube as a 3 - component tuple
 normals : ( bool ) Include normals
 uvs : ( bool ) include uv coordinates
 Returns :
 A : py : class : ` demosys . opengl . vao . VAO ` instance"""

implementation:
width , height , depth = width / 2.0 , height / 2.0 , depth / 2.0
pos = numpy . array ( [ center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] - height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] - depth , center [ 0 ] - width , center [ 1 ] + height , center [ 2 ] + depth , center [ 0 ] + width , center [ 1 ] + height , center [ 2 ] + depth , ] , dtype = numpy . float32 )
if normals :
    normal_data = numpy . array ( [ - 0 , 0 , 1 , - 0 , 0 , 1 , - 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , - 1 , - 0 , 0 , - 1 , - 0 , 0 , - 1 , - 0 , 0 , - 1 , - 0 , 0 , - 1 , - 0 , 0 , - 1 , - 0 , 0 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 0 , - 1 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , ] , dtype = numpy . float32 )
if uvs :
    uvs_data = numpy . array ( [ 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 ] , dtype = numpy . float32 )
vao = VAO ( "geometry:cube" )
# Add buffers
vao . buffer ( pos , '3f' , [ 'in_position' ] )
if normals :
    vao . buffer ( normal_data , '3f' , [ 'in_normal' ] )
if uvs :
    vao . buffer ( uvs_data , '2f' , [ 'in_uv' ] )
return vao


signature:
def draw ( self , mesh , projection_matrix = None , view_matrix = None , camera_matrix = None , time = 0 ) :
    """Draw code for the mesh . Should be overriden .
 : param projection _ matrix : projection _ matrix ( bytes )
 : param view _ matrix : view _ matrix ( bytes )
 : param camera _ matrix : camera _ matrix ( bytes )
 : param time : The current time"""

implementation:
self . program [ "m_proj" ] . write ( projection_matrix )
self . program [ "m_mv" ] . write ( view_matrix )
mesh . vao . render ( self . program )


signature:
def pause ( self ) :
    """Pause the music"""

implementation:
self . pause_time = self . get_time ( )
self . paused = True
self . player . pause ( )


signature:
def get_time ( self ) -> float :
    """Get the current time in seconds
 Returns :
 The current time in seconds"""

implementation:
if self . paused :
    return self . pause_time
return self . player . get_time ( ) / 1000.0


signature:
def parse_package_string ( path ) :
    """Parse the effect package string .
 Can contain the package python path or path to effect class in an effect package .
 Examples : :
 # Path to effect pacakge
 examples . cubes
 # Path to effect class
 examples . cubes . Cubes
 Args :
 path : python path to effect package . May also include effect class name .
 Returns :
 tuple : ( package _ path , effect _ class )"""

implementation:
parts = path . split ( '.' )
# Is the last entry in the path capitalized ?
if parts [ - 1 ] [ 0 ] . isupper ( ) :
    return "." . join ( parts [ : - 1 ] ) , parts [ - 1 ]
return path , ""


signature:
def get_dirs ( self ) -> List [ str ] :
    """Get all effect directories for registered effects ."""

implementation:
for package in self . packages :
    yield os . path . join ( package . path , 'resources' )


signature:
def get_effect_resources ( self ) -> List [ Any ] :
    """Get all resources registed in effect packages .
 These are typically located in ` ` resources . py ` `"""

implementation:
resources = [ ]
for package in self . packages :
    resources . extend ( package . resources )
return resources


signature:
def add_package ( self , name ) :
    """Registers a single package
 : param name : ( str ) The effect package to add"""

implementation:
name , cls_name = parse_package_string ( name )
if name in self . package_map :
    return
package = EffectPackage ( name )
package . load ( )
self . packages . append ( package )
self . package_map [ package . name ] = package
# Load effect package dependencies
self . polulate ( package . effect_packages )


signature:
def get_package ( self , name ) -> 'EffectPackage' :
    """Get a package by python path . Can also contain path to an effect .
 Args :
 name ( str ) : Path to effect package or effect
 Returns :
 The requested EffectPackage
 Raises :
 EffectError when no package is found"""

implementation:
name , cls_name = parse_package_string ( name )
try :
    return self . package_map [ name ]
except KeyError :
    raise EffectError ( "No package '{}' registered" . format ( name ) )


signature:
def find_effect_class ( self , path ) -> Type [ Effect ] :
    """Find an effect class by class name or full python path to class
 Args :
 path ( str ) : effect class name or full python path to effect class
 Returns :
 Effect class
 Raises :
 EffectError if no class is found"""

implementation:
package_name , class_name = parse_package_string ( path )
if package_name :
    package = self . get_package ( package_name )
    return package . find_effect_class ( class_name , raise_for_error = True )
for package in self . packages :
    effect_cls = package . find_effect_class ( class_name )
    if effect_cls :
        return effect_cls
raise EffectError ( "No effect class '{}' found in any packages" . format ( class_name ) )


signature:
def runnable_effects ( self ) -> List [ Type [ Effect ] ] :
    """Returns the runnable effect in the package"""

implementation: return [ cls for cls in self . effect_classes if cls . runnable ]


signature:
def load_package ( self ) :
    """FInd the effect package"""

implementation:
try :
    self . package = importlib . import_module ( self . name )
except ModuleNotFoundError :
    raise ModuleNotFoundError ( "Effect package '{}' not found." . format ( self . name ) )


signature:
def load_effects_classes ( self ) :
    """Iterate the module attributes picking out effects"""

implementation:
self . effect_classes = [ ]
for _ , cls in inspect . getmembers ( self . effect_module ) :
    if inspect . isclass ( cls ) :
        if cls == Effect :
            continue
        if issubclass ( cls , Effect ) :
            self . effect_classes . append ( cls )
            self . effect_class_map [ cls . __name__ ] = cls
            cls . _name = "{}.{}" . format ( self . effect_module_name , cls . __name__ )


signature:
def load_resource_module ( self ) :
    """Fetch the resource list"""

implementation:
# Attempt to load the dependencies module
try :
    name = '{}.{}' . format ( self . name , 'dependencies' )
    self . dependencies_module = importlib . import_module ( name )
except ModuleNotFoundError as err :
    raise EffectError ( ( "Effect package '{}' has no 'dependencies' module or the module has errors. " "Forwarded error from importlib: {}" ) . format ( self . name , err ) )
# Fetch the resource descriptions
try :
    self . resources = getattr ( self . dependencies_module , 'resources' )
except AttributeError :
    raise EffectError ( "Effect dependencies module '{}' has no 'resources' attribute" . format ( name ) )
if not isinstance ( self . resources , list ) :
    raise EffectError ( "Effect dependencies module '{}': 'resources' is of type {} instead of a list" . format ( name , type ( self . resources ) ) )
# Fetch the effect class list
try :
    self . effect_packages = getattr ( self . dependencies_module , 'effect_packages' )
except AttributeError :
    raise EffectError ( "Effect dependencies module '{}' has 'effect_packages' attribute" . format ( name ) )
if not isinstance ( self . effect_packages , list ) :
    raise EffectError ( "Effect dependencies module '{}': 'effect_packages' is of type {} instead of a list" . format ( name , type ( self . effects ) ) )


signature:
def create ( file_format = 'png' , name = None ) :
    """Create a screenshot
 : param file _ format : formats supported by PIL ( png , jpeg etc )"""

implementation:
dest = ""
if settings . SCREENSHOT_PATH :
    if not os . path . exists ( settings . SCREENSHOT_PATH ) :
        print ( "SCREENSHOT_PATH does not exist. creating: {}" . format ( settings . SCREENSHOT_PATH ) )
        os . makedirs ( settings . SCREENSHOT_PATH )
    dest = settings . SCREENSHOT_PATH
else :
    print ( "SCREENSHOT_PATH not defined in settings. Using cwd as fallback." )
if not Config . target :
    Config . target = context . window ( ) . fbo
image = Image . frombytes ( "RGB" , ( Config . target . viewport [ 2 ] , Config . target . viewport [ 3 ] ) , Config . target . read ( viewport = Config . target . viewport , alignment = Config . alignment ) , )
image = image . transpose ( Image . FLIP_TOP_BOTTOM )
if not name :
    name = "{}.{}" . format ( datetime . now ( ) . strftime ( "%Y-%m-%d-%H-%M-%S-%f" ) , file_format )
dest = os . path . join ( dest , name )
print ( "Creating screenshot:" , dest )
image . save ( dest , format = file_format )


signature:
def draw ( self , time , frametime , target ) :
    """Fetch track value for every runnable effect .
 If the value is > 0.5 we draw it ."""

implementation:
for effect in self . effects :
    value = effect . rocket_timeline_track . time_value ( time )
    if value > 0.5 :
        effect . draw ( time , frametime , target )


signature:
def load ( self ) :
    """Load a 2d texture"""

implementation:
self . _open_image ( )
components , data = image_data ( self . image )
texture = self . ctx . texture ( self . image . size , components , data , )
texture . extra = { 'meta' : self . meta }
if self . meta . mipmap :
    texture . build_mipmaps ( )
self . _close_image ( )
return texture


signature:
def from_single ( cls , meta : ProgramDescription , source : str ) :
    """Initialize a single glsl string containing all shaders"""

implementation:
instance = cls ( meta )
instance . vertex_source = ShaderSource ( VERTEX_SHADER , meta . path or meta . vertex_shader , source )
if GEOMETRY_SHADER in source :
    instance . geometry_source = ShaderSource ( GEOMETRY_SHADER , meta . path or meta . geometry_shader , source , )
if FRAGMENT_SHADER in source :
    instance . fragment_source = ShaderSource ( FRAGMENT_SHADER , meta . path or meta . fragment_shader , source , )
if TESS_CONTROL_SHADER in source :
    instance . tess_control_source = ShaderSource ( TESS_CONTROL_SHADER , meta . path or meta . tess_control_shader , source , )
if TESS_EVALUATION_SHADER in source :
    instance . tess_evaluation_source = ShaderSource ( TESS_EVALUATION_SHADER , meta . path or meta . tess_evaluation_shader , source , )
return instance


signature:
def from_separate ( cls , meta : ProgramDescription , vertex_source , geometry_source = None , fragment_source = None , tess_control_source = None , tess_evaluation_source = None ) :
    """Initialize multiple shader strings"""

implementation:
instance = cls ( meta )
instance . vertex_source = ShaderSource ( VERTEX_SHADER , meta . path or meta . vertex_shader , vertex_source , )
if geometry_source :
    instance . geometry_source = ShaderSource ( GEOMETRY_SHADER , meta . path or meta . geometry_shader , geometry_source , )
if fragment_source :
    instance . fragment_source = ShaderSource ( FRAGMENT_SHADER , meta . path or meta . fragment_shader , fragment_source , )
if tess_control_source :
    instance . tess_control_source = ShaderSource ( TESS_CONTROL_SHADER , meta . path or meta . tess_control_shader , tess_control_source , )
if tess_evaluation_source :
    instance . tess_evaluation_source = ShaderSource ( TESS_EVALUATION_SHADER , meta . path or meta . tess_control_shader , tess_evaluation_source , )
return instance


signature:
def create ( self ) :
    """Creates a shader program .
 Returns :
 ModernGL Program instance"""

implementation:
# Get out varyings
out_attribs = [ ]
# If no fragment shader is present we are doing transform feedback
if not self . fragment_source : # Out attributes is present in geometry shader if present
    if self . geometry_source :
        out_attribs = self . geometry_source . find_out_attribs ( )
    # Otherwise they are specified in vertex shader
    else :
        out_attribs = self . vertex_source . find_out_attribs ( )
program = self . ctx . program ( vertex_shader = self . vertex_source . source , geometry_shader = self . geometry_source . source if self . geometry_source else None , fragment_shader = self . fragment_source . source if self . fragment_source else None , tess_control_shader = self . tess_control_source . source if self . tess_control_source else None , tess_evaluation_shader = self . tess_evaluation_source . source if self . tess_evaluation_source else None , varyings = out_attribs , )
program . extra = { 'meta' : self . meta }
return program


signature:
def find_out_attribs ( self ) :
    """Get all out attributes in the shader source .
 : return : List of attribute names"""

implementation:
names = [ ]
for line in self . lines :
    if line . strip ( ) . startswith ( "out " ) :
        names . append ( line . split ( ) [ 2 ] . replace ( ';' , '' ) )
return names


signature:
def print ( self ) :
    """Print the shader lines"""

implementation:
print ( "---[ START {} ]---" . format ( self . name ) )
for i , line in enumerate ( self . lines ) :
    print ( "{}: {}" . format ( str ( i ) . zfill ( 3 ) , line ) )
print ( "---[ END {} ]---" . format ( self . name ) )


signature:
def create_effect ( self , label : str , name : str , * args , ** kwargs ) -> Effect :
    """Create an effect instance adding it to the internal effects dictionary using the label as key .
 Args :
 label ( str ) : The unique label for the effect instance
 name ( str ) : Name or full python path to the effect class we want to instantiate
 args : Positional arguments to the effect initializer
 kwargs : Keyword arguments to the effect initializer
 Returns :
 The newly created Effect instance"""

implementation:
effect_cls = effects . find_effect_class ( name )
effect = effect_cls ( * args , ** kwargs )
effect . _label = label
if label in self . _effects :
    raise ValueError ( "An effect with label '{}' already exists" . format ( label ) )
self . _effects [ label ] = effect
return effect


signature:
def load ( self ) :
    """Loads this project instance"""

implementation:
self . create_effect_classes ( )
self . _add_resource_descriptions_to_pools ( self . create_external_resources ( ) )
self . _add_resource_descriptions_to_pools ( self . create_resources ( ) )
for meta , resource in resources . textures . load_pool ( ) :
    self . _textures [ meta . label ] = resource
for meta , resource in resources . programs . load_pool ( ) :
    self . _programs [ meta . label ] = resource
for meta , resource in resources . scenes . load_pool ( ) :
    self . _scenes [ meta . label ] = resource
for meta , resource in resources . data . load_pool ( ) :
    self . _data [ meta . label ] = resource
self . create_effect_instances ( )
self . post_load ( )


signature:
def _add_resource_descriptions_to_pools ( self , meta_list ) :
    """Takes a list of resource descriptions adding them
 to the resource pool they belong to scheduling them for loading ."""

implementation:
if not meta_list :
    return
for meta in meta_list :
    getattr ( resources , meta . resource_type ) . add ( meta )


signature:
def reload_programs ( self ) :
    """Reload all shader programs with the reloadable flag set"""

implementation:
print ( "Reloading programs:" )
for name , program in self . _programs . items ( ) :
    if getattr ( program , 'program' , None ) :
        print ( " - {}" . format ( program . meta . label ) )
        program . program = resources . programs . load ( program . meta )


signature:
def get_effect ( self , label : str ) -> Effect :
    """Get an effect instance by label
 Args :
 label ( str ) : The label for the effect instance
 Returns :
 Effect class instance"""

implementation: return self . _get_resource ( label , self . _effects , "effect" )


signature:
def get_effect_class ( self , class_name , package_name = None ) -> Type [ Effect ] :
    """Get an effect class from the effect registry .
 Args :
 class _ name ( str ) : The exact class name of the effect
 Keyword Args :
 package _ name ( str ) : The python path to the effect package the effect name is located .
 This is optional and can be used to avoid issue with class name collisions .
 Returns :
 Effect class"""

implementation:
if package_name :
    return effects . find_effect_class ( "{}.{}" . format ( package_name , class_name ) )
return effects . find_effect_class ( class_name )


signature:
def get_scene ( self , label : str ) -> Scene :
    """Gets a scene by label
 Args :
 label ( str ) : The label for the scene to fetch
 Returns :
 Scene instance"""

implementation: return self . _get_resource ( label , self . _scenes , "scene" )


signature:
def get_texture ( self , label : str ) -> Union [ moderngl . Texture , moderngl . TextureArray , moderngl . Texture3D , moderngl . TextureCube ] :
    """Get a texture by label
 Args :
 label ( str ) : The label for the texture to fetch
 Returns :
 Texture instance"""

implementation: return self . _get_resource ( label , self . _textures , "texture" )


signature:
def get_data ( self , label : str ) -> Any :
    """Get a data resource by label
 Args :
 label ( str ) : The labvel for the data resource to fetch
 Returns :
 The requeted data object"""

implementation: return self . _get_resource ( label , self . _data , "data" )


signature:
def _get_resource ( self , label : str , source : dict , resource_type : str ) :
    """Generic resoure fetcher handling errors .
 Args :
 label ( str ) : The label to fetch
 source ( dict ) : The dictionary to look up the label
 resource _ type str : The display name of the resource type ( used in errors )"""

implementation:
try :
    return source [ label ]
except KeyError :
    raise ValueError ( "Cannot find {0} with label '{1}'.\nExisting {0} labels: {2}" . format ( resource_type , label , list ( source . keys ( ) ) ) )


signature:
def get_runnable_effects ( self ) -> List [ Effect ] :
    """Returns all runnable effects in the project .
 : return : List of all runnable effects"""

implementation: return [ effect for name , effect in self . _effects . items ( ) if effect . runnable ]


signature:
def image_data ( image ) :
    """Get components and bytes for an image"""

implementation:
# NOTE : We might want to check the actual image . mode
# and convert to an acceptable format .
# At the moment we load the data as is .
data = image . tobytes ( )
components = len ( data ) // ( image . size [ 0 ] * image . size [ 1 ] )
return components , data


signature:
def run_from_argv ( self , argv ) :
    """Called by the system when executing the command from the command line .
 This should not be overridden .
 : param argv : Arguments from command line"""

implementation:
parser = self . create_parser ( argv [ 0 ] , argv [ 1 ] )
options = parser . parse_args ( argv [ 2 : ] )
cmd_options = vars ( options )
args = cmd_options . pop ( 'args' , ( ) )
self . handle ( * args , ** cmd_options )


signature:
def create_parser ( self , prog_name , subcommand ) :
    """Create argument parser and deal with ` ` add _ arguments ` ` .
 This method should not be overriden .
 : param prog _ name : Name of the command ( argv [ 0 ] )
 : return : ArgumentParser"""

implementation:
parser = argparse . ArgumentParser ( prog_name , subcommand )
# Add generic arguments here
self . add_arguments ( parser )
return parser


signature:
def validate_name ( self , name ) :
    """Can the name be used as a python module or package ?
 Raises ` ` ValueError ` ` if the name is invalid .
 : param name : the name to check"""

implementation:
if not name :
    raise ValueError ( "Name cannot be empty" )
# Can the name be used as an identifier in python ( module or package name )
if not name . isidentifier ( ) :
    raise ValueError ( "{} is not a valid identifier" . format ( name ) )


signature:
def bbox ( width = 1.0 , height = 1.0 , depth = 1.0 ) :
    """Generates a bounding box with ( 0.0 , 0.0 , 0.0 ) as the center .
 This is simply a box with ` ` LINE _ STRIP ` ` as draw mode .
 Keyword Args :
 width ( float ) : Width of the box
 height ( float ) : Height of the box
 depth ( float ) : Depth of the box
 Returns :
 A : py : class : ` demosys . opengl . vao . VAO ` instance"""

implementation:
width , height , depth = width / 2.0 , height / 2.0 , depth / 2.0
pos = numpy . array ( [ width , - height , depth , width , height , depth , - width , - height , depth , width , height , depth , - width , height , depth , - width , - height , depth , width , - height , - depth , width , height , - depth , width , - height , depth , width , height , - depth , width , height , depth , width , - height , depth , width , - height , - depth , width , - height , depth , - width , - height , depth , width , - height , - depth , - width , - height , depth , - width , - height , - depth , - width , - height , depth , - width , height , depth , - width , height , - depth , - width , - height , depth , - width , height , - depth , - width , - height , - depth , width , height , - depth , width , - height , - depth , - width , - height , - depth , width , height , - depth , - width , - height , - depth , - width , height , - depth , width , height , - depth , - width , height , - depth , width , height , depth , - width , height , - depth , - width , height , depth , width , height , depth , ] , dtype = numpy . float32 )
vao = VAO ( "geometry:cube" , mode = moderngl . LINE_STRIP )
vao . buffer ( pos , '3f' , [ "in_position" ] )
return vao


signature:
def _find_last_of ( self , path , finders ) :
    """Find the last occurance of the file in finders"""

implementation:
found_path = None
for finder in finders :
    result = finder . find ( path )
    if result :
        found_path = result
return found_path


signature:
def initial_sanity_check ( self ) :
    """Checks if we can create the project"""

implementation:
# Check for python module collision
self . try_import ( self . project_name )
# Is the name a valid identifier ?
self . validate_name ( self . project_name )
# Make sure we don ' t mess with existing directories
if os . path . exists ( self . project_name ) :
    print ( "Directory {} already exist. Aborting." . format ( self . project_name ) )
    return False
if os . path . exists ( 'manage.py' ) :
    print ( "A manage.py file already exist in the current directory. Aborting." )
    return False
return True


signature:
def create_entrypoint ( self ) :
    """Write manage . py in the current directory"""

implementation:
with open ( os . path . join ( self . template_dir , 'manage.py' ) , 'r' ) as fd :
    data = fd . read ( ) . format ( project_name = self . project_name )
with open ( 'manage.py' , 'w' ) as fd :
    fd . write ( data )
os . chmod ( 'manage.py' , 0o777 )


signature:
def get_template_dir ( self ) :
    """Returns the absolute path to template directory"""

implementation:
directory = os . path . dirname ( os . path . abspath ( __file__ ) )
directory = os . path . dirname ( os . path . dirname ( directory ) )
directory = os . path . join ( directory , 'project_template' )
return directory


signature:
def resolve_loader ( self , meta : ProgramDescription ) :
    """Resolve program loader"""

implementation:
if not meta . loader :
    meta . loader = 'single' if meta . path else 'separate'
for loader_cls in self . _loaders :
    if loader_cls . name == meta . loader :
        meta . loader_cls = loader_cls
        break
else :
    raise ImproperlyConfigured ( ( "Program {} has no loader class registered." "Check PROGRAM_LOADERS or PROGRAM_DIRS" ) . format ( meta . path ) )


signature:
def davidson ( lname , fname = '.' , omit_fname = False ) :
    """Return Davidson ' s Consonant Code .
 This is a wrapper for : py : meth : ` Davidson . encode ` .
 Parameters
 lname : str
 Last name ( or word ) to be encoded
 fname : str
 First name ( optional ) , of which the first character is included in the
 code .
 omit _ fname : bool
 Set to True to completely omit the first character of the first name
 Returns
 str
 Davidson ' s Consonant Code
 Example
 > > > davidson ( ' Gough ' )
 > > > davidson ( ' pneuma ' )
 ' PNM . '
 > > > davidson ( ' knight ' )
 ' KNGT . '
 > > > davidson ( ' trice ' )
 ' TRC . '
 > > > davidson ( ' judge ' )
 ' JDG . '
 > > > davidson ( ' Smith ' , ' James ' )
 ' SMT J '
 > > > davidson ( ' Wasserman ' , ' Tabitha ' )
 ' WSRMT '"""

implementation: return Davidson ( ) . encode ( lname , fname , omit_fname )


signature:
def encode ( self , lname , fname = '.' , omit_fname = False ) :
    """Return Davidson ' s Consonant Code .
 Parameters
 lname : str
 Last name ( or word ) to be encoded
 fname : str
 First name ( optional ) , of which the first character is included in
 the code .
 omit _ fname : bool
 Set to True to completely omit the first character of the first
 name
 Returns
 str
 Davidson ' s Consonant Code
 Example
 > > > pe = Davidson ( )
 > > > pe . encode ( ' Gough ' )
 > > > pe . encode ( ' pneuma ' )
 ' PNM . '
 > > > pe . encode ( ' knight ' )
 ' KNGT . '
 > > > pe . encode ( ' trice ' )
 ' TRC . '
 > > > pe . encode ( ' judge ' )
 ' JDG . '
 > > > pe . encode ( ' Smith ' , ' James ' )
 ' SMT J '
 > > > pe . encode ( ' Wasserman ' , ' Tabitha ' )
 ' WSRMT '"""

implementation:
lname = text_type ( lname . upper ( ) )
code = self . _delete_consecutive_repeats ( lname [ : 1 ] + lname [ 1 : ] . translate ( self . _trans ) )
code = code [ : 4 ] + ( 4 - len ( code ) ) * ' '
if not omit_fname :
    code += fname [ : 1 ] . upper ( )
return code


signature:
def ac_encode ( text , probs ) :
    """Encode a text using arithmetic coding with the provided probabilities .
 This is a wrapper for : py : meth : ` Arithmetic . encode ` .
 Parameters
 text : str
 A string to encode
 probs : dict
 A probability statistics dictionary generated by
 : py : meth : ` Arithmetic . train `
 Returns
 tuple
 The arithmetically coded text
 Example
 > > > pr = ac _ train ( ' the quick brown fox jumped over the lazy dog ' )
 > > > ac _ encode ( ' align ' , pr )
 (16720586181 , 34)"""

implementation:
coder = Arithmetic ( )
coder . set_probs ( probs )
return coder . encode ( text )


signature:
def ac_decode ( longval , nbits , probs ) :
    """Decode the number to a string using the given statistics .
 This is a wrapper for : py : meth : ` Arithmetic . decode ` .
 Parameters
 longval : int
 The first part of an encoded tuple from ac _ encode
 nbits : int
 The second part of an encoded tuple from ac _ encode
 probs : dict
 A probability statistics dictionary generated by
 : py : meth : ` Arithmetic . train `
 Returns
 str
 The arithmetically decoded text
 Example
 > > > pr = ac _ train ( ' the quick brown fox jumped over the lazy dog ' )
 > > > ac _ decode ( 16720586181 , 34 , pr )
 ' align '"""

implementation:
coder = Arithmetic ( )
coder . set_probs ( probs )
return coder . decode ( longval , nbits )


signature:
def train ( self , text ) :
    r"""Generate a probability dict from the provided text .
 Text to 0 - order probability statistics as a dict
 Parameters
 text : str
 The text data over which to calculate probability statistics . This
 must not contain the NUL ( 0x00 ) character because that is used to
 indicate the end of data .
 Example
 > > > ac = Arithmetic ( )
 > > > ac . train ( ' the quick brown fox jumped over the lazy dog ' )
 > > > ac . get _ probs ( )
 { ' ' : ( Fraction ( 0 , 1 ) , Fraction ( 8 , 45 ) ) ,
 ' o ' : ( Fraction ( 8 , 45 ) , Fraction ( 4 , 15 ) ) ,
 ' e ' : ( Fraction ( 4 , 15 ) , Fraction ( 16 , 45 ) ) ,
 ' u ' : ( Fraction ( 16 , 45 ) , Fraction ( 2 , 5 ) ) ,
 ' t ' : ( Fraction ( 2 , 5 ) , Fraction ( 4 , 9 ) ) ,
 ' r ' : ( Fraction ( 4 , 9 ) , Fraction ( 22 , 45 ) ) ,
 ' h ' : ( Fraction ( 22 , 45 ) , Fraction ( 8 , 15 ) ) ,
 ' d ' : ( Fraction ( 8 , 15 ) , Fraction ( 26 , 45 ) ) ,
 ' z ' : ( Fraction ( 26 , 45 ) , Fraction ( 3 , 5 ) ) ,
 ' y ' : ( Fraction ( 3 , 5 ) , Fraction ( 28 , 45 ) ) ,
 ' x ' : ( Fraction ( 28 , 45 ) , Fraction ( 29 , 45 ) ) ,
 ' w ' : ( Fraction ( 29 , 45 ) , Fraction ( 2 , 3 ) ) ,
 ' v ' : ( Fraction ( 2 , 3 ) , Fraction ( 31 , 45 ) ) ,
 ' q ' : ( Fraction ( 31 , 45 ) , Fraction ( 32 , 45 ) ) ,
 ' p ' : ( Fraction ( 32 , 45 ) , Fraction ( 11 , 15 ) ) ,
 ' n ' : ( Fraction ( 11 , 15 ) , Fraction ( 34 , 45 ) ) ,
 ' m ' : ( Fraction ( 34 , 45 ) , Fraction ( 7 , 9 ) ) ,
 ' l ' : ( Fraction ( 7 , 9 ) , Fraction ( 4 , 5 ) ) ,
 ' k ' : ( Fraction ( 4 , 5 ) , Fraction ( 37 , 45 ) ) ,
 ' j ' : ( Fraction ( 37 , 45 ) , Fraction ( 38 , 45 ) ) ,
 ' i ' : ( Fraction ( 38 , 45 ) , Fraction ( 13 , 15 ) ) ,
 ' g ' : ( Fraction ( 13 , 15 ) , Fraction ( 8 , 9 ) ) ,
 ' f ' : ( Fraction ( 8 , 9 ) , Fraction ( 41 , 45 ) ) ,
 ' c ' : ( Fraction ( 41 , 45 ) , Fraction ( 14 , 15 ) ) ,
 ' b ' : ( Fraction ( 14 , 15 ) , Fraction ( 43 , 45 ) ) ,
 ' a ' : ( Fraction ( 43 , 45 ) , Fraction ( 44 , 45 ) ) ,
 ' \ x00 ' : ( Fraction ( 44 , 45 ) , Fraction ( 1 , 1 ) ) }"""

implementation:
text = text_type ( text )
if '\x00' in text :
    text = text . replace ( '\x00' , ' ' )
counts = Counter ( text )
counts [ '\x00' ] = 1
tot_letters = sum ( counts . values ( ) )
tot = 0
self . _probs = { }
prev = Fraction ( 0 )
for char , count in sorted ( counts . items ( ) , key = lambda x : ( x [ 1 ] , x [ 0 ] ) , reverse = True ) :
    follow = Fraction ( tot + count , tot_letters )
    self . _probs [ char ] = ( prev , follow )
    prev = follow
    tot = tot + count


signature:
def encode ( self , text ) :
    """Encode a text using arithmetic coding .
 Text and the 0 - order probability statistics - > longval , nbits
 The encoded number is Fraction ( longval , 2 * * nbits )
 Parameters
 text : str
 A string to encode
 Returns
 tuple
 The arithmetically coded text
 Example
 > > > ac = Arithmetic ( ' the quick brown fox jumped over the lazy dog ' )
 > > > ac . encode ( ' align ' )
 (16720586181 , 34)"""

implementation:
text = text_type ( text )
if '\x00' in text :
    text = text . replace ( '\x00' , ' ' )
minval = Fraction ( 0 )
maxval = Fraction ( 1 )
for char in text + '\x00' :
    prob_range = self . _probs [ char ]
    delta = maxval - minval
    maxval = minval + prob_range [ 1 ] * delta
    minval = minval + prob_range [ 0 ] * delta
# I tried without the / 2 just to check . Doesn ' t work .
# Keep scaling up until the error range is > = 1 . That
# gives me the minimum number of bits needed to resolve
# down to the end - of - data character .
delta = ( maxval - minval ) / 2
nbits = long ( 0 )
while delta < 1 :
    nbits += 1
    delta *= 2
# The below condition shouldn ' t ever be false
if nbits == 0 : # pragma : no cover
    return 0 , 0
# using - 1 instead of / 2
avg = ( maxval + minval ) * 2 ** ( nbits - 1 )
# Could return a rational instead . . .
# the division truncation is deliberate
return avg . numerator // avg . denominator , nbits


signature:
def decode ( self , longval , nbits ) :
    """Decode the number to a string using the given statistics .
 Parameters
 longval : int
 The first part of an encoded tuple from encode
 nbits : int
 The second part of an encoded tuple from encode
 Returns
 str
 The arithmetically decoded text
 Example
 > > > ac = Arithmetic ( ' the quick brown fox jumped over the lazy dog ' )
 > > > ac . decode ( 16720586181 , 34)
 ' align '"""

implementation:
val = Fraction ( longval , long ( 1 ) << nbits )
letters = [ ]
probs_items = [ ( char , minval , maxval ) for ( char , ( minval , maxval ) ) in self . _probs . items ( ) ]
char = '\x00'
while True :
    for ( char , minval , maxval ) in probs_items : # noqa : B007
        if minval <= val < maxval :
            break
    if char == '\x00' :
        break
    letters . append ( char )
    delta = maxval - minval
    val = ( val - minval ) / delta
return '' . join ( letters )


signature:
def fuzzy_soundex ( word , max_length = 5 , zero_pad = True ) :
    """Return the Fuzzy Soundex code for a word .
 This is a wrapper for : py : meth : ` FuzzySoundex . encode ` .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The length of the code returned ( defaults to 4)
 zero _ pad : bool
 Pad the end of the return value with 0s to achieve a max _ length string
 Returns
 str
 The Fuzzy Soundex value
 Examples
 > > > fuzzy _ soundex ( ' Christopher ' )
 ' K6931'
 > > > fuzzy _ soundex ( ' Niall ' )
 ' N4000'
 > > > fuzzy _ soundex ( ' Smith ' )
 ' S5300'
 > > > fuzzy _ soundex ( ' Smith ' )
 ' S5300'"""

implementation: return FuzzySoundex ( ) . encode ( word , max_length , zero_pad )


signature:
def encode ( self , word , max_length = 5 , zero_pad = True ) :
    """Return the Fuzzy Soundex code for a word .
 Parameters
 word : str
 The word to transform
 max _ length : int
 The length of the code returned ( defaults to 4)
 zero _ pad : bool
 Pad the end of the return value with 0s to achieve a max _ length
 string
 Returns
 str
 The Fuzzy Soundex value
 Examples
 > > > pe = FuzzySoundex ( )
 > > > pe . encode ( ' Christopher ' )
 ' K6931'
 > > > pe . encode ( ' Niall ' )
 ' N4000'
 > > > pe . encode ( ' Smith ' )
 ' S5300'
 > > > pe . encode ( ' Smith ' )
 ' S5300'"""

implementation:
word = unicode_normalize ( 'NFKD' , text_type ( word . upper ( ) ) )
word = word . replace ( 'ß' , 'SS' )
# Clamp max _ length to [ 4 , 64]
if max_length != - 1 :
    max_length = min ( max ( 4 , max_length ) , 64 )
else :
    max_length = 64
if not word :
    if zero_pad :
        return '0' * max_length
    return '0'
if word [ : 2 ] in { 'CS' , 'CZ' , 'TS' , 'TZ' } :
    word = 'SS' + word [ 2 : ]
elif word [ : 2 ] == 'GN' :
    word = 'NN' + word [ 2 : ]
elif word [ : 2 ] in { 'HR' , 'WR' } :
    word = 'RR' + word [ 2 : ]
elif word [ : 2 ] == 'HW' :
    word = 'WW' + word [ 2 : ]
elif word [ : 2 ] in { 'KN' , 'NG' } :
    word = 'NN' + word [ 2 : ]
if word [ - 2 : ] == 'CH' :
    word = word [ : - 2 ] + 'KK'
elif word [ - 2 : ] == 'NT' :
    word = word [ : - 2 ] + 'TT'
elif word [ - 2 : ] == 'RT' :
    word = word [ : - 2 ] + 'RR'
elif word [ - 3 : ] == 'RDT' :
    word = word [ : - 3 ] + 'RR'
word = word . replace ( 'CA' , 'KA' )
word = word . replace ( 'CC' , 'KK' )
word = word . replace ( 'CK' , 'KK' )
word = word . replace ( 'CE' , 'SE' )
word = word . replace ( 'CHL' , 'KL' )
word = word . replace ( 'CL' , 'KL' )
word = word . replace ( 'CHR' , 'KR' )
word = word . replace ( 'CR' , 'KR' )
word = word . replace ( 'CI' , 'SI' )
word = word . replace ( 'CO' , 'KO' )
word = word . replace ( 'CU' , 'KU' )
word = word . replace ( 'CY' , 'SY' )
word = word . replace ( 'DG' , 'GG' )
word = word . replace ( 'GH' , 'HH' )
word = word . replace ( 'MAC' , 'MK' )
word = word . replace ( 'MC' , 'MK' )
word = word . replace ( 'NST' , 'NSS' )
word = word . replace ( 'PF' , 'FF' )
word = word . replace ( 'PH' , 'FF' )
word = word . replace ( 'SCH' , 'SSS' )
word = word . replace ( 'TIO' , 'SIO' )
word = word . replace ( 'TIA' , 'SIO' )
word = word . replace ( 'TCH' , 'CHH' )
sdx = word . translate ( self . _trans )
sdx = sdx . replace ( '-' , '' )
# remove repeating characters
sdx = self . _delete_consecutive_repeats ( sdx )
if word [ 0 ] in { 'H' , 'W' , 'Y' } :
    sdx = word [ 0 ] + sdx
else :
    sdx = word [ 0 ] + sdx [ 1 : ]
sdx = sdx . replace ( '0' , '' )
if zero_pad :
    sdx += '0' * max_length
return sdx [ : max_length ]


signature:
def corpus_importer ( self , corpus , n_val = 1 , bos = '_START_' , eos = '_END_' ) :
    r"""Fill in self . ngcorpus from a Corpus argument .
 Parameters
 corpus : Corpus
 The Corpus from which to initialize the n - gram corpus
 n _ val : int
 Maximum n value for n - grams
 bos : str
 String to insert as an indicator of beginning of sentence
 eos : str
 String to insert as an indicator of end of sentence
 Raises
 TypeError
 Corpus argument of the Corpus class required .
 Example
 > > > tqbf = ' The quick brown fox jumped over the lazy dog . \ n '
 > > > tqbf + = ' And then it slept . \ n And the dog ran off . '
 > > > ngcorp = NGramCorpus ( )
 > > > ngcorp . corpus _ importer ( Corpus ( tqbf ) )"""

implementation:
if not corpus or not isinstance ( corpus , Corpus ) :
    raise TypeError ( 'Corpus argument of the Corpus class required.' )
sentences = corpus . sents ( )
for sent in sentences :
    ngs = Counter ( sent )
    for key in ngs . keys ( ) :
        self . _add_to_ngcorpus ( self . ngcorpus , [ key ] , ngs [ key ] )
    if n_val > 1 :
        if bos and bos != '' :
            sent = [ bos ] + sent
        if eos and eos != '' :
            sent += [ eos ]
        for i in range ( 2 , n_val + 1 ) :
            for j in range ( len ( sent ) - i + 1 ) :
                self . _add_to_ngcorpus ( self . ngcorpus , sent [ j : j + i ] , 1 )


signature:
def get_count ( self , ngram , corpus = None ) :
    r"""Get the count of an n - gram in the corpus .
 Parameters
 ngram : str
 The n - gram to retrieve the count of from the n - gram corpus
 corpus : Corpus
 The corpus
 Returns
 int
 The n - gram count
 Examples
 > > > tqbf = ' The quick brown fox jumped over the lazy dog . \ n '
 > > > tqbf + = ' And then it slept . \ n And the dog ran off . '
 > > > ngcorp = NGramCorpus ( Corpus ( tqbf ) )
 > > > NGramCorpus ( Corpus ( tqbf ) ) . get _ count ( ' the ' )
 > > > NGramCorpus ( Corpus ( tqbf ) ) . get _ count ( ' fox ' )"""

implementation:
if not corpus :
    corpus = self . ngcorpus
# if ngram is empty , we ' re at our leaf node and should return the
# value in None
if not ngram :
    return corpus [ None ]
# support strings or lists / tuples by splitting strings
if isinstance ( ngram , ( text_type , str ) ) :
    ngram = text_type ( ngram ) . split ( )
# if ngram is not empty , check whether the next element is in the
# corpus ; if so , recurse - - if not , return 0
if ngram [ 0 ] in corpus :
    return self . get_count ( ngram [ 1 : ] , corpus [ ngram [ 0 ] ] )
return 0


signature:
def _add_to_ngcorpus ( self , corpus , words , count ) :
    """Build up a corpus entry recursively .
 Parameters
 corpus : Corpus
 The corpus
 words : [ str ]
 Words to add to the corpus
 count : int
 Count of words"""

implementation:
if words [ 0 ] not in corpus :
    corpus [ words [ 0 ] ] = Counter ( )
if len ( words ) == 1 :
    corpus [ words [ 0 ] ] [ None ] += count
else :
    self . _add_to_ngcorpus ( corpus [ words [ 0 ] ] , words [ 1 : ] , count )


signature:
def gng_importer ( self , corpus_file ) :
    """Fill in self . ngcorpus from a Google NGram corpus file .
 Parameters
 corpus _ file : file
 The Google NGram file from which to initialize the n - gram corpus"""

implementation:
with c_open ( corpus_file , 'r' , encoding = 'utf-8' ) as gng :
    for line in gng :
        line = line . rstrip ( ) . split ( '\t' )
        words = line [ 0 ] . split ( )
        self . _add_to_ngcorpus ( self . ngcorpus , words , int ( line [ 2 ] ) )


signature:
def tf ( self , term ) :
    r"""Return term frequency .
 Parameters
 term : str
 The term for which to calculate tf
 Returns
 float
 The term frequency ( tf )
 Raises
 ValueError
 tf can only calculate the frequency of individual words
 Examples
 > > > tqbf = ' The quick brown fox jumped over the lazy dog . \ n '
 > > > tqbf + = ' And then it slept . \ n And the dog ran off . '
 > > > ngcorp = NGramCorpus ( Corpus ( tqbf ) )
 > > > NGramCorpus ( Corpus ( tqbf ) ) . tf ( ' the ' )
 1.3010299956639813
 > > > NGramCorpus ( Corpus ( tqbf ) ) . tf ( ' fox ' )
 1.0"""

implementation:
if ' ' in term :
    raise ValueError ( 'tf can only calculate the term frequency of individual words' )
tcount = self . get_count ( term )
if tcount == 0 :
    return 0.0
return 1 + log10 ( tcount )


signature:
def encode ( self , word , terminator = '\0' ) :
    r"""Return the Burrows - Wheeler transformed form of a word .
 Parameters
 word : str
 The word to transform using BWT
 terminator : str
 A character added to signal the end of the string
 Returns
 str
 Word encoded by BWT
 Raises
 ValueError
 Specified terminator absent from code .
 Examples
 > > > bwt = BWT ( )
 > > > bwt . encode ( ' align ' )
 ' n \ x00ilag '
 > > > bwt . encode ( ' banana ' )
 ' annb \ x00aa '
 > > > bwt . encode ( ' banana ' , ' @ ' )
 ' annb @ aa '"""

implementation:
if word :
    if terminator in word :
        raise ValueError ( 'Specified terminator, {}, already in word.' . format ( terminator if terminator != '\0' else '\\0' ) )
    else :
        word += terminator
        wordlist = sorted ( word [ i : ] + word [ : i ] for i in range ( len ( word ) ) )
        return '' . join ( [ w [ - 1 ] for w in wordlist ] )
else :
    return terminator


signature:
def decode ( self , code , terminator = '\0' ) :
    r"""Return a word decoded from BWT form .
 Parameters
 code : str
 The word to transform from BWT form
 terminator : str
 A character added to signal the end of the string
 Returns
 str
 Word decoded by BWT
 Raises
 ValueError
 Specified terminator absent from code .
 Examples
 > > > bwt = BWT ( )
 > > > bwt . decode ( ' n \ x00ilag ' )
 ' align '
 > > > bwt . decode ( ' annb \ x00aa ' )
 ' banana '
 > > > bwt . decode ( ' annb @ aa ' , ' @ ' )
 ' banana '"""

implementation:
if code :
    if terminator not in code :
        raise ValueError ( 'Specified terminator, {}, absent from code.' . format ( terminator if terminator != '\0' else '\\0' ) )
    else :
        wordlist = [ '' ] * len ( code )
        for i in range ( len ( code ) ) :
            wordlist = sorted ( code [ i ] + wordlist [ i ] for i in range ( len ( code ) ) )
        rows = [ w for w in wordlist if w [ - 1 ] == terminator ] [ 0 ]
        return rows . rstrip ( terminator )
else :
    return ''


signature:
def dist_abs ( self , src , tar ) :
    """Return the indel distance between two strings .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 int
 Indel distance
 Examples
 > > > cmp = Indel ( )
 > > > cmp . dist _ abs ( ' cat ' , ' hat ' )
 > > > cmp . dist _ abs ( ' Niall ' , ' Neil ' )
 > > > cmp . dist _ abs ( ' Colin ' , ' Cuilen ' )
 > > > cmp . dist _ abs ( ' ATCG ' , ' TAGC ' )"""

implementation: return self . _lev . dist_abs ( src , tar , mode = 'lev' , cost = ( 1 , 1 , 9999 , 9999 ) )


signature:
def dist ( self , src , tar ) :
    """Return the normalized indel distance between two strings .
 This is equivalent to normalized Levenshtein distance , when only
 inserts and deletes are possible .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 float
 Normalized indel distance
 Examples
 > > > cmp = Indel ( )
 > > > round ( cmp . dist ( ' cat ' , ' hat ' ) , 12)
 0.33333
 > > > round ( cmp . dist ( ' Niall ' , ' Neil ' ) , 12)
 0.33333
 > > > round ( cmp . dist ( ' Colin ' , ' Cuilen ' ) , 12)
 0.454545454545
 > > > cmp . dist ( ' ATCG ' , ' TAGC ' )
 0.5"""

implementation:
if src == tar :
    return 0.0
return self . dist_abs ( src , tar ) / ( len ( src ) + len ( tar ) )


signature:
def sim ( self , src , tar , * args , ** kwargs ) :
    """Return similarity .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 * args
 Variable length argument list .
 * * kwargs
 Arbitrary keyword arguments .
 Returns
 float
 Similarity"""

implementation: return 1.0 - self . dist ( src , tar , * args , ** kwargs )


signature:
def dist_abs ( self , src , tar , * args , ** kwargs ) :
    """Return absolute distance .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 * args
 Variable length argument list .
 * * kwargs
 Arbitrary keyword arguments .
 Returns
 int
 Absolute distance"""

implementation: return self . dist ( src , tar , * args , ** kwargs )


signature:
def occurrence_fingerprint ( word , n_bits = 16 , most_common = MOST_COMMON_LETTERS_CG ) :
    """Return the occurrence fingerprint .
 This is a wrapper for : py : meth : ` Occurrence . fingerprint ` .
 Parameters
 word : str
 The word to fingerprint
 n _ bits : int
 Number of bits in the fingerprint returned
 most _ common : list
 The most common tokens in the target language , ordered by frequency
 Returns
 int
 The occurrence fingerprint
 Examples
 > > > bin ( occurrence _ fingerprint ( ' hat ' ) )
 '0b110000100000'
 > > > bin ( occurrence _ fingerprint ( ' niall ' ) )
 '0b10110000100000'
 > > > bin ( occurrence _ fingerprint ( ' colin ' ) )
 '0b1110000110000'
 > > > bin ( occurrence _ fingerprint ( ' atcg ' ) )
 '0b110000010000'
 > > > bin ( occurrence _ fingerprint ( ' entreatment ' ) )
 '0b1110010010000100'"""

implementation: return Occurrence ( ) . fingerprint ( word , n_bits , most_common )


signature:
def fingerprint ( self , word , n_bits = 16 , most_common = MOST_COMMON_LETTERS_CG ) :
    """Return the occurrence fingerprint .
 Parameters
 word : str
 The word to fingerprint
 n _ bits : int
 Number of bits in the fingerprint returned
 most _ common : list
 The most common tokens in the target language , ordered by frequency
 Returns
 int
 The occurrence fingerprint
 Examples
 > > > of = Occurrence ( )
 > > > bin ( of . fingerprint ( ' hat ' ) )
 '0b110000100000'
 > > > bin ( of . fingerprint ( ' niall ' ) )
 '0b10110000100000'
 > > > bin ( of . fingerprint ( ' colin ' ) )
 '0b1110000110000'
 > > > bin ( of . fingerprint ( ' atcg ' ) )
 '0b110000010000'
 > > > bin ( of . fingerprint ( ' entreatment ' ) )
 '0b1110010010000100'"""

implementation:
word = set ( word )
fingerprint = 0
for letter in most_common :
    if letter in word :
        fingerprint += 1
    n_bits -= 1
    if n_bits :
        fingerprint <<= 1
    else :
        break
n_bits -= 1
if n_bits > 0 :
    fingerprint <<= n_bits
return fingerprint


signature:
def sim_baystat ( src , tar , min_ss_len = None , left_ext = None , right_ext = None ) :
    """Return the Baystat similarity .
 This is a wrapper for : py : meth : ` Baystat . sim ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 min _ ss _ len : int
 Minimum substring length to be considered
 left _ ext : int
 Left - side extension length
 right _ ext : int
 Right - side extension length
 Returns
 float
 The Baystat similarity
 Examples
 > > > round ( sim _ baystat ( ' cat ' , ' hat ' ) , 12)
 0.666667
 > > > sim _ baystat ( ' Niall ' , ' Neil ' )
 0.4
 > > > round ( sim _ baystat ( ' Colin ' , ' Cuilen ' ) , 12)
 0.1666667
 > > > sim _ baystat ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation: return Baystat ( ) . sim ( src , tar , min_ss_len , left_ext , right_ext )


signature:
def dist_baystat ( src , tar , min_ss_len = None , left_ext = None , right_ext = None ) :
    """Return the Baystat distance .
 This is a wrapper for : py : meth : ` Baystat . dist ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 min _ ss _ len : int
 Minimum substring length to be considered
 left _ ext : int
 Left - side extension length
 right _ ext : int
 Right - side extension length
 Returns
 float
 The Baystat distance
 Examples
 > > > round ( dist _ baystat ( ' cat ' , ' hat ' ) , 12)
 0.33333
 > > > dist _ baystat ( ' Niall ' , ' Neil ' )
 0.6
 > > > round ( dist _ baystat ( ' Colin ' , ' Cuilen ' ) , 12)
 0.833333
 > > > dist _ baystat ( ' ATCG ' , ' TAGC ' )
 1.0"""

implementation: return Baystat ( ) . dist ( src , tar , min_ss_len , left_ext , right_ext )


signature:
def sim ( self , src , tar , min_ss_len = None , left_ext = None , right_ext = None ) :
    """Return the Baystat similarity .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 min _ ss _ len : int
 Minimum substring length to be considered
 left _ ext : int
 Left - side extension length
 right _ ext : int
 Right - side extension length
 Returns
 float
 The Baystat similarity
 Examples
 > > > cmp = Baystat ( )
 > > > round ( cmp . sim ( ' cat ' , ' hat ' ) , 12)
 0.666667
 > > > cmp . sim ( ' Niall ' , ' Neil ' )
 0.4
 > > > round ( cmp . sim ( ' Colin ' , ' Cuilen ' ) , 12)
 0.1666667
 > > > cmp . sim ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation:
if src == tar :
    return 1.0
if not src or not tar :
    return 0.0
max_len = max ( len ( src ) , len ( tar ) )
if not ( min_ss_len and left_ext and right_ext ) : # These can be set via arguments to the function . Otherwise they
# are set automatically based on values from the article .
    if max_len >= 7 :
        min_ss_len = 2
        left_ext = 2
        right_ext = 2
    else : # The paper suggests that for short names , ( exclusively ) one or
    # the other of left _ ext and right _ ext can be 1 , with good
    # results . I use 0 & 0 as the default in this case .
        min_ss_len = 1
        left_ext = 0
        right_ext = 0
pos = 0
match_len = 0
while True :
    if pos + min_ss_len > len ( src ) :
        return match_len / max_len
    hit_len = 0
    ix = 1
    substring = src [ pos : pos + min_ss_len ]
    search_begin = pos - left_ext
    if search_begin < 0 :
        search_begin = 0
        left_ext_len = pos
    else :
        left_ext_len = left_ext
    if pos + min_ss_len + right_ext >= len ( tar ) :
        right_ext_len = len ( tar ) - pos - min_ss_len
    else :
        right_ext_len = right_ext
    if ( search_begin + left_ext_len + min_ss_len + right_ext_len > search_begin ) :
        search_val = tar [ search_begin : ( search_begin + left_ext_len + min_ss_len + right_ext_len ) ]
    else :
        search_val = ''
    flagged_tar = ''
    while substring in search_val and pos + ix <= len ( src ) :
        hit_len = len ( substring )
        flagged_tar = tar . replace ( substring , '#' * hit_len )
        if pos + min_ss_len + ix <= len ( src ) :
            substring = src [ pos : pos + min_ss_len + ix ]
        if pos + min_ss_len + right_ext_len + 1 <= len ( tar ) :
            right_ext_len += 1
        # The following is unnecessary , I think
        # if ( search _ begin + left _ ext _ len + min _ ss _ len + right _ ext _ len
        # < = len ( tar ) ) :
        search_val = tar [ search_begin : ( search_begin + left_ext_len + min_ss_len + right_ext_len ) ]
        ix += 1
    if hit_len > 0 :
        tar = flagged_tar
    match_len += hit_len
    pos += ix


signature:
def sim_tversky ( src , tar , qval = 2 , alpha = 1 , beta = 1 , bias = None ) :
    """Return the Tversky index of two strings .
 This is a wrapper for : py : meth : ` Tversky . sim ` .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version
 alpha : float
 Tversky index parameter as described above
 beta : float
 Tversky index parameter as described above
 bias : float
 The symmetric Tversky index bias parameter
 Returns
 float
 Tversky similarity
 Examples
 > > > sim _ tversky ( ' cat ' , ' hat ' )
 0.33333
 > > > sim _ tversky ( ' Niall ' , ' Neil ' )
 0.22222
 > > > sim _ tversky ( ' aluminum ' , ' Catalan ' )
 0.0625
 > > > sim _ tversky ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation: return Tversky ( ) . sim ( src , tar , qval , alpha , beta , bias )


signature:
def dist_tversky ( src , tar , qval = 2 , alpha = 1 , beta = 1 , bias = None ) :
    """Return the Tversky distance between two strings .
 This is a wrapper for : py : meth : ` Tversky . dist ` .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version
 alpha : float
 Tversky index parameter as described above
 beta : float
 Tversky index parameter as described above
 bias : float
 The symmetric Tversky index bias parameter
 Returns
 float
 Tversky distance
 Examples
 > > > dist _ tversky ( ' cat ' , ' hat ' )
 0.666667
 > > > dist _ tversky ( ' Niall ' , ' Neil ' )
 0.777778
 > > > dist _ tversky ( ' aluminum ' , ' Catalan ' )
 0.9375
 > > > dist _ tversky ( ' ATCG ' , ' TAGC ' )
 1.0"""

implementation: return Tversky ( ) . dist ( src , tar , qval , alpha , beta , bias )


signature:
def sim ( self , src , tar , qval = 2 , alpha = 1 , beta = 1 , bias = None ) :
    """Return the Tversky index of two strings .
 Parameters
 src : str
 Source string ( or QGrams / Counter objects ) for comparison
 tar : str
 Target string ( or QGrams / Counter objects ) for comparison
 qval : int
 The length of each q - gram ; 0 for non - q - gram version
 alpha : float
 Tversky index parameter as described above
 beta : float
 Tversky index parameter as described above
 bias : float
 The symmetric Tversky index bias parameter
 Returns
 float
 Tversky similarity
 Raises
 ValueError
 Unsupported weight assignment ; alpha and beta must be greater than
 or equal to 0.
 Examples
 > > > cmp = Tversky ( )
 > > > cmp . sim ( ' cat ' , ' hat ' )
 0.33333
 > > > cmp . sim ( ' Niall ' , ' Neil ' )
 0.22222
 > > > cmp . sim ( ' aluminum ' , ' Catalan ' )
 0.0625
 > > > cmp . sim ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation:
if alpha < 0 or beta < 0 :
    raise ValueError ( 'Unsupported weight assignment; alpha and beta ' + 'must be greater than or equal to 0.' )
if src == tar :
    return 1.0
elif not src or not tar :
    return 0.0
q_src , q_tar = self . _get_qgrams ( src , tar , qval )
q_src_mag = sum ( q_src . values ( ) )
q_tar_mag = sum ( q_tar . values ( ) )
q_intersection_mag = sum ( ( q_src & q_tar ) . values ( ) )
if not q_src or not q_tar :
    return 0.0
if bias is None :
    return q_intersection_mag / ( q_intersection_mag + alpha * ( q_src_mag - q_intersection_mag ) + beta * ( q_tar_mag - q_intersection_mag ) )
a_val = min ( q_src_mag - q_intersection_mag , q_tar_mag - q_intersection_mag )
b_val = max ( q_src_mag - q_intersection_mag , q_tar_mag - q_intersection_mag )
c_val = q_intersection_mag + bias
return c_val / ( beta * ( alpha * a_val + ( 1 - alpha ) * b_val ) + c_val )


signature:
def lcsseq ( self , src , tar ) :
    """Return the longest common subsequence of two strings .
 Based on the dynamic programming algorithm from
 http : / / rosettacode . org / wiki / Longest _ common _ subsequence
 : cite : ` rosettacode : 2018b ` . This is licensed GFDL 1.2.
 Modifications include :
 conversion to a numpy array in place of a list of lists
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 str
 The longest common subsequence
 Examples
 > > > sseq = LCSseq ( )
 > > > sseq . lcsseq ( ' cat ' , ' hat ' )
 ' at '
 > > > sseq . lcsseq ( ' Niall ' , ' Neil ' )
 ' Nil '
 > > > sseq . lcsseq ( ' aluminum ' , ' Catalan ' )
 ' aln '
 > > > sseq . lcsseq ( ' ATCG ' , ' TAGC ' )
 ' AC '"""

implementation:
lengths = np_zeros ( ( len ( src ) + 1 , len ( tar ) + 1 ) , dtype = np_int )
# row 0 and column 0 are initialized to 0 already
for i , src_char in enumerate ( src ) :
    for j , tar_char in enumerate ( tar ) :
        if src_char == tar_char :
            lengths [ i + 1 , j + 1 ] = lengths [ i , j ] + 1
        else :
            lengths [ i + 1 , j + 1 ] = max ( lengths [ i + 1 , j ] , lengths [ i , j + 1 ] )
# read the substring out from the matrix
result = ''
i , j = len ( src ) , len ( tar )
while i != 0 and j != 0 :
    if lengths [ i , j ] == lengths [ i - 1 , j ] :
        i -= 1
    elif lengths [ i , j ] == lengths [ i , j - 1 ] :
        j -= 1
    else :
        result = src [ i - 1 ] + result
        i -= 1
        j -= 1
return result


signature:
def sim ( self , src , tar ) :
    r"""Return the longest common subsequence similarity of two strings .
 Longest common subsequence similarity ( : math : ` sim _ { LCSseq } ` ) .
 This employs the LCSseq function to derive a similarity metric :
 : math : ` sim _ { LCSseq } ( s , t ) = \ frac { | LCSseq ( s , t ) | } { max ( | s | , | t | ) } `
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 float
 LCSseq similarity
 Examples
 > > > sseq = LCSseq ( )
 > > > sseq . sim ( ' cat ' , ' hat ' )
 0.66666
 > > > sseq . sim ( ' Niall ' , ' Neil ' )
 0.6
 > > > sseq . sim ( ' aluminum ' , ' Catalan ' )
 0.375
 > > > sseq . sim ( ' ATCG ' , ' TAGC ' )
 0.5"""

implementation:
if src == tar :
    return 1.0
elif not src or not tar :
    return 0.0
return len ( self . lcsseq ( src , tar ) ) / max ( len ( src ) , len ( tar ) )


signature:
def sim ( self , src , tar ) :
    """Return the prefix similarity of two strings .
 Prefix similarity is the ratio of the length of the shorter term that
 exactly matches the longer term to the length of the shorter term ,
 beginning at the start of both terms .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 float
 Prefix similarity
 Examples
 > > > cmp = Prefix ( )
 > > > cmp . sim ( ' cat ' , ' hat ' )
 0.0
 > > > cmp . sim ( ' Niall ' , ' Neil ' )
 0.25
 > > > cmp . sim ( ' aluminum ' , ' Catalan ' )
 0.0
 > > > cmp . sim ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation:
if src == tar :
    return 1.0
if not src or not tar :
    return 0.0
min_word , max_word = ( src , tar ) if len ( src ) < len ( tar ) else ( tar , src )
min_len = len ( min_word )
for i in range ( min_len , 0 , - 1 ) :
    if min_word [ : i ] == max_word [ : i ] :
        return i / min_len
return 0.0


signature:
def count_fingerprint ( word , n_bits = 16 , most_common = MOST_COMMON_LETTERS_CG ) :
    """Return the count fingerprint .
 This is a wrapper for : py : meth : ` Count . fingerprint ` .
 Parameters
 word : str
 The word to fingerprint
 n _ bits : int
 Number of bits in the fingerprint returned
 most _ common : list
 The most common tokens in the target language , ordered by frequency
 Returns
 int
 The count fingerprint
 Examples
 > > > bin ( count _ fingerprint ( ' hat ' ) )
 '0b101000001'
 > > > bin ( count _ fingerprint ( ' niall ' ) )
 '0b10001010000'
 > > > bin ( count _ fingerprint ( ' colin ' ) )
 '0b101010000'
 > > > bin ( count _ fingerprint ( ' atcg ' ) )
 '0b10100000'
 > > > bin ( count _ fingerprint ( ' entreatment ' ) )
 '0b1111010000100000'"""

implementation: return Count ( ) . fingerprint ( word , n_bits , most_common )


signature:
def fingerprint ( self , word , n_bits = 16 , most_common = MOST_COMMON_LETTERS_CG ) :
    """Return the count fingerprint .
 Parameters
 word : str
 The word to fingerprint
 n _ bits : int
 Number of bits in the fingerprint returned
 most _ common : list
 The most common tokens in the target language , ordered by frequency
 Returns
 int
 The count fingerprint
 Examples
 > > > cf = Count ( )
 > > > bin ( cf . fingerprint ( ' hat ' ) )
 '0b101000001'
 > > > bin ( cf . fingerprint ( ' niall ' ) )
 '0b10001010000'
 > > > bin ( cf . fingerprint ( ' colin ' ) )
 '0b101010000'
 > > > bin ( cf . fingerprint ( ' atcg ' ) )
 '0b10100000'
 > > > bin ( cf . fingerprint ( ' entreatment ' ) )
 '0b1111010000100000'"""

implementation:
if n_bits % 2 :
    n_bits += 1
word = Counter ( word )
fingerprint = 0
for letter in most_common :
    if n_bits :
        fingerprint <<= 2
        fingerprint += word [ letter ] & 3
        n_bits -= 2
    else :
        break
if n_bits :
    fingerprint <<= n_bits
return fingerprint


signature:
def phonetic_fingerprint ( phrase , phonetic_algorithm = double_metaphone , joiner = ' ' , * args , ** kwargs ) :
    """Return the phonetic fingerprint of a phrase .
 This is a wrapper for : py : meth : ` Phonetic . fingerprint ` .
 Parameters
 phrase : str
 The string from which to calculate the phonetic fingerprint
 phonetic _ algorithm : function
 A phonetic algorithm that takes a string and returns a string
 ( presumably a phonetic representation of the original string ) . By
 default , this function uses : py : func : ` . double _ metaphone ` .
 joiner : str
 The string that will be placed between each word
 * args
 Variable length argument list
 * * kwargs
 Arbitrary keyword arguments
 Returns
 str
 The phonetic fingerprint of the phrase
 Examples
 > > > phonetic _ fingerprint ( ' The quick brown fox jumped over the lazy dog . ' )
 '0 afr fks jmpt kk ls prn tk '
 > > > from abydos . phonetic import soundex
 > > > phonetic _ fingerprint ( ' The quick brown fox jumped over the lazy dog . ' ,
 . . . phonetic _ algorithm = soundex )
 ' b650 d200 f200 j513 l200 o160 q200 t000'"""

implementation: return Phonetic ( ) . fingerprint ( phrase , phonetic_algorithm , joiner , * args , ** kwargs )


signature:
def fingerprint ( self , phrase , phonetic_algorithm = double_metaphone , joiner = ' ' , * args , ** kwargs ) :
    """Return the phonetic fingerprint of a phrase .
 Parameters
 phrase : str
 The string from which to calculate the phonetic fingerprint
 phonetic _ algorithm : function
 A phonetic algorithm that takes a string and returns a string
 ( presumably a phonetic representation of the original string ) . By
 default , this function uses : py : func : ` . double _ metaphone ` .
 joiner : str
 The string that will be placed between each word
 * args
 Variable length argument list
 * * kwargs
 Arbitrary keyword arguments
 Returns
 str
 The phonetic fingerprint of the phrase
 Examples
 > > > pf = Phonetic ( )
 > > > pf . fingerprint ( ' The quick brown fox jumped over the lazy dog . ' )
 '0 afr fks jmpt kk ls prn tk '
 > > > from abydos . phonetic import soundex
 > > > pf . fingerprint ( ' The quick brown fox jumped over the lazy dog . ' ,
 . . . phonetic _ algorithm = soundex )
 ' b650 d200 f200 j513 l200 o160 q200 t000'"""

implementation:
phonetic = ''
for word in phrase . split ( ) :
    word = phonetic_algorithm ( word , * args , ** kwargs )
    if not isinstance ( word , text_type ) and hasattr ( word , '__iter__' ) :
        word = word [ 0 ]
    phonetic += word + joiner
phonetic = phonetic [ : - len ( joiner ) ]
return super ( self . __class__ , self ) . fingerprint ( phonetic )


signature:
def docs_of_words ( self ) :
    r"""Return the docs in the corpus , with sentences flattened .
 Each list within the corpus represents all the words of that document .
 Thus the sentence level of lists has been flattened .
 Returns
 [ [ str ] ]
 The docs in the corpus as a list of list of strs
 Example
 > > > tqbf = ' The quick brown fox jumped over the lazy dog . \ n '
 > > > tqbf + = ' And then it slept . \ n And the dog ran off . '
 > > > corp = Corpus ( tqbf )
 > > > corp . docs _ of _ words ( )
 [ [ ' The ' , ' quick ' , ' brown ' , ' fox ' , ' jumped ' , ' over ' , ' the ' , ' lazy ' ,
 ' dog . ' , ' And ' , ' then ' , ' it ' , ' slept . ' , ' And ' , ' the ' , ' dog ' , ' ran ' ,
 ' off . ' ] ]
 > > > len ( corp . docs _ of _ words ( ) )"""

implementation: return [ [ words for sents in doc for words in sents ] for doc in self . corpus ]


signature:
def raw ( self ) :
    r"""Return the raw corpus .
 This is reconstructed by joining sub - components with the corpus ' split
 characters
 Returns
 str
 The raw corpus
 Example
 > > > tqbf = ' The quick brown fox jumped over the lazy dog . \ n '
 > > > tqbf + = ' And then it slept . \ n And the dog ran off . '
 > > > corp = Corpus ( tqbf )
 > > > print ( corp . raw ( ) )
 The quick brown fox jumped over the lazy dog .
 And then it slept .
 And the dog ran off .
 > > > len ( corp . raw ( ) )
 85"""

implementation:
doc_list = [ ]
for doc in self . corpus :
    sent_list = [ ]
    for sent in doc :
        sent_list . append ( ' ' . join ( sent ) )
    doc_list . append ( self . sent_split . join ( sent_list ) )
    del sent_list
return self . doc_split . join ( doc_list )


signature:
def idf ( self , term , transform = None ) :
    r"""Calculate the Inverse Document Frequency of a term in the corpus .
 Parameters
 term : str
 The term to calculate the IDF of
 transform : function
 A function to apply to each document term before checking for the
 presence of term
 Returns
 float
 The IDF
 Examples
 > > > tqbf = ' The quick brown fox jumped over the lazy dog . \ n \ n '
 > > > tqbf + = ' And then it slept . \ n \ n And the dog ran off . '
 > > > corp = Corpus ( tqbf )
 > > > print ( corp . docs ( ) )
 [ [ [ ' The ' , ' quick ' , ' brown ' , ' fox ' , ' jumped ' , ' over ' , ' the ' , ' lazy ' ,
 ' dog . ' ] ] ,
 [ [ ' And ' , ' then ' , ' it ' , ' slept . ' ] ] ,
 [ [ ' And ' , ' the ' , ' dog ' , ' ran ' , ' off . ' ] ] ]
 > > > round ( corp . idf ( ' dog ' ) , 10)
 0.4771212547
 > > > round ( corp . idf ( ' the ' ) , 10)
 0.1760912591"""

implementation:
docs_with_term = 0
docs = self . docs_of_words ( )
for doc in docs :
    doc_set = set ( doc )
    if transform :
        transformed_doc = [ ]
        for word in doc_set :
            transformed_doc . append ( transform ( word ) )
        doc_set = set ( transformed_doc )
    if term in doc_set :
        docs_with_term += 1
if docs_with_term == 0 :
    return float ( 'inf' )
return log10 ( len ( docs ) / docs_with_term )


signature:
def stem ( self , word ) :
    """Return Paice - Husk stem .
 Parameters
 word : str
 The word to stem
 Returns
 str
 Word stem
 Examples
 > > > stmr = PaiceHusk ( )
 > > > stmr . stem ( ' assumption ' )
 ' assum '
 > > > stmr . stem ( ' verifiable ' )
 ' ver '
 > > > stmr . stem ( ' fancies ' )
 ' fant '
 > > > stmr . stem ( ' fanciful ' )
 ' fancy '
 > > > stmr . stem ( ' torment ' )
 ' tor '"""

implementation:
terminate = False
intact = True
while not terminate :
    for n in range ( 6 , 0 , - 1 ) :
        if word [ - n : ] in self . _rule_table [ n ] :
            accept = False
            if len ( self . _rule_table [ n ] [ word [ - n : ] ] ) < 4 :
                for rule in self . _rule_table [ n ] [ word [ - n : ] ] :
                    ( word , accept , intact , terminate , ) = self . _apply_rule ( word , rule , intact , terminate )
                    if accept :
                        break
            else :
                rule = self . _rule_table [ n ] [ word [ - n : ] ]
                ( word , accept , intact , terminate ) = self . _apply_rule ( word , rule , intact , terminate )
            if accept :
                break
    else :
        break
return word


signature:
def encode ( self , word ) :
    """Return Reth - Schek Phonetik code for a word .
 Parameters
 word : str
 The word to transform
 Returns
 str
 The Reth - Schek Phonetik code
 Examples
 > > > reth _ schek _ phonetik ( ' Joachim ' )
 ' JOAGHIM '
 > > > reth _ schek _ phonetik ( ' Christoph ' )
 ' GHRISDOF '
 > > > reth _ schek _ phonetik ( ' Jörg ' )
 ' JOERG '
 > > > reth _ schek _ phonetik ( ' Smith ' )
 ' SMID '
 > > > reth _ schek _ phonetik ( ' Schmidt ' )
 ' SCHMID '"""

implementation:
# Uppercase
word = word . upper ( )
# Replace umlauts / eszett
word = word . replace ( 'Ä' , 'AE' )
word = word . replace ( 'Ö' , 'OE' )
word = word . replace ( 'Ü' , 'UE' )
word = word . replace ( 'ß' , 'SS' )
# Main loop , using above replacements table
pos = 0
while pos < len ( word ) :
    for num in range ( 3 , 0 , - 1 ) :
        if word [ pos : pos + num ] in self . _replacements [ num ] :
            word = ( word [ : pos ] + self . _replacements [ num ] [ word [ pos : pos + num ] ] + word [ pos + num : ] )
            pos += 1
            break
    else :
        pos += 1
# Advance if nothing is recognized
# Change ' CH ' back ( ? ) to ' SCH '
word = word . replace ( 'CH' , 'SCH' )
# Replace final sequences
if word [ - 2 : ] == 'ER' :
    word = word [ : - 2 ] + 'R'
elif word [ - 2 : ] == 'EL' :
    word = word [ : - 2 ] + 'L'
elif word [ - 1 : ] == 'H' :
    word = word [ : - 1 ]
return word


signature:
def bmpm ( word , language_arg = 0 , name_mode = 'gen' , match_mode = 'approx' , concat = False , filter_langs = False , ) :
    """Return the Beider - Morse Phonetic Matching encoding ( s ) of a term .
 This is a wrapper for : py : meth : ` BeiderMorse . encode ` .
 Parameters
 word : str
 The word to transform
 language _ arg : str
 The language of the term ; supported values include :
 - ` ` any ` `
 - ` ` arabic ` `
 - ` ` cyrillic ` `
 - ` ` czech ` `
 - ` ` dutch ` `
 - ` ` english ` `
 - ` ` french ` `
 - ` ` german ` `
 - ` ` greek ` `
 - ` ` greeklatin ` `
 - ` ` hebrew ` `
 - ` ` hungarian ` `
 - ` ` italian ` `
 - ` ` latvian ` `
 - ` ` polish ` `
 - ` ` portuguese ` `
 - ` ` romanian ` `
 - ` ` russian ` `
 - ` ` spanish ` `
 - ` ` turkish ` `
 name _ mode : str
 The name mode of the algorithm :
 - ` ` gen ` ` - - general ( default )
 - ` ` ash ` ` - - Ashkenazi
 - ` ` sep ` ` - - Sephardic
 match _ mode : str
 Matching mode : ` ` approx ` ` or ` ` exact ` `
 concat : bool
 Concatenation mode
 filter _ langs : bool
 Filter out incompatible languages
 Returns
 tuple
 The Beider - Morse phonetic value ( s )
 Examples
 > > > bmpm ( ' Christopher ' )
 ' xrQstopir xrQstYpir xristopir xristYpir xrQstofir xrQstYfir xristofir
 xristYfir xristopi xritopir xritopi xristofi xritofir xritofi
 tzristopir tzristofir zristopir zristopi zritopir zritopi zristofir
 zristofi zritofir zritofi '
 > > > bmpm ( ' Niall ' )
 ' nial niol '
 > > > bmpm ( ' Smith ' )
 ' zmit '
 > > > bmpm ( ' Schmidt ' )
 ' zmit stzmit '
 > > > bmpm ( ' Christopher ' , language _ arg = ' German ' )
 ' xrQstopir xrQstYpir xristopir xristYpir xrQstofir xrQstYfir xristofir
 xristYfir '
 > > > bmpm ( ' Christopher ' , language _ arg = ' English ' )
 ' tzristofir tzrQstofir tzristafir tzrQstafir xristofir xrQstofir
 xristafir xrQstafir '
 > > > bmpm ( ' Christopher ' , language _ arg = ' German ' , name _ mode = ' ash ' )
 ' xrQstopir xrQstYpir xristopir xristYpir xrQstofir xrQstYfir xristofir
 xristYfir '
 > > > bmpm ( ' Christopher ' , language _ arg = ' German ' , match _ mode = ' exact ' )
 ' xriStopher xriStofer xristopher xristofer '"""

implementation: return BeiderMorse ( ) . encode ( word , language_arg , name_mode , match_mode , concat , filter_langs )


signature:
def _language ( self , name , name_mode ) :
    """Return the best guess language ID for the word and language choices .
 Parameters
 name : str
 The term to guess the language of
 name _ mode : str
 The name mode of the algorithm : ` ` gen ` ` ( default ) ,
 ` ` ash ` ` ( Ashkenazi ) , or ` ` sep ` ` ( Sephardic )
 Returns
 int
 Language ID"""

implementation:
name = name . strip ( ) . lower ( )
rules = BMDATA [ name_mode ] [ 'language_rules' ]
all_langs = ( sum ( _LANG_DICT [ _ ] for _ in BMDATA [ name_mode ] [ 'languages' ] ) - 1 )
choices_remaining = all_langs
for rule in rules :
    letters , languages , accept = rule
    if search ( letters , name ) is not None :
        if accept :
            choices_remaining &= languages
        else :
            choices_remaining &= ( ~ languages ) % ( all_langs + 1 )
if choices_remaining == L_NONE :
    choices_remaining = L_ANY
return choices_remaining


signature:
def _redo_language ( self , term , name_mode , rules , final_rules1 , final_rules2 , concat ) :
    """Reassess the language of the terms and call the phonetic encoder .
 Uses a split multi - word term .
 Parameters
 term : str
 The term to encode via Beider - Morse
 name _ mode : str
 The name mode of the algorithm : ` ` gen ` ` ( default ) ,
 ` ` ash ` ` ( Ashkenazi ) , or ` ` sep ` ` ( Sephardic )
 rules : tuple
 The set of initial phonetic transform regexps
 final _ rules1 : tuple
 The common set of final phonetic transform regexps
 final _ rules2 : tuple
 The specific set of final phonetic transform regexps
 concat : bool
 A flag to indicate concatenation
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
language_arg = self . _language ( term , name_mode )
return self . _phonetic ( term , name_mode , rules , final_rules1 , final_rules2 , language_arg , concat , )


signature:
def _phonetic ( self , term , name_mode , rules , final_rules1 , final_rules2 , language_arg = 0 , concat = False , ) :
    """Return the Beider - Morse encoding ( s ) of a term .
 Parameters
 term : str
 The term to encode via Beider - Morse
 name _ mode : str
 The name mode of the algorithm : ` ` gen ` ` ( default ) ,
 ` ` ash ` ` ( Ashkenazi ) , or ` ` sep ` ` ( Sephardic )
 rules : tuple
 The set of initial phonetic transform regexps
 final _ rules1 : tuple
 The common set of final phonetic transform regexps
 final _ rules2 : tuple
 The specific set of final phonetic transform regexps
 language _ arg : int
 The language of the term
 concat : bool
 A flag to indicate concatenation
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
term = term . replace ( '-' , ' ' ) . strip ( )
if name_mode == 'gen' : # generic case
# discard and concatenate certain words if at the start of the name
    for pfx in BMDATA [ 'gen' ] [ 'discards' ] :
        if term . startswith ( pfx ) :
            remainder = term [ len ( pfx ) : ]
            combined = pfx [ : - 1 ] + remainder
            result = ( self . _redo_language ( remainder , name_mode , rules , final_rules1 , final_rules2 , concat , ) + '-' + self . _redo_language ( combined , name_mode , rules , final_rules1 , final_rules2 , concat , ) )
            return result
words = ( term . split ( ) )
# create array of the individual words in the name
words2 = [ ]
if name_mode == 'sep' : # Sephardic case
# for each word in the name , delete portions of word preceding
# apostrophe
# ex : d ' avila d ' aguilar - - > avila aguilar
# also discard certain words in the name
# note that we can never get a match on " de la " because we are
# checking single words below
# this is a bug , but I won ' t try to fix it now
    for word in words :
        word = word [ word . rfind ( '\'' ) + 1 : ]
        if word not in BMDATA [ 'sep' ] [ 'discards' ] :
            words2 . append ( word )
elif name_mode == 'ash' : # Ashkenazic case
# discard certain words if at the start of the name
    if len ( words ) > 1 and words [ 0 ] in BMDATA [ 'ash' ] [ 'discards' ] :
        words2 = words [ 1 : ]
    else :
        words2 = list ( words )
else :
    words2 = list ( words )
if concat : # concatenate the separate words of a multi - word name
# ( normally used for exact matches )
    term = ' ' . join ( words2 )
elif len ( words2 ) == 1 : # not a multi - word name
    term = words2 [ 0 ]
else : # encode each word in a multi - word name separately
# ( normally used for approx matches )
    result = '-' . join ( [ self . _redo_language ( w , name_mode , rules , final_rules1 , final_rules2 , concat ) for w in words2 ] )
    return result
term_length = len ( term )
# apply language rules to map to phonetic alphabet
phonetic = ''
skip = 0
for i in range ( term_length ) :
    if skip :
        skip -= 1
        continue
    found = False
    for rule in rules :
        pattern = rule [ _PATTERN_POS ]
        pattern_length = len ( pattern )
        lcontext = rule [ _LCONTEXT_POS ]
        rcontext = rule [ _RCONTEXT_POS ]
        # check to see if next sequence in input matches the string in
        # the rule
        if ( pattern_length > term_length - i ) or ( term [ i : i + pattern_length ] != pattern ) : # no match
            continue
        right = '^' + rcontext
        left = lcontext + '$'
        # check that right context is satisfied
        if rcontext != '' :
            if not search ( right , term [ i + pattern_length : ] ) :
                continue
        # check that left context is satisfied
        if lcontext != '' :
            if not search ( left , term [ : i ] ) :
                continue
        # check for incompatible attributes
        candidate = self . _apply_rule_if_compat ( phonetic , rule [ _PHONETIC_POS ] , language_arg )
        # The below condition shouldn ' t ever be false
        if candidate is not None : # pragma : no branch
            phonetic = candidate
            found = True
            break
    if ( not found ) : # character in name that is not in table - - e . g . , space
        pattern_length = 1
    skip = pattern_length - 1
# apply final rules on phonetic - alphabet ,
# doing a substitution of certain characters
phonetic = self . _apply_final_rules ( phonetic , final_rules1 , language_arg , False )
# apply common rules
# final _ rules1 are the common approx rules ,
# final _ rules2 are approx rules for specific language
phonetic = self . _apply_final_rules ( phonetic , final_rules2 , language_arg , True )
# apply lang specific rules
return phonetic


signature:
def _apply_final_rules ( self , phonetic , final_rules , language_arg , strip ) :
    """Apply a set of final rules to the phonetic encoding .
 Parameters
 phonetic : str
 The term to which to apply the final rules
 final _ rules : tuple
 The set of final phonetic transform regexps
 language _ arg : int
 An integer representing the target language of the phonetic
 encoding
 strip : bool
 Flag to indicate whether to normalize the language attributes
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
# optimization to save time
if not final_rules :
    return phonetic
# expand the result
phonetic = self . _expand_alternates ( phonetic )
phonetic_array = phonetic . split ( '|' )
for k in range ( len ( phonetic_array ) ) :
    phonetic = phonetic_array [ k ]
    phonetic2 = ''
    phoneticx = self . _normalize_lang_attrs ( phonetic , True )
    i = 0
    while i < len ( phonetic ) :
        found = False
        if phonetic [ i ] == '[' : # skip over language attribute
            attrib_start = i
            i += 1
            while True :
                if phonetic [ i ] == ']' :
                    i += 1
                    phonetic2 += phonetic [ attrib_start : i ]
                    break
                i += 1
            continue
        for rule in final_rules :
            pattern = rule [ _PATTERN_POS ]
            pattern_length = len ( pattern )
            lcontext = rule [ _LCONTEXT_POS ]
            rcontext = rule [ _RCONTEXT_POS ]
            right = '^' + rcontext
            left = lcontext + '$'
            # check to see if next sequence in phonetic matches the
            # string in the rule
            if ( pattern_length > len ( phoneticx ) - i ) or phoneticx [ i : i + pattern_length ] != pattern :
                continue
            # check that right context is satisfied
            if rcontext != '' :
                if not search ( right , phoneticx [ i + pattern_length : ] ) :
                    continue
            # check that left context is satisfied
            if lcontext != '' :
                if not search ( left , phoneticx [ : i ] ) :
                    continue
            # check for incompatible attributes
            candidate = self . _apply_rule_if_compat ( phonetic2 , rule [ _PHONETIC_POS ] , language_arg )
            # The below condition shouldn ' t ever be false
            if candidate is not None : # pragma : no branch
                phonetic2 = candidate
                found = True
                break
        if not found : # character in name for which there is no substitution in
        # the table
            phonetic2 += phonetic [ i ]
            pattern_length = 1
        i += pattern_length
    phonetic_array [ k ] = self . _expand_alternates ( phonetic2 )
phonetic = '|' . join ( phonetic_array )
if strip :
    phonetic = self . _normalize_lang_attrs ( phonetic , True )
if '|' in phonetic :
    phonetic = '(' + self . _remove_dupes ( phonetic ) + ')'
return phonetic


signature:
def _expand_alternates ( self , phonetic ) :
    """Expand phonetic alternates separated by | s .
 Parameters
 phonetic : str
 A Beider - Morse phonetic encoding
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
alt_start = phonetic . find ( '(' )
if alt_start == - 1 :
    return self . _normalize_lang_attrs ( phonetic , False )
prefix = phonetic [ : alt_start ]
alt_start += 1
# get past the (
alt_end = phonetic . find ( ')' , alt_start )
alt_string = phonetic [ alt_start : alt_end ]
alt_end += 1
# get past the )
suffix = phonetic [ alt_end : ]
alt_array = alt_string . split ( '|' )
result = ''
for i in range ( len ( alt_array ) ) :
    alt = alt_array [ i ]
    alternate = self . _expand_alternates ( prefix + alt + suffix )
    if alternate != '' and alternate != '[0]' :
        if result != '' :
            result += '|'
        result += alternate
return result


signature:
def _pnums_with_leading_space ( self , phonetic ) :
    """Join prefixes & suffixes in cases of alternate phonetic values .
 Parameters
 phonetic : str
 A Beider - Morse phonetic encoding
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
alt_start = phonetic . find ( '(' )
if alt_start == - 1 :
    return ' ' + self . _phonetic_number ( phonetic )
prefix = phonetic [ : alt_start ]
alt_start += 1
# get past the (
alt_end = phonetic . find ( ')' , alt_start )
alt_string = phonetic [ alt_start : alt_end ]
alt_end += 1
# get past the )
suffix = phonetic [ alt_end : ]
alt_array = alt_string . split ( '|' )
result = ''
for alt in alt_array :
    result += self . _pnums_with_leading_space ( prefix + alt + suffix )
return result


signature:
def _phonetic_numbers ( self , phonetic ) :
    """Prepare & join phonetic numbers .
 Split phonetic value on ' - ' , run through _ pnums _ with _ leading _ space ,
 and join with ' '
 Parameters
 phonetic : str
 A Beider - Morse phonetic encoding
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
phonetic_array = phonetic . split ( '-' )
# for names with spaces in them
result = ' ' . join ( [ self . _pnums_with_leading_space ( i ) [ 1 : ] for i in phonetic_array ] )
return result


signature:
def _remove_dupes ( self , phonetic ) :
    """Remove duplicates from a phonetic encoding list .
 Parameters
 phonetic : str
 A Beider - Morse phonetic encoding
 Returns
 str
 A Beider - Morse phonetic code"""

implementation:
alt_string = phonetic
alt_array = alt_string . split ( '|' )
result = '|'
for i in range ( len ( alt_array ) ) :
    alt = alt_array [ i ]
    if alt and '|' + alt + '|' not in result :
        result += alt + '|'
return result [ 1 : - 1 ]


signature:
def _normalize_lang_attrs ( self , text , strip ) :
    """Remove embedded bracketed attributes .
 This ( potentially ) bitwise - ands bracketed attributes together and adds
 to the end .
 This is applied to a single alternative at a time - - not to a
 parenthesized list .
 It removes all embedded bracketed attributes , logically - ands them
 together , and places them at the end .
 However if strip is true , this can indeed remove embedded bracketed
 attributes from a parenthesized list .
 Parameters
 text : str
 A Beider - Morse phonetic encoding ( in progress )
 strip : bool
 Remove the bracketed attributes ( and throw away )
 Returns
 str
 A Beider - Morse phonetic code
 Raises
 ValueError
 No closing square bracket"""

implementation:
uninitialized = - 1
# all 1 ' s
attrib = uninitialized
while '[' in text :
    bracket_start = text . find ( '[' )
    bracket_end = text . find ( ']' , bracket_start )
    if bracket_end == - 1 :
        raise ValueError ( 'No closing square bracket: text=(' + text + ') strip=(' + text_type ( strip ) + ')' )
    attrib &= int ( text [ bracket_start + 1 : bracket_end ] )
    text = text [ : bracket_start ] + text [ bracket_end + 1 : ]
if attrib == uninitialized or strip :
    return text
elif attrib == 0 : # means that the attributes were incompatible and there is no
# alternative here
    return '[0]'
return text + '[' + str ( attrib ) + ']'


signature:
def _apply_rule_if_compat ( self , phonetic , target , language_arg ) :
    """Apply a phonetic regex if compatible .
 tests for compatible language rules
 to do so , apply the rule , expand the results , and detect alternatives
 with incompatible attributes
 then drop each alternative that has incompatible attributes and keep
 those that are compatible
 if there are no compatible alternatives left , return false
 otherwise return the compatible alternatives
 apply the rule
 Parameters
 phonetic : str
 The Beider - Morse phonetic encoding ( so far )
 target : str
 A proposed addition to the phonetic encoding
 language _ arg : int
 An integer representing the target language of the phonetic
 encoding
 Returns
 str
 A candidate encoding"""

implementation:
candidate = phonetic + target
if '[' not in candidate : # no attributes so we need test no further
    return candidate
# expand the result , converting incompatible attributes to [ 0]
candidate = self . _expand_alternates ( candidate )
candidate_array = candidate . split ( '|' )
# drop each alternative that has incompatible attributes
candidate = ''
found = False
for i in range ( len ( candidate_array ) ) :
    this_candidate = candidate_array [ i ]
    if language_arg != 1 :
        this_candidate = self . _normalize_lang_attrs ( this_candidate + '[' + str ( language_arg ) + ']' , False )
    if this_candidate != '[0]' :
        found = True
        if candidate :
            candidate += '|'
        candidate += this_candidate
# return false if no compatible alternatives remain
if not found :
    return None
# return the result of applying the rule
if '|' in candidate :
    candidate = '(' + candidate + ')'
return candidate


signature:
def _language_index_from_code ( self , code , name_mode ) :
    """Return the index value for a language code .
 This returns l _ any if more than one code is specified or the code is
 out of bounds .
 Parameters
 code : int
 The language code to interpret
 name _ mode : str
 The name mode of the algorithm : ` ` gen ` ` ( default ) ,
 ` ` ash ` ` ( Ashkenazi ) , or ` ` sep ` ` ( Sephardic )
 Returns
 int
 Language code index"""

implementation:
if code < 1 or code > sum ( _LANG_DICT [ _ ] for _ in BMDATA [ name_mode ] [ 'languages' ] ) : # code out of range
    return L_ANY
if ( code & ( code - 1 ) ) != 0 : # choice was more than one language ; use any
    return L_ANY
return code


signature:
def encode ( self , word , language_arg = 0 , name_mode = 'gen' , match_mode = 'approx' , concat = False , filter_langs = False , ) :
    """Return the Beider - Morse Phonetic Matching encoding ( s ) of a term .
 Parameters
 word : str
 The word to transform
 language _ arg : int
 The language of the term ; supported values include :
 - ` ` any ` `
 - ` ` arabic ` `
 - ` ` cyrillic ` `
 - ` ` czech ` `
 - ` ` dutch ` `
 - ` ` english ` `
 - ` ` french ` `
 - ` ` german ` `
 - ` ` greek ` `
 - ` ` greeklatin ` `
 - ` ` hebrew ` `
 - ` ` hungarian ` `
 - ` ` italian ` `
 - ` ` latvian ` `
 - ` ` polish ` `
 - ` ` portuguese ` `
 - ` ` romanian ` `
 - ` ` russian ` `
 - ` ` spanish ` `
 - ` ` turkish ` `
 name _ mode : str
 The name mode of the algorithm :
 - ` ` gen ` ` - - general ( default )
 - ` ` ash ` ` - - Ashkenazi
 - ` ` sep ` ` - - Sephardic
 match _ mode : str
 Matching mode : ` ` approx ` ` or ` ` exact ` `
 concat : bool
 Concatenation mode
 filter _ langs : bool
 Filter out incompatible languages
 Returns
 tuple
 The Beider - Morse phonetic value ( s )
 Raises
 ValueError
 Unknown language
 Examples
 > > > pe = BeiderMorse ( )
 > > > pe . encode ( ' Christopher ' )
 ' xrQstopir xrQstYpir xristopir xristYpir xrQstofir xrQstYfir
 xristofir xristYfir xristopi xritopir xritopi xristofi xritofir
 xritofi tzristopir tzristofir zristopir zristopi zritopir zritopi
 zristofir zristofi zritofir zritofi '
 > > > pe . encode ( ' Niall ' )
 ' nial niol '
 > > > pe . encode ( ' Smith ' )
 ' zmit '
 > > > pe . encode ( ' Schmidt ' )
 ' zmit stzmit '
 > > > pe . encode ( ' Christopher ' , language _ arg = ' German ' )
 ' xrQstopir xrQstYpir xristopir xristYpir xrQstofir xrQstYfir
 xristofir xristYfir '
 > > > pe . encode ( ' Christopher ' , language _ arg = ' English ' )
 ' tzristofir tzrQstofir tzristafir tzrQstafir xristofir xrQstofir
 xristafir xrQstafir '
 > > > pe . encode ( ' Christopher ' , language _ arg = ' German ' , name _ mode = ' ash ' )
 ' xrQstopir xrQstYpir xristopir xristYpir xrQstofir xrQstYfir
 xristofir xristYfir '
 > > > pe . encode ( ' Christopher ' , language _ arg = ' German ' , match _ mode = ' exact ' )
 ' xriStopher xriStofer xristopher xristofer '"""

implementation:
word = normalize ( 'NFC' , text_type ( word . strip ( ) . lower ( ) ) )
name_mode = name_mode . strip ( ) . lower ( ) [ : 3 ]
if name_mode not in { 'ash' , 'sep' , 'gen' } :
    name_mode = 'gen'
if match_mode != 'exact' :
    match_mode = 'approx'
# Translate the supplied language _ arg value into an integer
# representing a set of languages
all_langs = ( sum ( _LANG_DICT [ _ ] for _ in BMDATA [ name_mode ] [ 'languages' ] ) - 1 )
lang_choices = 0
if isinstance ( language_arg , ( int , float , long ) ) :
    lang_choices = int ( language_arg )
elif language_arg != '' and isinstance ( language_arg , ( text_type , str ) ) :
    for lang in text_type ( language_arg ) . lower ( ) . split ( ',' ) :
        if lang in _LANG_DICT and ( _LANG_DICT [ lang ] & all_langs ) :
            lang_choices += _LANG_DICT [ lang ]
        elif not filter_langs :
            raise ValueError ( 'Unknown \'' + name_mode + '\' language: \'' + lang + '\'' )
# Language choices are either all incompatible with the name mode or
# no choices were given , so try to autodetect
if lang_choices == 0 :
    language_arg = self . _language ( word , name_mode )
else :
    language_arg = lang_choices
language_arg2 = self . _language_index_from_code ( language_arg , name_mode )
rules = BMDATA [ name_mode ] [ 'rules' ] [ language_arg2 ]
final_rules1 = BMDATA [ name_mode ] [ match_mode ] [ 'common' ]
final_rules2 = BMDATA [ name_mode ] [ match_mode ] [ language_arg2 ]
result = self . _phonetic ( word , name_mode , rules , final_rules1 , final_rules2 , language_arg , concat , )
result = self . _phonetic_numbers ( result )
return result


signature:
def sim_strcmp95 ( src , tar , long_strings = False ) :
    """Return the strcmp95 similarity of two strings .
 This is a wrapper for : py : meth : ` Strcmp95 . sim ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 long _ strings : bool
 Set to True to increase the probability of a match when the number of
 matched characters is large . This option allows for a little more
 tolerance when the strings are large . It is not an appropriate test
 when comparing fixed length fields such as phone and social security
 numbers .
 Returns
 float
 Strcmp95 similarity
 Examples
 > > > sim _ strcmp95 ( ' cat ' , ' hat ' )
 0.77777
 > > > sim _ strcmp95 ( ' Niall ' , ' Neil ' )
 0.845499999
 > > > sim _ strcmp95 ( ' aluminum ' , ' Catalan ' )
 0.6547619047619048
 > > > sim _ strcmp95 ( ' ATCG ' , ' TAGC ' )
 0.8333334"""

implementation: return Strcmp95 ( ) . sim ( src , tar , long_strings )


signature:
def dist_strcmp95 ( src , tar , long_strings = False ) :
    """Return the strcmp95 distance between two strings .
 This is a wrapper for : py : meth : ` Strcmp95 . dist ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 long _ strings : bool
 Set to True to increase the probability of a match when the number of
 matched characters is large . This option allows for a little more
 tolerance when the strings are large . It is not an appropriate test
 when comparing fixed length fields such as phone and social security
 numbers .
 Returns
 float
 Strcmp95 distance
 Examples
 > > > round ( dist _ strcmp95 ( ' cat ' , ' hat ' ) , 12)
 0.22222
 > > > round ( dist _ strcmp95 ( ' Niall ' , ' Neil ' ) , 12)
 0.1545
 > > > round ( dist _ strcmp95 ( ' aluminum ' , ' Catalan ' ) , 12)
 0.345238095238
 > > > round ( dist _ strcmp95 ( ' ATCG ' , ' TAGC ' ) , 12)
 0.1666667"""

implementation: return Strcmp95 ( ) . dist ( src , tar , long_strings )


signature:
def encode ( self , word ) :
    """Return the Naval Research Laboratory phonetic encoding of a word .
 Parameters
 word : str
 The word to transform
 Returns
 str
 The NRL phonetic encoding
 Examples
 > > > pe = NRL ( )
 > > > pe . encode ( ' the ' )
 ' DHAX '
 > > > pe . encode ( ' round ' )
 ' rAWnd '
 > > > pe . encode ( ' quick ' )
 ' kwIHk '
 > > > pe . encode ( ' eaten ' )
 ' IYtEHn '
 > > > pe . encode ( ' Smith ' )
 ' smIHTH '
 > > > pe . encode ( ' Larsen ' )
 ' lAArsEHn '"""

implementation:
def _to_regex ( pattern , left_match = True ) :
    new_pattern = ''
    replacements = { '#' : '[AEIOU]+' , ':' : '[BCDFGHJKLMNPQRSTVWXYZ]*' , '^' : '[BCDFGHJKLMNPQRSTVWXYZ]' , '.' : '[BDVGJLMNTWZ]' , '%' : '(ER|E|ES|ED|ING|ELY)' , '+' : '[EIY]' , ' ' : '^' , }
    for char in pattern :
        new_pattern += ( replacements [ char ] if char in replacements else char )
    if left_match :
        new_pattern += '$'
        if '^' not in pattern :
            new_pattern = '^.*' + new_pattern
    else :
        new_pattern = '^' + new_pattern . replace ( '^' , '$' )
        if '$' not in new_pattern :
            new_pattern += '.*$'
    return new_pattern
word = word . upper ( )
pron = ''
pos = 0
while pos < len ( word ) :
    left_orig = word [ : pos ]
    right_orig = word [ pos : ]
    first = word [ pos ] if word [ pos ] in self . _rules else ' '
    for rule in self . _rules [ first ] :
        left , match , right , out = rule
        if right_orig . startswith ( match ) :
            if left :
                l_pattern = _to_regex ( left , left_match = True )
            if right :
                r_pattern = _to_regex ( right , left_match = False )
            if ( not left or re_match ( l_pattern , left_orig ) ) and ( not right or re_match ( r_pattern , right_orig [ len ( match ) : ] ) ) :
                pron += out
                pos += len ( match )
                break
    else :
        pron += word [ pos ]
        pos += 1
return pron


signature:
def lcsstr ( self , src , tar ) :
    """Return the longest common substring of two strings .
 Longest common substring ( LCSstr ) .
 Based on the code from
 https : / / en . wikibooks . org / wiki / Algorithm _ Implementation / Strings / Longest _ common _ substring
 : cite : ` Wikibooks : 2018 ` .
 This is licensed Creative Commons : Attribution - ShareAlike 3.0.
 Modifications include :
 - conversion to a numpy array in place of a list of lists
 - conversion to Python 2/3 - safe range from xrange via six
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 str
 The longest common substring
 Examples
 > > > sstr = LCSstr ( )
 > > > sstr . lcsstr ( ' cat ' , ' hat ' )
 ' at '
 > > > sstr . lcsstr ( ' Niall ' , ' Neil ' )
 > > > sstr . lcsstr ( ' aluminum ' , ' Catalan ' )
 ' al '
 > > > sstr . lcsstr ( ' ATCG ' , ' TAGC ' )"""

implementation:
lengths = np_zeros ( ( len ( src ) + 1 , len ( tar ) + 1 ) , dtype = np_int )
longest , i_longest = 0 , 0
for i in range ( 1 , len ( src ) + 1 ) :
    for j in range ( 1 , len ( tar ) + 1 ) :
        if src [ i - 1 ] == tar [ j - 1 ] :
            lengths [ i , j ] = lengths [ i - 1 , j - 1 ] + 1
            if lengths [ i , j ] > longest :
                longest = lengths [ i , j ]
                i_longest = i
        else :
            lengths [ i , j ] = 0
return src [ i_longest - longest : i_longest ]


signature:
def sim ( self , src , tar ) :
    r"""Return the longest common substring similarity of two strings .
 Longest common substring similarity ( : math : ` sim _ { LCSstr } ` ) .
 This employs the LCS function to derive a similarity metric :
 : math : ` sim _ { LCSstr } ( s , t ) = \ frac { | LCSstr ( s , t ) | } { max ( | s | , | t | ) } `
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 Returns
 float
 LCSstr similarity
 Examples
 > > > sim _ lcsstr ( ' cat ' , ' hat ' )
 0.66666
 > > > sim _ lcsstr ( ' Niall ' , ' Neil ' )
 0.2
 > > > sim _ lcsstr ( ' aluminum ' , ' Catalan ' )
 0.25
 > > > sim _ lcsstr ( ' ATCG ' , ' TAGC ' )
 0.25"""

implementation:
if src == tar :
    return 1.0
elif not src or not tar :
    return 0.0
return len ( self . lcsstr ( src , tar ) ) / max ( len ( src ) , len ( tar ) )


signature:
def needleman_wunsch ( src , tar , gap_cost = 1 , sim_func = sim_ident ) :
    """Return the Needleman - Wunsch score of two strings .
 This is a wrapper for : py : meth : ` NeedlemanWunsch . dist _ abs ` .
 Parameters
 src : str
 Source string for comparison
 tar : str
 Target string for comparison
 gap _ cost : float
 The cost of an alignment gap ( 1 by default )
 sim _ func : function
 A function that returns the similarity of two characters ( identity
 similarity by default )
 Returns
 float
 Needleman - Wunsch score
 Examples
 > > > needleman _ wunsch ( ' cat ' , ' hat ' )
 2.0
 > > > needleman _ wunsch ( ' Niall ' , ' Neil ' )
 1.0
 > > > needleman _ wunsch ( ' aluminum ' , ' Catalan ' )
 -1.0
 > > > needleman _ wunsch ( ' ATCG ' , ' TAGC ' )
 0.0"""

implementation: return NeedlemanWunsch ( ) . dist_abs ( src , tar , gap_cost , sim_func )