Commit 6759cb8c authored by ElaFHNW's avatar ElaFHNW
Browse files

further collections

parent 5cd41a2a
package Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
// Exercise 1 on Sets
public class TextStatisticsHashSet {
private Set<String> tokens;
public TextStatisticsHashSet(File file) {
this.tokens = new HashSet<String>();
getTokens(file);
}
private void getTokens(File file) {
Scanner scan = null;
try {
scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.replaceAll("[^A-Za-z0-9-’\s]", "");
line = line.replaceAll("!", "");
// System.out.println(line);
String[] words = line.split("\s+");
for (String w : words) {
tokens.add(w);
// System.out.println(w);
}
}
}
catch (FileNotFoundException e){
System.out.println(e.getMessage());
}
finally {
scan.close();
}
}
public String toString() {
String result = "There are : " + tokens.size();
result += " distinct tokens\n--------------------\n";
ArrayList<String> al = new ArrayList<String>(tokens);
Collections.sort(al);
for (String h: al) {
result += h + " ";
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
File text = null;
text = new File("SzymborskaBirthday.txt");
TextStatisticsHashSet stats = new TextStatisticsHashSet(text);
System.out.println(stats);
}
}
package Collections;
// Though both Hashtable and HashMap are data-structure based upon hashing
// and implementation of Map interface, the main difference between them
// is that HashMap is not thread-safe but Hashtable is thread-safe.
//... Another difference is HashMap allows one null key and null values but
// Hashtable doesn't allow null key or values.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.TreeSet;
import java.util.Vector;
import java.util.Scanner;
import java.util.Set;
// How many times is each word found?
// mapping: word -> count of occurrences
public class TextStatisticsHashTable {
private TreeSet<String> tokens;
private Vector<String> alltokens;
private Hashtable<String,Integer> counts;
public TextStatisticsHashTable(File file) {
this.tokens = new TreeSet<String>();
this.alltokens = new Vector<String>();
this.counts = new Hashtable<String,Integer>();
getTokens(file);
}
private void getTokens(File file) {
Scanner scan = null;
try {
scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.replaceAll("[^A-Za-z0-9_’\s]", "");
line = line.replaceAll("!", "");
String[] words = line.split("\s");
for (String w : words) {
tokens.add(w.toLowerCase());
alltokens.add(w.toLowerCase());
if (counts.containsKey(w.toLowerCase())) {
counts.put(w.toLowerCase(), counts.get(w.toLowerCase())+1);
}
else {
counts.put(w.toLowerCase(),1);
}
}
}
}
catch (FileNotFoundException e){
System.out.println(e.getMessage());
}
finally {
scan.close();
}
}
public String toString() {
String result = "There are : " + alltokens.size();
result += " tokens\ndistinct tokens: " + tokens.size();
result += "\n--------------------\n";
Set<String> keys = new TreeSet<String> ();
keys.addAll(counts.keySet());
for (String h: keys) {
result += h + "\t" + counts.get(h) + "\n";
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
File text = null;
text = new File("SzymborskaBirthday.txt");
TextStatisticsHashTable stats = new TextStatisticsHashTable(text);
System.out.println(stats);
}
}
package Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.TreeSet;
import java.util.Scanner;
// Exercise 1 on Sets
public class TextStatisticsTreeSet {
private TreeSet<String> tokens;
public TextStatisticsTreeSet(File file) {
tokens = new TreeSet<String>();
getTokens(file);
}
private void getTokens(File file) {
Scanner scan = null;
try {
scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.replaceAll("[^A-Za-z0-9_’\s]", "");
line = line.replaceAll("!", "");
String[] words = line.split("\s");
for (String w : words) {
tokens.add(w);
}
}
}
catch (FileNotFoundException e){
System.out.println(e.getMessage());
}
finally {
scan.close();
}
}
public String toString() {
String result = "There are : " + tokens.size();
result += " distinct tokens\n--------------------\n";
// ArrayList<String> al = new ArrayList<String>(tokens);
// Collections.sort(al);
for (String h: tokens) {
result += h + " ";
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
File text = null;
text = new File("SzymborskaBirthday.txt");
TextStatisticsTreeSet stats = new TextStatisticsTreeSet(text);
System.out.println(stats);
}
}
package Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.TreeSet;
import java.util.Vector;
import java.util.Scanner;
// Exercise 1 on Sets
public class TextStatisticsVector {
private TreeSet<String> tokens;
private Vector<String> alltokens;
public TextStatisticsVector(File file) {
tokens = new TreeSet<String>();
alltokens = new Vector<String>();
getTokens(file);
}
private void getTokens(File file) {
Scanner scan = null;
try {
scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.replaceAll("[^A-Za-z0-9_’\s]", "");
line = line.replaceAll("!", "");
String[] words = line.split("\s");
for (String w : words) {
tokens.add(w);
alltokens.add(w);
}
}
}
catch (FileNotFoundException e){
System.out.println(e.getMessage());
}
finally {
scan.close();
}
}
public String toString() {
String result = "There are : " + alltokens.size();
result += " tokens\ndistinct tokens: " + tokens.size();
result += "\n--------------------\n";
for (String h: tokens) {
result += h + " ";
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
File text = null;
text = new File("SzymborskaBirthday.txt");
TextStatisticsVector stats = new TextStatisticsVector(text);
System.out.println(stats);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment