Advertisement
Guest User

Char Distribution

a guest
Dec 11th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1.  
  2. public static double[] computeDistribution(final String corpus) {
  3. String shortenedCorpus = corpus.replaceAll("[^a-zA-Z]", "");
  4. char[] corpusArray = shortenedCorpus.toCharArray();
  5. int corpusLength = shortenedCorpus.length();
  6. double[] resultSet = new double[ALPHA_BET];
  7. char check = ' ';
  8. String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9. if (corpusLength < 1) {
  10. return resultSet;
  11. }
  12. for (int i = 0; i < corpusLength; i++) {
  13. int charIndex = alphabet.indexOf(corpusArray[i]);
  14. resultSet[charIndex] = resultSet[charIndex] + 1;
  15. }
  16. for (int i = 0; i < resultSet.length; i++) {
  17. resultSet[i] = resultSet[i] / corpusLength;
  18. }
  19. return resultSet;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement