From b12d6904821bec2ebe9ee82294b48e951e7f6c85 Mon Sep 17 00:00:00 2001 From: antononcube Date: Sat, 7 Apr 2018 16:26:41 -0400 Subject: [PATCH] Implemented JavaTrieClassify. --- JavaTriesWithFrequencies.m | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/JavaTriesWithFrequencies.m b/JavaTriesWithFrequencies.m index e39901c2..e71064c7 100644 --- a/JavaTriesWithFrequencies.m +++ b/JavaTriesWithFrequencies.m @@ -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`"] @@ -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[] \ No newline at end of file