PanPhon: IPA and Phonological Feature Tools

repository·master·Indexed 43 days ago

https://github.com/dmort27/panphon

A Python library and database for mapping International Phonetic Alphabet (IPA) segments to articulatory phonological features. PanPhon provides tools for feature vector extraction, segment manipulation via the Segment class, and various distance metrics—including Levenshtein and feature-based edit distances—via the Distance class. It includes utilities for validating IPA text, aligning wordlists, and generating comprehensive IPA feature files.

Tokens
13.9K
Snippets
62
Records
68
Agent score
55%

What's inside PanPhon

  1. Understand the `Modifiers` data structure

    master

    The Modifiers object manages how diacritics (modifiers) affect phonological features and IPA strings. It is returned by generate_modifiers().

    Key attributes:

    • prefix_modifiers: A list of strings representing modifiers that appear before a base phoneme (e.g., aspiration).
    • postfix_modifiers: A list of strings representing modifiers that appear after a base phoneme.
    • transforms: A dictionary mapping a modifier string to a tuple containing:
      1. An np.ndarray representing the feature change (the transform vector).
      2. A Callable that takes the current phoneme string and returns the new phoneme string with the modifier applied.
  2. Understand the `FeatureVectors` data structure

    master

    The FeatureVectors object is the core data container returned by generate_feature_vectors(). It maps phonemes to their numeric representations and vice versa.

    Key attributes:

    • vector_map: A dictionary mapping IPA strings (e.g., 'p') to their np.ndarray feature vectors.
    • phoneme_map: A dictionary mapping feature vector tuples (as tuple[int, ...] ) to a list of corresponding IPA strings.
    • feature_names: A list of the names of the phonological features.
    • phonemes: A list of the base IPA phonemes.
    • feature_vectors: A NumPy array containing all feature vectors.
  3. Configure sort order for the Diacritic Application Tool

    master

    The sort_order.yml file controls the ordering of segments in the output of the generate_ipa_all.py script. It is a sequence of maps where the order of features determines the priority of sorting.

    Each map in the sequence must contain:

    • name: The name of the feature to sort by.
    • reverse: A boolean (True or False) specifying whether the sort order for that feature should be reversed.

    A schema for validation is provided in sort_order_schema_.yml.

    # Example structure of sort_order.yml
    - name: feature_name
      reverse: False
    - name: another_feature
      reverse: True
  4. Configure diacritic and modifier rules in YAML

    master

    The generate_ipa_all.py tool is controlled by diacritic_definitions.yml. This file contains two main sections:

    diacritics

    A list of rules for applying modifiers to bases. Each rule includes:

    • marker: The Unicode diacritic/modifier.
    • name: The name of the resulting series.
    • postion: Position relative to base (pre or post).
    • conditions: List of feature specifications required for application.
    • exclude: Segments to ignore even if they match conditions.
    • content: Feature specifications to set if applied.

    combinations

    A list of rules for combining existing diacritic rules. Each rule includes:

    • name: The name of the combined category.
    • combines: A sequence of names from the diacritics section.
  5. Define diacritics and modifier rules in diacritic_definitions.yml

    master

    The diacritic_definitions.yml file is used to control the Diacritic Application Tool by defining how diacritics and modifiers are applied to IPA segments based on phonological features. The file is organized into two main sections: diacritics and combinations.

    Diacritics

    The diacritics section is a list of rules for applying modifiers to bases. Each rule is a map containing:

    • marker: The Unicode diacritic or modifier.
    • name: The name of the series derived from applying the diacritic/modifier.
    • postion: The position of the diacritic relative to the base (pre or post).
    • conditions: A list of conditions (associative arrays of feature specifications) that must be met for the diacritic to be applied.
    • exclude: A sequence of segments to be excluded from application, even if they match the conditions.
    • content: A map of feature specifications that will be set if the diacritic is applied.

    Combinations

    The combinations section allows you to group existing diacritic rules. Each rule is a map containing:

    • name: The name of the combined category.
    • combines: A sequence of names referencing rules defined in the diacritics section.

    A schema for validation is provided in diacritic_definitions_schema.yml.

  6. Use the generate_ipa_all script to generate all IPA segments

    master

    The generate_ipa_all.py script is a CLI tool used to generate a comprehensive list of IPA segments by applying diacritics and combinations to a set of base IPA segments. It uses a CSV file of base segments, a YAML file defining diacritics and combinations, and a YAML file defining the sort order for the output.

    Input File Requirements

    1. Base IPA Segments (bases): A CSV file where the first column is ipa (the segment string) and subsequent columns are feature names and their values.
    2. Diacritic Definitions (--dia): A YAML file containing:
      • diacritics: A list of diacritic objects with name, marker, position (pre or post), conditions (feature requirements), exclude (forms to ignore), and content (feature overrides).
      • combinations: A list of named sequences of diacritics defined by name and combines (a list of diacritic names).
    3. Sort Order (--sort-order): A YAML file defining the order in which segments should be sorted in the output. It uses a list of objects with name (feature name) and reverse (boolean).

    Output

    A CSV file containing all generated segments (bases + modified segments) with the same columns as the input base file.

    python panphon/bin/generate_ipa_all.py <bases_csv> <output_csv> --dia <diacritics_yml> --sort-order <sort_order_yml>
  7. Calculate articulatory similarity with panphon.distance

    master

    The panphon.distance.Distance class provides several methods for computing the distance or similarity between Unicode IPA strings based on their articulatory features.

    Available methods:

    • levenshtein_distance: Python implementation of Levenshtein distance.
    • fast_levenshtein_distance: Faster C implementation of Levenshtein distance.
    • dogol_prime_distance: Fast Levenshtein distance after collapsing segments into Dogolpolsky's equivalence classes.
    • feature_edit_distance: Edit distance where each feature-edit has cost 1/22 (unspecified to specified costs 1/44).
    • hamming_feature_edit_distance: Edit distance where feature-edit costs are 1/22 (including unspecified to specified), and insertions/substitutions cost 1.
    • weighted_feature_edit_distance: Edit distance where feature-edit costs are weighted by class and subjective variability.
    import panphon.distance
    
    dst = panphon.distance.Distance()
    # Example using dogol_prime_distance
    dist = dst.dogol_prime_distance(u'pops', u'bobz') # 0
  8. Calculate similarity and edit distance with the Distance class

    master

    The panphon.distance.Distance class provides several methods for computing the distance between Unicode IPA strings, ranging from standard Levenshtein distance to articulatory feature-based metrics.

    Available Methods

    • levenshtein_distance(s1, s2): Standard Python implementation of Levenshtein distance.
    • fast_levenshtein_distance(s1, s2): A high-performance C implementation of Levenshtein distance.
    • dolgo_prime_distance(s1, s2): Fast Levenshtein distance using enhanced Dolgopolsky equivalence classes.
    • feature_edit_distance(s1, s2): Edit distance where feature-edit costs are fixed (1/22 per edit; 1/44 for unspecified to specified).
    • hamming_feature_edit_distance(s1, s2): Edit distance using Hamming distance for substitutions (cost 1/22 per feature edit; insertions/substitutions cost 1).
    • weighted_feature_edit_distance(s1, s2): Edit distance where feature-edit costs are weighted by class and variability.
    import panphon.distance
    
    dst = panphon.distance.Distance()
    
    # Example: Dolgo prime distance
    dist = dst.dolgo_prime_distance(u'pops', u'bobz')
    print(dist)
  9. Access IPA segment features with FeatureTable

    master

    The panphon.panphon.FeatureTable class provides an API to access the segment-feature relationships encoded in the IPA database. You can use it to check if a segment matches specific features, retrieve individual segments from a string, or get the full set of features for every segment in a word.

    Key methods:

    • ftr_match(feature_set, segment): Returns True if the segment matches the provided set of features (e.g., set([(u'+', u'syl')])).
    • segs(word): Returns a list of Unicode segments for a given word.
    • word_fts(word): Returns a list of sets, where each set contains the features for the corresponding segment in the word.
    import panphon.panphon as panphon
    
    ft = panphon.FeatureTable()
    
    # Check if a segment matches a feature set
    matches = ft.ftr_match(set([(u'+', u'syl')]), u'a') # True
    
    # Get segments from a word
    segments = ft.segs(u'pʲãk') # [u'p\u02b2', u'a\u0303', u'k']
    
    # Get feature sets for each segment in a word
    feature_sets = ft.word_fts(u'pʲãk')
  10. Convert words to NumPy feature arrays with word2array

    master

    The panphon.word2array function converts a word (represented as a list of feature sets from FeatureTable().word_fts()) into a NumPy array. Each row in the array represents a segment, and each column represents one of the specified features.

    Values in the array are typically represented as integers (e.g., 1 for +, -1 for -).

    import panphon
    
    ft = panphon.FeatureTable()
    # Convert 'snik' to an array containing only 'syl', 'son', and 'cont' features
    array = panphon.word2array(['syl', 'son', 'cont'], ft.word_fts(u'snik'))
    # Resulting array:
    # array([[-1, -1,  1],
    #        [-1,  1, -1],
    #        [ 1,  1,  1],
    #        [-1, -1, -1]])
  11. Manipulate and query segments with the Segment class

    master

    The panphon.segment.Segment class represents an analyzed IPA segment. It provides a dictionary-like interface for feature manipulation and supports set-like operations for matching and intersection.

    Construction

    Construct a Segment by providing a list of feature names (to define order) and either a dictionary of values or a formatted string.

    • Values are integers: -1 (for -), 0 (for 0), or 1 (for +).
    • ftstr format: [+feature1, -feature2]

    Operations

    • Access/Update: Use segment['feature_name'] to get or set values.
    • Match (match or >=): Checks if the segment has a superset of the provided features.
    • Intersection (intersection or &): Returns a new Segment containing only the shared features.
    • Vectors: Use .numeric() for integer lists or .strings() for ['+', '-', '0'] lists.
    from panphon.segment import Segment
    
    # Construction
    # Method 1: Using a dictionary
    seg = Segment(['syl', 'son', 'cont'], {'syl': -1, 'son': -1, 'cont': 1})
    
    # Method 2: Using a feature string
    seg = Segment(['syl', 'son', 'cont'], ftstr='[-syl, -son, +cont]')
    
    # Querying and Updating
    print(seg['syl'])      # -1
    seg['son'] = 1          # Update feature
    seg.update({'cont': -1}) # Bulk update
    
    # Set Operations
    # Check if segment matches a pattern
    print(seg >= {'son': 1, 'cont': -1}) 
    
    # Find intersection of features
    shared = seg & {'syl': -1, 'son': 1}
    
    # Get vector representations
    print(seg.numeric())   # [-1, 1, -1]
    print(seg.strings())   # ['-', '+', '-']
  12. Reference: IPA Phonological Features

    master

    The following features are used in the PanPhon IPA Character Tables (ipa_bases.csv and ipa_all.csv):

    FeatureDescription
    sylsyllabic
    sonsonorant
    consconsonantal
    contcontinuant
    delreldelayed release
    latlateral
    nasnasal
    stridstrident
    voivoice
    sgspread glottis
    cgconstricted glottis
    antanterior
    corcoronal
    distrdistributed
    lablabial
    hihigh (vowel/consonant)
    lolow (vowel/consonant)
    backback
    roundround
    velaricvelaric airstream mechanism (click)
    tensetense
    longlong