8000 SONARPY-2080 Move ShannonEntropy to analyzer commons by ghislainpiot · Pull Request #340 · SonarSource/sonar-analyzer-commons · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 4 commits into from
Aug 22, 2024

Conversation

ghislainpiot
Copy link
Contributor
@ghislainpiot ghislainpiot commented Aug 22, 2024

Comment on lines +41 to +42
.map(frequency -> -frequency * Math.log(frequency))
.sum() / LOG_2;
Copy link
Contributor Author

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)

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;

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link

@ghislainpiot ghislainpiot merged commit cdc7195 into master Aug 22, 2024
4 checks passed
@ghislainpiot ghislainpiot deleted the SONARPY-2080 branch August 22, 2024 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0