Created
July 26, 2012 08:14
-
-
Save longouyang/3180917 to your computer and use it in GitHub Desktop.
PCFG unfold in Mathematica
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TerminalQ[x_] := MemberQ[{a, b, c, d}, x]; | |
Rules = {S -> {{{X, Y}, 1}, {{X, X, Y}, 5}}, | |
X -> {{{a}, 1/2}, {{b}, 1/2}}, Y -> {{{c}, 1/3}, {{d}, 2/3}}}; | |
PCFGUnfold[sym_, Rules_, TerminalQ_] := | |
If[TerminalQ[sym], | |
{{{sym}, 1}}, | |
Block[{lookup, rules, probabilities}, | |
(*for some reason,I can't specify the last two in the {} above*) | |
lookup = sym /. Rules; | |
rules = #[[1]] & /@ lookup; | |
probabilities = | |
Normalize[#[[2]] & /@ lookup, | |
Total];(*divide pcfg weights by their Total*)Join @@ MapThread[ | |
(*a production is a list whose first element is a sequence of \ | |
symbols and whose second element is a probability*) | |
Function[{production, prob}, | |
Block[(*subprods is a list of productions, | |
one for each symbol in prod*){subProds = | |
PCFGUnfold[#, Rules, TerminalQ] & /@ production}, | |
Map[ | |
Function[prod, | |
List[ | |
List @@ (First /@ prod), | |
prob*Times @@ (#[[2]] & /@ prod)]], | |
Tuples[subProds]]]], | |
{rules, probabilities}] | |
] | |
]; | |
PCFGUnfold[S, Rules, TerminalQ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment