Skip to content

Commit

Permalink
refactor:Additional check for non-numeric $TrieValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
antononcube committed May 14, 2023
1 parent 1f475cb commit 4adf8df
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion TriesWithFrequencies.m
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,25 @@ An Association Trie with Frequencies (ATF) has the form:
Options[TrieNodeProbabilities] = {"ProbabilityModifier" -> N};
Options[TrieNodeProbabilitiesRec] = Options[TrieNodeProbabilities];

TrieNodeProbabilities::nnr = "Non-numeric $TrieValue encountered.";

TrieNodeProbabilities[tr_?TrieQ, opts : OptionsPattern[]] :=
Block[{},
<|First[Keys[tr]] -> Join[TrieNodeProbabilitiesRec[First@Values[tr], opts], <|$TrieValue -> 1|>]|>
];

TrieNodeProbabilitiesRec[trb_?TrieBodyQ, opts : OptionsPattern[]] :=
Block[{sum, res, pm = OptionValue[TrieNodeProbabilitiesRec, "ProbabilityModifier"]},
Module[{sum, res, pm = OptionValue[TrieNodeProbabilitiesRec, "ProbabilityModifier"]},
Which[
Length[Keys[trb]] == 1, trb,

True,
If[trb[$TrieValue] == 0,
sum = TrieValueTotal[trb],
(*ELSE*)
sum = trb[$TrieValue],
(*OTHER*)
Message[TrieNodeProbabilities::nnr];
sum = trb[$TrieValue]
];
res = Map[TrieNodeProbabilitiesRec[#] &, KeyDrop[trb, $TrieValue]];
Expand Down

0 comments on commit 4adf8df

Please sign in to comment.