Last active
November 26, 2021 13:52
-
-
Save BedrockDigger/6d63bc02d6a173c0cecd222977a67eec to your computer and use it in GitHub Desktop.
Use getWordCountArray() with WordCount.increment().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public WordCount[] getWordCountArray() { | |
// step 1: remove all leading and trailing ' 's | |
// step 2: remove extra ' 's between words | |
// step 3: remove all special chars | |
// step 4: all to lowercase | |
// step 5: add a leading space for traversion | |
String processedContent = " " + content.trim() | |
.replaceAll(" +", " ") | |
.replaceAll("[.;!?()*,]", "") | |
.toLowerCase(); | |
// Traverse. | |
// ArrayList Version (so that WordCount.increment() is used) | |
ArrayList<WordCount> wordList = new ArrayList<>(); | |
traverseContent: | |
for (int index = 0; index < processedContent.length(); index++) { | |
if (processedContent.charAt(index) == ' ') { | |
StringBuilder currentWordBuilder = new StringBuilder(); | |
// offset upon the leading ' ' | |
for (int offset = 1; offset + index < processedContent.length() && | |
processedContent.charAt(index + offset) != ' '; offset++) { | |
currentWordBuilder.append(processedContent.charAt(index + offset)); | |
} | |
String currentWord = currentWordBuilder.toString(); | |
for (WordCount wordCount : wordList) { | |
if (wordCount.getWord().equals(currentWord)) { | |
wordCount.increment(); | |
continue traverseContent; | |
} | |
} | |
wordList.add(new WordCount(currentWord, 1)); | |
} | |
} | |
WordCount[] wordArray = new WordCount[wordList.size()]; | |
wordArray = wordList.toArray(wordArray); | |
return wordArray; | |
// HashMap version (without using WordCount.increment(), god knows if Artemis reduce my score because of this) | |
// HashMap<String, Integer> wordMap = new HashMap<>(); | |
// for (int index = 0; index < processedContent.length(); index++) { | |
// if (processedContent.charAt(index) == ' ') { | |
// StringBuilder currentWordBuilder = new StringBuilder(); | |
// // offset upon the leading ' ' | |
// for (int offset = 1; offset + index < processedContent.length() && | |
// processedContent.charAt(index + offset) != ' '; offset++) { | |
// currentWordBuilder.append(processedContent.charAt(index + offset)); | |
// } | |
// String currentWord = currentWordBuilder.toString(); | |
// if (wordMap.containsKey(currentWord)) | |
// wordMap.put(currentWord, wordMap.get(currentWord) + 1); | |
// else | |
// wordMap.put(currentWord, 1); | |
// } | |
// } | |
// WordCount[] wordArray = new WordCount[wordMap.size()]; | |
// int i = 0; | |
// for (String word : wordMap.keySet()) { | |
// wordArray[i] = new WordCount(word, wordMap.get(word)); | |
// i++; | |
// } | |
// return wordArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment