Skip to content

Commit

Permalink
Implemented JavaTrieClassify.
Browse files Browse the repository at this point in the history
  • Loading branch information
antononcube committed Apr 7, 2018
1 parent 1bf1dcd commit b12d690
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions JavaTriesWithFrequencies.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ Mathematica is (C) Copyright 1988-2017 Wolfram Research, Inc.

JavaTrieComparisonGrid::usage = "Makes a grid trie plots for a specified list of Java trie expressions."

JavaTrieClassify::usage = "JavaTrieClassify[jTr_,record_] classifies a record using a Java trie. \
The signature JavaTrieClassify[jTr_,record_,prop_] can take properties as the ones given to ClassifierFunction. \
JavaTrieClassify[jTr_,record_] is the same as JavaTrieClassify[tr_,record_,\"Decision\"]."


Begin["`Private`"]

Needs["JLink`"]
Expand Down Expand Up @@ -493,6 +498,34 @@ Mathematica is (C) Copyright 1988-2017 Wolfram Research, Inc.
];


Clear[JavaTrieClassify]

Options[JavaTrieClassify] := {"Default" -> None};

JavaTrieClassify[tr_, record_, opts : OptionsPattern[]] :=
JavaTrieClassify[tr, record, "Decision", opts];

JavaTrieClassify[tr_, record_, "Decision", opts : OptionsPattern[]] :=
First@Keys@JavaTrieClassify[tr, record, "Probabilities", opts];

JavaTrieClassify[tr_, record_, "Probability" -> class_] :=
Lookup[JavaTrieClassify[tr, record, "Probabilities"], class, 0];

JavaTrieClassify[tr_, record_, "TopProbabilities", opts : OptionsPattern[]] :=
Select[JavaTrieClassify[tr, record, "Probabilities", opts], # > 0 &];

JavaTrieClassify[tr_, record_, "TopProbabilities" -> n_Integer, opts : OptionsPattern[]] :=
Take[JavaTrieClassify[tr, record, "Probabilities", opts], UpTo[n]];

JavaTrieClassify[tr_, record_, "Probabilities", opts : OptionsPattern[]] :=
Block[{res, dval = OptionValue[TrieClassify, "Default"]},
res = JavaTrieLeafProbabilities[JavaTrieRetrieve[tr, record]];
If[Length[res] == 0, <|dval -> 0|>,
res = AssociationThread[res[[All, 2, 2]] -> res[[All, 1, 2]]];
res = ReverseSort[Association[Rule @@@ res]]
]
];

End[] (* `Private` *)

EndPackage[]

0 comments on commit b12d690

Please sign in to comment.