Hallo, womit fang’ ich an? Ich hab hier 'ne Nuss:
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication;
import java.io.*;
import java.net.*;
import java.util.*;
import org.*.*;
import org.*.*.*;
/**
* @author CB
*/
public class WS {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
ArrayList<Post> posts = loadPosts();
printPosts(posts);
HashMap<String, ArrayList<Post>> usersPosts = getMap1(posts);
HashMap<String, HashMap<String, Integer>> usersWordsCounts = getMap2(usersPosts);
HashMap<String, ArrayList<Word>> usersWords = getMap3(usersWordsCounts);
printUsers(usersWords);
}
private static HashMap<String, ArrayList<Post>> getMap1(ArrayList<Post> posts) {
HashMap<String, ArrayList<Post>> usersPosts = new HashMap<String, ArrayList<Post>>();
for (Post p : posts) {
if (!usersPosts.containsKey(p.name)) {
usersPosts.put(p.name, new ArrayList<Post>());
}
usersPosts.get(p.name).add(p);
}
return usersPosts;
}
private static HashMap<String, HashMap<String, Integer>> getMap2(ArrayList<Post> posts) {
HashMap<String , HashMap<String, Word> > userWordSyno = new HashMap<String, HashMap<String, Word>>();
for (Post p : posts) {
if (!userWordSyno.containsKey(p.name)) {
userWordSyno.put(p.name, new HashMap<String, Word>());
}
String[] sa = p.text.split("[^\\wÄäÖöÜüß]+");
for (String s : sa) {
if (s.isEmpty()) {
continue;
}
s = s.toLowerCase();
if (wordsCounts.containsKey(s)) {
wordsCounts.put(s, wordsCounts.get(s) + 1);
} else {
wordsCounts.put(s, 1);
}
}
}
HashMap<String, HashMap<String, Integer>> usersWordsCounts = new HashMap<String, HashMap<String, Integer>>();
for (Post p : posts) {
if (!usersWordsCounts.containsKey(p.name)) {
usersWordsCounts.put(p.name, new HashMap<String, Integer>());
}
HashMap<String, Integer> wordsCounts = usersWordsCounts.get(p.name);
String[] sa = p.text.split("[^\\wÄäÖöÜüß]+");
for (String s : sa) {
if (s.isEmpty()) {
continue;
}
s = s.toLowerCase();
if (wordsCounts.containsKey(s)) {
wordsCounts.put(s, wordsCounts.get(s) + 1);
} else {
wordsCounts.put(s, 1);
}
}
}
return usersWordsCounts;
/* HashMap<String, HashMap<String, Integer>> usersWordsCounts = new HashMap<String, HashMap<String, Integer>>();
for (String user : usersPosts.keySet()) {
HashMap<String, Integer> wordsCounts = new HashMap<String, Integer>();
for (Post p : usersPosts.get(user)) {
String[] sa = p.text.split("[^\\wÄäÖöÜüß]+");
for (String s : sa) {
if (s.isEmpty()) {
continue;
}
s = s.toLowerCase();
if (wordsCounts.containsKey(s)) {
wordsCounts.put(s, wordsCounts.get(s) + 1);
} else {
wordsCounts.put(s, 1);
}
}
}
usersWordsCounts.put(user, wordsCounts);
}
return usersWordsCounts; */
}
private static HashMap<String, ArrayList<Word>> getMap3(HashMap<String, HashMap<String, Integer>> usersWordsCounts) {
HashMap<String, ArrayList<Word>> usersWords = new HashMap<String, ArrayList<Word>>();
for (Map.Entry<String, HashMap<String, Integer>> e : usersWordsCounts.entrySet()) { // Users
HashMap<Word, HashSet<Word>> tree = new TreeMap<Word, ArrayList<Word>> ();
for (Map.Entry<String, Integer> e2 : e.getValue().entrySet()) { // Wörter
Word w = new Word();
w.setUser(e.getKey());
w.setWord(e2.getKey());
w.setCount(e2.getValue());
if (tree.containsKey(w)) {
tree.get(w).add(w);
}
if (tree.contains(w)) {
tree.floor(w).setCount(tree.floor(w).getCount() + e2.getValue());
} else {
tree.add(w);
}
}
ArrayList<Word> words = new ArrayList<Word>(tree);
words.sort(new Comparator<Word>() {
@Override
public int compare(Word o1, Word o2) {
return Integer.compare(o2.getCount(), o1.getCount());
}
});
usersWords.put(e.getKey(), words);
}
return usersWords;
}
private static ArrayList<Post> loadPosts() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("woen_uns.txt"));
ArrayList<Post> posts = (ArrayList<Post>) ois.readObject();
ois.close();
return posts;
}
private static void printPosts(ArrayList<Post> posts) {
for (Post p : posts) {
System.out.println(p);
System.out.println("");
}
}
private static void printUser(ArrayList<Word> words) {
for (Word w : words) {
System.out.printf("%-20s %-20s %04d%n", w.user, w.word, w.count);
}
System.out.println("");
}
private static void printUsers(HashMap<String, ArrayList<Word>> usersWords) {
for (Map.Entry<String, ArrayList<Word>> e : usersWords.entrySet()) {
printUser(e.getValue());
}
}
private static String readUrl(String string) throws IOException {
StringBuilder sb = new StringBuilder();
// weiter ist geheim
return sb.toString();
}
private static void savePosts(ArrayList<Post> posts) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("woen_uns.txt"));
oos.writeObject(posts);
oos.close();
posts = sortPosts(posts);
oos = new ObjectOutputStream(new FileOutputStream("woen_sor.txt"));
oos.writeObject(posts);
oos.close();
}
private static ArrayList<Post> scanBis(int bis) throws InterruptedException, IOException {
ArrayList<Post> alp = new ArrayList<Post>();
// weiter ist geheim
return alp;
}
private static ArrayList<Post> sortPosts(ArrayList<Post> posts) {
ArrayList<Post> posts2 = new ArrayList<Post>(posts);
Collections.sort(posts2);
return posts2;
}
}
class Post implements Comparable<Post>, Serializable {
String uhrz;
String name;
String text;
@Override
public int compareTo(Post o) {
return name.compareTo(o.name);
}
@Override
public String toString() {
return "Post{" + "uhrz=" + uhrz + ", name=" + name + ", text=" + text + '}';
}
}
/* class Word implements Comparable<Word>, Serializable {
int count;
HashSet<String> syno;
public Word(String s) {
count = 1;
syno = new HashSet<String>();
syno.add(s);
}
public void add(String s) {
syno.add(s);
}
public void inc() {
count++;
}
@Override
public int compareTo(Word o) {
return Integer.compare(o.count, count);
}
} */
class Word implements Comparable<Word>, Serializable {
String user;
String word;
int count;
@Override
public int compareTo(Word o) {
return Integer.compare(o.count, count);
}
@Override
public String toString() {
return "Word{" + "user=" + user + ", word=" + word + ", count=" + count + '}';
}
}```
Es läuft nicht so ganz, wie ich mir das gedacht hab.
Es gibt auf Top-level zwei Schritte,
eine Liste mit allen Posts erstellen,
für jeden User wird jedes Wort gezählt und das wird in eine Map gelegt,
(es wird sortiert).
Jetzt kommen wir näher zum Prob (welche Datenstruktur), in
`HashMap<String, HashMap<String, Integer>>`
wird User, Wort/Wörter und Anzahl abgelegt.
Allerdings hab ich jetzt das Prob, dass HashM. quasi nur genau übereinstimmende Zeichenketten/-Sequenzen nimmt, kein etwa, ungefähr, Gruppierungen usw. Außerdem quasi nur das zuletzt eingefügte 'Vorkommen'.
Ich möchte aber... quasi für ein Begriff einen Oberbegriff und mehrere Unterbegriffe, sowie Summierung aller Begriffe 'Vorkommen' in einer Kategorie. Das Prob ist mit der Datenstruktur.
Ist das schlüssig?
Edit: `private` (noch) nicht vorgeschrieben, Bezeichner könnten noch anders.