| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Lingua::Phonology; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 8 | $VERSION = 0.35_03; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 8 | ▶ | use warnings::register; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 8 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 8 | ▶ | use Lingua::Phonology::Common; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 8 | ▶ | use Lingua::Phonology::Features; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 8 | ▶ | use Lingua::Phonology::Segment; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 8 | ▶ | use Lingua::Phonology::Symbols; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 8 | ▶ | use Lingua::Phonology::Rules; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 8 | ▶ | use Lingua::Phonology::Syllable; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | Lingua::Phonology - an object model for lingistic phonology and sound change | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | use Lingua::Phonology; | ||||||||||||||||||||
| 27 | $phono = new Lingua::Phonology; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # Get sub-objects | ||||||||||||||||||||
| 30 | $features = $phono->features; | ||||||||||||||||||||
| 31 | $symbols = $phono->symbols; | ||||||||||||||||||||
| 32 | $rules = $phono->rules; | ||||||||||||||||||||
| 33 | $syllabification = $phono->syllable; | ||||||||||||||||||||
| 34 | $segment = $phono->segment; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # Load phonology defaults | ||||||||||||||||||||
| 37 | $phono->loadfile; | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # Load a phonology definition from a file | ||||||||||||||||||||
| 40 | $phono->loadfile('language.xml'); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | # Save phonology definition to a file | ||||||||||||||||||||
| 43 | $phono->savefile('language.xml'); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =head1 ABSTRACT | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | Lingua::Phonology is a unified module for handling phonological descriptions | ||||||||||||||||||||
| 48 | and units. It includes sub-modules for hierarchical (feature-geometric) sets of | ||||||||||||||||||||
| 49 | features, phonetic or orthographic symbols, individual segments, linguistic | ||||||||||||||||||||
| 50 | rules, syllabification algorithms, etc. It is written as an object-oriented | ||||||||||||||||||||
| 51 | module, wherein one will generally have a single object for the list of | ||||||||||||||||||||
| 52 | features, one for the phonetic symbols, one for the set of rules, etc., and | ||||||||||||||||||||
| 53 | multiple segment objects to be programatically manipulated. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # Remainder of POD is after the __END__ token | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | sub err ($) { warnings::warnif(shift); return; }; | ||||||||||||||||||
|
Subroutine
err: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | # Constructor - creates new (empty) objects | ||||||||||||||||||||
| 62 | 16 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 63 | 16 | my $proto = shift; | |||||||||||||||||||
| 64 | 16 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 65 | 16 | my $self = bless {}, $class; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 16 | $self->{FEATURES} = Lingua::Phonology::Features->new(); | |||||||||||||||||||
| 68 | 16 | $self->{SYMBOLS} = Lingua::Phonology::Symbols->new($self->{FEATURES}); | |||||||||||||||||||
| 69 | 16 | $self->{RULES} = Lingua::Phonology::Rules->new(); | |||||||||||||||||||
| 70 | 16 | $self->{SYLLABLE} = Lingua::Phonology::Syllable->new(); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 16 | return $self; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # Next section deals w/ making accessor methods | ||||||||||||||||||||
| 76 | # Accessor methods to create and/or iterate over (order may be significant!) | ||||||||||||||||||||
| 77 | 8 | my @accessors = qw/features symbols syllable rules/; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # Object types to create/expect--all accessors must be defined here | ||||||||||||||||||||
| 80 | 8 | my %classes = ( | |||||||||||||||||||
| 81 | features => 'Lingua::Phonology::Features', | ||||||||||||||||||||
| 82 | symbols => 'Lingua::Phonology::Symbols', | ||||||||||||||||||||
| 83 | rules => 'Lingua::Phonology::Rules', | ||||||||||||||||||||
| 84 | syllable => 'Lingua::Phonology::Syllable' | ||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # Continuations (if needed) | ||||||||||||||||||||
| 88 | my %continue = ( | ||||||||||||||||||||
| 89 | 1 | ▶ | features => sub { $_[0]->{SYMBOLS}->features($_[1]) if defined $_[1] } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 8 | ); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | # Create methods for each of the accessors | ||||||||||||||||||||
| 93 | 8 | ▶ | while (my ($name, $class) = each %classes) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 32 | my $key = uc $name; | |||||||||||||||||||
| 95 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 96 | 316 | ▶ | *$name = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 97 | 316 | ▶ | return $_[0]->{$key} unless @_ > 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 4 | my ($self, $val) = @_; | |||||||||||||||||||
| 99 | 4 | ▶ | croak "Argument to $name() not a $class" unless _is($val, $class); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 4 | $self->{$key} = $val; | |||||||||||||||||||
| 101 | 4 | ▶ | $continue{$name}->(@_) if exists $continue{$name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 4 | return $self->{$key}; | |||||||||||||||||||
| 103 | 32 | }; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # Get blank segments | ||||||||||||||||||||
| 107 | 8 | ▶ | sub segment { | ||||||||||||||||||
|
Subroutine
segment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 108 | 8 | my $self = shift; | |||||||||||||||||||
| 109 | 8 | my $seg = Lingua::Phonology::Segment->new($self->{FEATURES}); | |||||||||||||||||||
| 110 | 8 | $seg->symbolset($self->{SYMBOLS}); | |||||||||||||||||||
| 111 | 8 | return $seg; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # Load a complete phonology definition from a file | ||||||||||||||||||||
| 115 | 5 | ▶ | sub loadfile { | ||||||||||||||||||
|
Subroutine
loadfile: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||
| 116 | 5 | my ($self, $file) = @_; | |||||||||||||||||||
| 117 | 5 | my $err = 0; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | # If called with one argument, load defaults. Submodules implement | ||||||||||||||||||||
| 120 | # "default" in different ways--that's their problem. They must do the right | ||||||||||||||||||||
| 121 | # thing when loadfile() is called on them with no arguments. | ||||||||||||||||||||
| 122 | 5 | ▶ | if (not defined $file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 3 | for (@accessors) { | |||||||||||||||||||
| 124 | 12 | ▶ | $self->$_->loadfile or $err = 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # When given an actual filename | ||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||
| 130 | 2 | my $parse; | |||||||||||||||||||
| 131 | 2 | eval { $parse = _parse_from_file $file }; | |||||||||||||||||||
| 132 | 2 | ▶ | return err($@) unless $parse; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 2 | for (@accessors) { | |||||||||||||||||||
| 135 | 8 | ▶ | $self->$_->_load_from_struct($parse->{$_}) or $err = 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # $return should | ||||||||||||||||||||
| 140 | 5 | ▶ | return $err ? () : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | # Save a total phonology. We do this by concatenating the strings returned by | ||||||||||||||||||||
| 144 | # the to_str() methods in the various submodules. | ||||||||||||||||||||
| 145 | 2 | ▶ | sub savefile { | ||||||||||||||||||
|
Subroutine
savefile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 146 | 2 | my ($self, $file) = @_; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 2 | my $str = ''; | |||||||||||||||||||
| 149 | 2 | for (@accessors) { | |||||||||||||||||||
| 150 | 8 | $str .= "\n" . $self->$_->_to_str; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | 2 | $str = "<?xml version=\"1.0\" standalone=\"yes\" ?>\n<phonology>$str</phonology>\n"; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 2 | eval { $file = _to_handle($file, '>') }; | |||||||||||||||||||
| 155 | 2 | ▶ | return err($@) if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 2 | print $file $str; | |||||||||||||||||||
| 158 | 2 | return $str; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 8 | 1; | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||