------------------------------------------------------------------------------- |-- Module : Text.Hyphenation.Pattern-- Copyright : (C) 2012 Edward Kmett,-- License : BSD-style (see the file LICENSE)---- Maintainer : Edward Kmett <ekmett@gmail.com>-- Stability : provisional-- Portability : portable------------------------------------------------------------------------------moduleText.Hyphenation.Pattern(-- * Pattern file supportPatterns,insertPattern,lookupPattern,scorePattern,parsePatterns)whereimportqualifiedData.IntMapasIMimportData.MonoidimportPreludehiding(lookup)importData.Char(digitToInt,isDigit)dataPatterns=Patterns[Int](IM.IntMapPatterns)derivingShowinstanceMonoidPatternswheremempty=Patterns[]IM.emptyPatternspsm`mappend`Patternsqsn=Patterns(zipMaxpsqs)(IM.unionWithmappendmn)-- | Tallies the hyphenation scores for a word considering all tails.lookupPattern::String->Patterns->[Int]lookupPatternxs0=init.tail.go('.':xs0++".")wherego[](Patternsys_)=ysgoxxs@(_:xs)t=zipMax(go1xxst)(0:goxst)go1[](Patternsys_)=ysgo1(x:xs)(Patternsysm)=caseIM.lookup(fromEnumx)mofJustt'->zipMaxys(go1xst')Nothing->ys-- | Insert a Knuth-Liang hyphenation pattern into the trie-- .-- * @.@ denotes the start or end of the input-- .-- * @0-9@ are used to denote hyphenation or dehyphenation depending on whether or not they are even (no hyphen) or odd (hyphen allowed).-- .-- Patterns are overlaid and the maximum value at each location is used.-- this allows you to implement a finite number of precedences between hyphenation rules---- (e.g. @3foo.@ indicates that the suffix '-foo' should be hyphenated with precedence 3.)insertPattern::String->Patterns->PatternsinsertPatterns0=go(charss0)wherepts=scorePatterns0go[](Patterns_m)=Patternsptsmgo(x:xs)(Patternsnm)=Patternsn(IM.insertWith(\_->goxs)(fromEnumx)(mkxs)m)mk[]=PatternsptsIM.emptymk(x:xs)=Patterns[](IM.singleton(fromEnumx)(mkxs))-- | Parse one pattern per line from an input string---- @hyph-utf8@ supplies these files UTF-8 encoded in the @txt@ folder with a @.pat.txt@ extensionparsePatterns::String->PatternsparsePatterns=foldrinsertPatternmempty.lineschars::String->Stringchars=filter(\x->(x<'0'||x>'9'))scorePattern::String->[Int]scorePattern[]=[0]scorePattern(x:ys)|isDigitx=digitToIntx:ifnullysthen[]elsescorePattern(tailys)|otherwise=0:scorePatternyszipMax::[Int]->[Int]->[Int]zipMax(x:xs)(y:ys)=maxxy:zipMaxxsyszipMax[]ys=yszipMaxxs[]=xs