Skip to content

Commit

Permalink
Implemented JavaTrieRandomChoice and options for normalization for Ja…
Browse files Browse the repository at this point in the history
…vaTrieRetrieve.
  • Loading branch information
antononcube committed Feb 14, 2017
1 parent 3a278b0 commit 50652ec
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions JavaTriesWithFrequencies.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ Mathematica is (C) Copyright 1988-2017 Wolfram Research, Inc.
JavaTrieParetoFractionRemove::usage = "Remove nodes that have values below (or above) thresholds derived\
from a specified Pareto fraction."

JavaTrieRandomChoice::usage = "Random choice of a root-to-leaf path."

JavaTrieToJSON::usage = "Converts a Java trie to a corresponding JSON expression."

JSONTrieToRules::usage = "Converts a JSON trie into rules for GraphPlot."
Expand Down Expand Up @@ -253,10 +255,30 @@ Mathematica is (C) Copyright 1988-2017 Wolfram Research, Inc.
JLink`LoadJavaClass["TrieFunctions"];
];

Clear[JavaTrieLeafProbabilities]
JavaTrieLeafProbabilities[jTr_?JavaObjectQ] :=
Clear[JavaTrieLeafProbabilities, JavaTrieLeafProbabilitiesSimple]

Options[JavaTrieLeafProbabilities] = { "Normalized"->False, "ChopValue"->Automatic };

JavaTrieLeafProbabilities[jTr_?JavaObjectQ, opts:OptionsPattern[]] :=
Block[{rootVal, chopVal, res},
res =
If[ TrueQ[ OptionValue["Normalized"] ],
rootVal = "value" /. JavaTrieToJSON[jTr, 0];
JavaTrieLeafProbabilitiesSimple[jTr] /. ("value" -> x_?NumberQ) :> ("value" -> x/rootVal),
(*ELSE*)
JavaTrieLeafProbabilitiesSimple[jTr]
];
chopVal = OptionValue["ChopValue"];
If[ TrueQ[NumericQ[chopVal]],
Select[res, ("value" /. #) >= chopVal &],
res
]
];

JavaTrieLeafProbabilitiesSimple[jTr_?JavaObjectQ]:=
ImportString[ StringReplace[ TrieFunctions`leafProbabilitiesJSON[jTr], "\"\"\"" -> "\"\\\"\""], "JSON"];


Clear[JavaTrieNodeCounts]
JavaTrieNodeCounts[jTr_?JavaObjectQ] :=
AssociationThread[{"total","internal","leaves"}->JLink`JavaObjectToExpression[TrieFunctions`nodeCounts[jTr]]];
Expand Down Expand Up @@ -362,6 +384,9 @@ Mathematica is (C) Copyright 1988-2017 Wolfram Research, Inc.
JavaTrieParetoFractionRemove[jTr_?JavaObjectQ, paretoFraction_?NumericQ, postfix_String] :=
TrieFunctions`removeByParetoFraction[jTr, paretoFraction, True, postfix];

Clear[JavaTrieRandomChoice]
JavaTrieRandomChoice[jTr_?JavaObjectQ, weightedQ:(True|False):True] :=
JavaObjectToExpression[ TrieFunctions`randomChoice[jTr, weightedQ] ];

Clear[JSONTrieToRules]
JSONTrieToRules[tree_] := Block[{ORDER = 0}, JSONTrieToRules[tree, 0, 0]];
Expand Down

0 comments on commit 50652ec

Please sign in to comment.