Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>collections</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/zipcoder/MainApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.zipcoder;

import java.io.IOException;

public class MainApplication {

public static void main(String[] args) {
WC wordCountsimulation = new WC("/Users/petermccormick/Dev/Peter/Week6Labs/CR-MesoLabs-Collections-" +
"EncapsulativeCharacters/src/main/resources/14015.txt");

wordCountsimulation.wordCollector();
wordCountsimulation.sortWordCollector();
wordCountsimulation.printSortedMap();
try {
wordCountsimulation.printToText();
} catch (IOException e) {
e.printStackTrace();
}
}
}
54 changes: 54 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
package io.zipcoder;

import java.util.Stack;

public class ParenChecker {

ParenChecker() {

}

public boolean parenPair(String string) {

Stack stack = new Stack();

for (int i = 0; i <string.length(); i++) {
char character = string.charAt(i);
if (stack.empty() && character == ')') {
return false;
}
if (character == '(') {
stack.push(character);
}
if (string.charAt(i) == ')' ) {
stack.pop();
}
}
return stack.isEmpty();

}

public boolean forEachOpeningAClosing(String string) {

Stack stack = new Stack();

for (int i = 0; i <string.length(); i++) {
char character = string.charAt(i);

if (stack.empty() && character == ')' || stack.empty() && character == '}' || stack.empty() && character
== ']' || stack.empty() && character == '>' ) {
return false;
}
if (character == '(' || character == '{' || character == '[' || character == '<' || character == '\'' ||
character == '"') {
stack.push(character);
}
if (character == ')' || character == '}' || character == ']' || character == '>' ||
character == '\'' || character == '"') {
stack.pop();
}
}
return stack.isEmpty();


}



}
55 changes: 51 additions & 4 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package io.zipcoder;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
import java.util.*;

import static java.util.stream.Collectors.toMap;

public class WC {

private Iterator<String> si;
private Map<String, Integer> wordCounter = new LinkedHashMap<String, Integer>();


public WC() {

}

public WC(String fileName) {
try {
Expand All @@ -20,4 +27,44 @@ public WC(String fileName) {
public WC(Iterator<String> si) {
this.si = si;
}

public Map<String, Integer> wordCollector() {
while (si.hasNext()) {
String nextWord = si.next().toLowerCase().replaceAll("[^a-z]", "");

if (wordCounter.containsKey(nextWord)) {
wordCounter.put(nextWord, wordCounter.get(nextWord) + 1);
} else wordCounter.put(nextWord, 1);
}
return wordCounter;
}

public Map<String, Integer> sortWordCollector() {
Map<String, Integer> sortByDes = wordCollector().entrySet()
.stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()))
.collect(toMap(Map.Entry::getKey,
Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
return sortByDes;

}

public String printSortedMap() {
StringBuilder printOut = new StringBuilder();
for (Map.Entry<String, Integer> e: sortWordCollector().entrySet()) {
printOut.append(String.format("%18s : Occurred %5d times!\n", e.getKey(), e.getValue()));
//////printOut.append(e.getKey() + " : Occurred " + e.getValue() + " times!\n" );\\\\\\\
} return printOut.toString();
}

public void printToText() throws IOException {
final File file = new File(
"/Users/petermccormick/Dev/Peter/Week6Labs/CR-MesoLabs-Collections-EncapsulativeCharacters" +
"/src/main/resources/WordCountResults.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(printSortedMap());
bufferedWriter.close();
}

}
Loading