semiring 0.1.1 → 0.2
raw patch · 6 files changed
+37/−9 lines, 6 files
Files
- NLP/Semiring.hs +6/−1
- NLP/Semiring/Derivation.hs +17/−6
- NLP/Semiring/Prob.hs +1/−1
- NLP/Semiring/Viterbi.hs +6/−0
- NLP/Semiring/ViterbiNBestDerivation.hs +6/−0
- semiring.cabal +1/−1
NLP/Semiring.hs view
@@ -8,7 +8,8 @@ Monoid(..), Semiring, WeightedSemiring, - Weighted(..)) where+ Weighted(..), + getWeight, getInfo ) where import Data.Monoid import Data.Monoid.Multiplicative @@ -56,6 +57,10 @@ -- The best example of this is the ViterbiDerivation semiring. newtype Weighted semi1 semi2 = Weighted (semi1, semi2) deriving (Eq, Show, Monoid, Multiplicative, Semiring)++getWeight (Weighted (semi1, _))= semi1 +getInfo (Weighted (_, semi2))= semi2 + instance (Ord semi1, Eq semi2) => Ord (Weighted semi1 semi2) where compare (Weighted s1) (Weighted s2) = (compare `on` fst) s1 s2
NLP/Semiring/Derivation.hs view
@@ -1,11 +1,11 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-}-module NLP.Semiring.Derivation (Derivation(..), MultiDerivation(..)) where+module NLP.Semiring.Derivation (Derivation(..), MultiDerivation(..), mkDerivation, fromDerivation) where import NLP.Semiring import NLP.Semiring.Helpers import qualified Data.Set as S import Data.Monoid import Data.Maybe (isNothing)-+import Control.Exception -- | The 'Derivation' semiring keeps track of a single path or derivation -- that led to the known output. If there are more than one path it discards @@ -16,7 +16,7 @@ -- Derivation takes a Monoid as an argument that describes how to build up paths or -- more complicated structures. newtype Derivation m = Derivation (Maybe m)- deriving (Eq, Show, Ord) + deriving (Eq, Ord) instance (Monoid m) => Multiplicative (Derivation m) where one = Derivation $ Just mempty@@ -25,15 +25,26 @@ d2' <- d2 return $ mappend d1' d2' -instance (Ord m) => Monoid (Derivation m) where +instance Monoid (Derivation m) where mempty = Derivation Nothing mappend (Derivation s1) (Derivation s2) = Derivation $ case (s1,s2) of (Nothing, s2) -> s2 (s1, Nothing) -> s1- (s1, s2) -> max s1 s2 + (s1, s2) -> s1 -instance (Monoid m, Eq m, Ord m) => Semiring (Derivation m)+instance (Monoid m) => Semiring (Derivation m)++instance (Show m) => Show (Derivation m) where + show (Derivation (Just m)) = show m + show (Derivation Nothing) = "[]" ++mkDerivation :: (Monoid m ) => m -> Derivation m +mkDerivation = Derivation . Just ++fromDerivation :: (Monoid m ) => Derivation m -> m +fromDerivation (Derivation (Just m)) = m +fromDerivation (Derivation Nothing) = throw $ AssertionFailed "no derivation" -- | The 'MultiDerivation' semiring keeps track of a all paths or derivations
NLP/Semiring/Prob.hs view
@@ -5,7 +5,7 @@ -- | The 'Prob' semiring keeps track of the likelihood of the known output -- by keeping track of the probability of all paths. newtype Prob = Prob Double- deriving (Eq, Show, Num, Fractional, Ord) + deriving (Eq, Show, Num, Real, Fractional, Ord) instance Multiplicative Prob where one = 1.0
NLP/Semiring/Viterbi.hs view
@@ -1,3 +1,4 @@+ {-# LANGUAGE GeneralizedNewtypeDeriving #-} module NLP.Semiring.Viterbi where import NLP.Semiring@@ -10,3 +11,8 @@ type Viterbi semi = ViterbiNBest One semi +mkViterbi v = ViterbiNBest [v]++fromViterbi :: (Semiring semi) => Viterbi semi -> semi +fromViterbi (ViterbiNBest []) = mempty +fromViterbi (ViterbiNBest [v]) = v
NLP/Semiring/ViterbiNBestDerivation.hs view
@@ -19,3 +19,9 @@ -- -- > type ViterbiDerivation m = Viterbi (Weighted Prob (Derivation m)) type ViterbiDerivation m = Viterbi (Weighted Prob (Derivation m))++getBestDerivation :: (Monoid m) => ViterbiDerivation m -> m+getBestDerivation = fromDerivation . getInfo . fromViterbi++getBestScore :: (Monoid m) => ViterbiDerivation m -> Prob+getBestScore = getWeight . fromViterbi
semiring.cabal view
@@ -1,5 +1,5 @@ name: semiring-version: 0.1.1+version: 0.2 synopsis: Semirings, ring-like structures used for dynamic programming applications description: This provides a type class for semirings and implementations of the common semirings used in natural language