-
Notifications
You must be signed in to change notification settings - Fork 13
SONARPY-2080 Move ShannonEntropy to analyzer commons #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
59dceab
to
799f838
Compare
.map(frequency -> -frequency * Math.log(frequency)) | ||
.sum() / LOG_2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the division from the map
to outside of it, it does not seem to affect numerical stability in the few tests
} | ||
int length = str.length(); | ||
return str.chars() | ||
.collect(HashMap<Integer, Integer>::new, (map, ch) -> map.merge(ch, 1, Integer::sum), HashMap::putAll) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be simplifies:
return str.chars()
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.values()
.stream()
.map(Long::doubleValue)
.mapToDouble(count -> count / length)
.map(frequency -> -frequency * Math.log(frequency))
.sum() / LOG_2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was reusing the previous SonarJava implementation, but if this one also works I can change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just one super minor suggestion to simplify the stream API used
|
ACOMMONS-17