| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Lingua::Phonology::Functions; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Lingua::Phonology::Functions | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use Lingua::Phonology; | ||||||||||||||||||||||||||||
| 12 | use Lingua::Phonology::Functions qw/:all/; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Lingua::Phonology::Functions contains a suite of functions that can be | ||||||||||||||||||||||||||||
| 17 | exported to make it easier to write linguistic rules. I hope to have a | ||||||||||||||||||||||||||||
| 18 | function here for each broad, sufficiently common linguistic process. So if | ||||||||||||||||||||||||||||
| 19 | there are any missing here that you think should be included, feel free to | ||||||||||||||||||||||||||||
| 20 | contact the author. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | use warnings::register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | use Lingua::Phonology::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | require Exporter; | |||||||||||||||||||||||||||
| 31 | 8 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 8 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 34 | assimilate | ||||||||||||||||||||||||||||
| 35 | flat_assimilate | ||||||||||||||||||||||||||||
| 36 | adjoin | ||||||||||||||||||||||||||||
| 37 | flat_adjoin | ||||||||||||||||||||||||||||
| 38 | copy | ||||||||||||||||||||||||||||
| 39 | flat_copy | ||||||||||||||||||||||||||||
| 40 | dissimilate | ||||||||||||||||||||||||||||
| 41 | change | ||||||||||||||||||||||||||||
| 42 | metathesize | ||||||||||||||||||||||||||||
| 43 | metathesize_feature | ||||||||||||||||||||||||||||
| 44 | delete_seg | ||||||||||||||||||||||||||||
| 45 | insert_after | ||||||||||||||||||||||||||||
| 46 | insert_before | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | 8 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 8 | our $VERSION = 0.21; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub err ($) { _err($_[0]) if warnings::enabled() }; | ||||||||||||||||||||||||||
|
Subroutine
err: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Join two segments for a feature | ||||||||||||||||||||||||||||
| 55 | 16 | ▶ | sub assimilate { | ||||||||||||||||||||||||||
|
Subroutine
assimilate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 16 | my ($feature, $seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 57 | 16 | ▶ | return unless _is_seg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_seg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_seg($seg1) and &_is_seg($seg2)
&_is_seg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_seg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 58 | 15 | $seg2->delink($feature); | |||||||||||||||||||||||||||
| 59 | 15 | $seg2->$feature( $seg1->value_ref($feature) ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Synonym | ||||||||||||||||||||||||||||
| 63 | 8 | *adjoin = \&assimilate; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Copy vals but not references | ||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub copy { | ||||||||||||||||||||||||||
|
Subroutine
copy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 2 | my ($feature, $seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 68 | 2 | ▶ | return unless _is_seg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_seg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_seg($seg1) and &_is_seg($seg2)
&_is_seg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_seg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 69 | 1 | $seg2->delink($feature); | |||||||||||||||||||||||||||
| 70 | 1 | $seg2->$feature( $seg1->$feature ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Assimilate only the top value of a feature | ||||||||||||||||||||||||||||
| 74 | 74 | ▶ | sub flat_assimilate { | ||||||||||||||||||||||||||
|
Subroutine
flat_assimilate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 74 | my ($feature, $seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 76 | 74 | ▶ | return unless _is_seg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_seg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_seg($seg1) and &_is_seg($seg2)
&_is_seg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_seg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 77 | 73 | my @rv = $seg1->value_ref($feature); | |||||||||||||||||||||||||||
| 78 | 73 | $seg2->value_ref($feature, $rv[0]); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 8 | *flat_adjoin = \&flat_assimilate; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Same, but copy | ||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub flat_copy { | ||||||||||||||||||||||||||
|
Subroutine
flat_copy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 2 | my ($feature, $seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 85 | 2 | ▶ | return unless _is_seg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_seg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_seg($seg1) and &_is_seg($seg2)
&_is_seg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_seg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 86 | 1 | my @rv = $seg1->$feature; | |||||||||||||||||||||||||||
| 87 | 1 | $seg2->$feature($rv[0]); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Make two segs disagree | ||||||||||||||||||||||||||||
| 91 | 2 | ▶ | sub dissimilate { | ||||||||||||||||||||||||||
|
Subroutine
dissimilate: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 2 | my ($feature, $seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 93 | 2 | ▶ | return unless _is_seg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_seg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_seg($seg1) and &_is_seg($seg2)
&_is_seg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_seg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $seg1->$feature ? $seg2->$feature(\0) : $seg2->$feature(\1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | return $seg2->$feature; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Change a segment into a symbol | ||||||||||||||||||||||||||||
| 99 | 16 | ▶ | sub change { | ||||||||||||||||||||||||||
|
Subroutine
change: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 16 | my ($seg, $sym) = @_; | |||||||||||||||||||||||||||
| 101 | 16 | ▶ | return unless _is_seg $seg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 15 | $seg->delink($_) for keys %{$seg->all_values}; | |||||||||||||||||||||||||||
| 103 | 15 | my %new_vals = $seg->symbolset->prototype($sym)->all_values; | |||||||||||||||||||||||||||
| 104 | 15 | $seg->$_($new_vals{$_}) for (keys %new_vals); | |||||||||||||||||||||||||||
| 105 | 15 | return 1; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Switch the position of two segments | ||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub metathesize { | ||||||||||||||||||||||||||
|
Subroutine
metathesize: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 2 | my ($seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 111 | # This will ensure that we get the right kind of segs | ||||||||||||||||||||||||||||
| 112 | 2 | ▶ | return unless _is_ruleseg $seg1 && _is_ruleseg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_ruleseg($seg1) and &_is_ruleseg($seg2)
MC/DC50%1/2
&_is_ruleseg($seg1) and &_is_ruleseg($seg2)
&_is_ruleseg($seg1)
&_is_ruleseg($seg2)
Truth table66%2/3
A and B
A= &_is_ruleseg($seg1)B= &_is_ruleseg($seg2)
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Decide which direction we're going | ||||||||||||||||||||||||||||
| 115 | 1 | ▶ | if ($seg1->_RULE->{direction} eq 'rightward') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 116 | 1 | $seg1->INSERT_LEFT($seg2->duplicate); | |||||||||||||||||||||||||||
| 117 | 1 | $seg2->clear; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | elsif ($seg1->_RULE->{direction} eq 'leftward') { | ||||||||||||||||||||||||||||
| 120 | 0 | $seg2->INSERT_RIGHT($seg1->duplicate); | |||||||||||||||||||||||||||
| 121 | 0 | $seg1->clear; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 1 | return 1; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Switch the feature of two segments | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | sub metathesize_feature { | ||||||||||||||||||||||||||
|
Subroutine
metathesize_feature: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 2 | my ($feature, $seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | return unless _is_seg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_seg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_seg($seg1) and &_is_seg($seg2)
&_is_seg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_seg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Capture existing vals | ||||||||||||||||||||||||||||
| 132 | 1 | my @temp1 = $seg1->$feature; | |||||||||||||||||||||||||||
| 133 | 1 | my @temp2 = $seg2->$feature; | |||||||||||||||||||||||||||
| 134 | 1 | $seg1->delink($feature); | |||||||||||||||||||||||||||
| 135 | 1 | $seg2->delink($feature); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Switcheroo | ||||||||||||||||||||||||||||
| 138 | 1 | $seg1->$feature(@temp2); | |||||||||||||||||||||||||||
| 139 | 1 | $seg2->$feature(@temp1); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Delete a seg | ||||||||||||||||||||||||||||
| 143 | 2 | ▶ | sub delete_seg { | ||||||||||||||||||||||||||
|
Subroutine
delete_seg: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | return unless _is_seg $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | $_[0]->clear; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Insert a seg after a seg | ||||||||||||||||||||||||||||
| 149 | 2 | ▶ | sub insert_after { | ||||||||||||||||||||||||||
|
Subroutine
insert_after: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 2 | my ($seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 151 | 2 | ▶ | return unless _is_ruleseg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_ruleseg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_ruleseg($seg1) and &_is_seg($seg2)
&_is_ruleseg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_ruleseg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 152 | 1 | $seg1->INSERT_RIGHT($seg2); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Insert a seg before a seg | ||||||||||||||||||||||||||||
| 156 | 2 | ▶ | sub insert_before { | ||||||||||||||||||||||||||
|
Subroutine
insert_before: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 2 | my ($seg1, $seg2) = @_; | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | return unless _is_ruleseg $seg1 && _is_seg $seg2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&_is_ruleseg($seg1) and &_is_seg($seg2)
MC/DC50%1/2
&_is_ruleseg($seg1) and &_is_seg($seg2)
&_is_ruleseg($seg1)
&_is_seg($seg2)
Truth table66%2/3
A and B
A= &_is_ruleseg($seg1)B= &_is_seg($seg2)
| |||||||||||||||||||||||||||||
| 159 | 1 | $seg1->INSERT_LEFT($seg2); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||