#!/usr/local/bin/perl # # David MacKay Jan 2000 # # dictator.p - converts text to dictatable text phrases. # # see also dasher/dictate # $minphraselength = 5 ; # method: try preferred length , then increase to maxphrase length, # then go down to preferred min, looking for a good break point # rule: anywhere with punctuation is automatically a break, unless that # makes the string too short. $preferredmin = 5 ; $preferredlength = 7 ; $maxphraselength = 10 ; # I think 10 is the max, people $verbose = 1 ; $hyphentospace = 1 ; # verbose = 1 -> annotated to illustrate the rules being used. # Any line that begins # P # is a break that was forced by the rule "accept almost all punctuation # as giving a good break point" # # Any line beginning # | # is a line broken at one of the magic words that I reckoned would be good # break points # # Any line beginning # ? # is a break that was forced by the constraints on phrase length, and # that could not necessarily be made at a magic word. If it was, it was fluke. # read command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # sanity checks if ( $preferredlength > $maxphraselength ) { $preferredlength = $maxphraselength ; } @a = <> ; $_ = join ( '' , @a ) ; # get rid of newlines s/\n/ /g ; # get rid of special punctuation s/\Mr\./Mr/g ; s/\Ms\./Ms/g ; s/\Mrs\./Mrs/g ; # remove within-word hyphens if( $hyphentospace ) { s/(\w)\-(\w)/$1 $2/g ; } else { s/(\w)\-(\w)/$1$2/g ; } # # modify silly multiple punctuation s/\s*[\-]+\s*/ - /g ; s/\?\"/?/g ; s/\"\?/?/g ; # mark punctuation, and separate it by a space s/([\-\!\"\(\)\?\.\,\;\:])/ $1_PUNC_/g ; # split there, such that each sentence ends like this . @c = split( '_PUNC_' ) ; # foreach $_ ( @c ) { foreach $sentence ( 0..$#c ) { $_ = $c[$sentence] ; # remove from the sentence any punctuation that is not going to be # included in the dictation, for example commas, # but that was useful in parsing s/([\"\(\)\,\'\-])//g ; @words = split ; # split on white space $I = ( $#words ) + 1 ; print "this sentence $_ has $I words including punctuation \n" if ($verbose>4) ; if ( $I < $minphraselength ) { # too short a phrase, combine it with the next $c[$sentence+1] = $_." ".$c[$sentence+1] ; } else { $current = 0 ; while ( $current < $I-1 ) { # $left = $I - $current ; print "\t there are still $left words to produce\n" if ($verbose>2) ; $boundaryfound = 0 ; if ( $left > $maxphraselength ) { # hunt for a good break point $preferredstart = $current+$preferredlength+1 ; $remainder = $I - $preferredstart ; print "if I started at $preferredstart the remainder would be of length $remainder\n" if ($verbose>2) ; if ($remainder < $minphraselength ) { $preferredstart += $remainder - $minphraselength ; } # hunt for a good break point $thismax = $current+$maxphraselength+1 ; $remainder = $I - $thismax ; print "if I cut at $thismax the remainder would be of length $remainder\n" if ($verbose>2) ; if ($remainder < $minphraselength ) { $thismax += $remainder - $minphraselength ; } # make list of places to try $i = 0 ; for ( $candidate = $preferredstart ; $candidate <= $thismax ; $candidate ++ ) { $candidatelist[$i] = $candidate ; $i ++ ; } for ( $candidate = $preferredstart - 1 ; $candidate >= $current+$preferredmin ; $candidate -- ) { $candidatelist[$i] = $candidate ; $i ++ ; } WORDLOOP: for ( $ii = 0 ; $ii < $i ; $ii ++ ) { # see if this would make a good start of the # next phrase $candidate = $candidatelist[$ii] ; $w = $words[$candidate] ; print "testing $w ($candidate) " if ($verbose > 1 ) ; if ( ( $w =~ /\bthe\b/ ) || ( $w =~ /\bby\b|\bon\b|\bhow\b/ ) || ( $w =~ /\band\b|\bas\b|\bnow\b|\bat\b|\blike\b/ ) || ( $w =~ /\bthat\b|\bany\b/ ) || ( $w =~ /\bto\b|\byou\b|\bthey\b|\bcan\b/ ) || ( $w =~ /\bwith\b|\bI\b|\bwe\b|\ba\b/ ) || ( $w =~ /\ban\b|\bbut\b|\bin\b|\bwas\b/ ) || ( $w =~ /\bis\b|\bwill\b/ ) || ( $w =~ /\bshall\b|\bshe\b|\bhe\b|\bher\b|\bwho\b|\bwhile\b/ ) || ( $w =~ /\bwhich\b|\bof\b|\bfor\b/ ) ) { $boundaryfound = 1 ; last WORDLOOP ; } } if ($verbose > 1 ) { if ( $boundaryfound ) { print "yes! \n" } else { print "no\n" } # } if (!$boundaryfound) { # use the preferred length $candidate = $preferredstart ; } } else { # # just blurt it all out $candidate = $I ; # } # blurt for ( $_ = "" ; $current < $candidate ; $current ++ ) { $_ .= "$words[$current] " ; } $_ .= "\n" ; if ($verbose > 0 ) { if ($boundaryfound ) { # mark the fact that we have chosen this boundary $_ .= "| " ; } elsif ($candidate == $I ) { # mark the fact that this boundary was chosen by punctuation $_ .= "P " ; } else { # mark the fact that this boundary was chosen as a last resort $_ .= "? " ; } } # get rid of punctuation that is meant to be lost s/([\(\)\,\'])//g ; print ; } } # end if I<=minphraselength }