Refactoring

pull/190/head
M66B 4 years ago
parent 0d8535815a
commit 1b69db2b93

@ -278,43 +278,13 @@ public class MessageClassifier {
static synchronized void save(Context context) throws JSONException, IOException {
if (!dirty)
return;
if (!isEnabled(context))
return;
JSONArray jmessages = new JSONArray();
for (Long account : classMessages.keySet())
for (String clazz : classMessages.get(account).keySet()) {
JSONObject jmessage = new JSONObject();
jmessage.put("account", account);
jmessage.put("class", clazz);
jmessage.put("count", classMessages.get(account).get(clazz));
jmessages.put(jmessage);
}
JSONArray jwords = new JSONArray();
for (Long account : classMessages.keySet())
for (String word : wordClassFrequency.get(account).keySet()) {
Map<String, Integer> classFrequency = wordClassFrequency.get(account).get(word);
for (String clazz : classFrequency.keySet()) {
JSONObject jword = new JSONObject();
jword.put("account", account);
jword.put("word", word);
jword.put("class", clazz);
jword.put("frequency", classFrequency.get(clazz));
jwords.put(jword);
}
}
JSONObject jroot = new JSONObject();
jroot.put("messages", jmessages);
jroot.put("words", jwords);
File file = getFile(context);
Helper.writeText(file, jroot.toString(2));
Helper.writeText(file, toJson().toString(2));
dirty = false;
Log.i("Classifier saved");
Log.i("Classifier data saved");
}
private static synchronized void load(Context context) throws IOException, JSONException {
@ -330,35 +300,11 @@ public class MessageClassifier {
File file = getFile(context);
if (file.exists()) {
String json = Helper.readText(file);
JSONObject jroot = new JSONObject(json);
JSONArray jmessages = jroot.getJSONArray("messages");
for (int m = 0; m < jmessages.length(); m++) {
JSONObject jmessage = (JSONObject) jmessages.get(m);
long account = jmessage.getLong("account");
if (!classMessages.containsKey(account))
classMessages.put(account, new HashMap<>());
classMessages.get(account).put(jmessage.getString("class"), jmessage.getInt("count"));
}
JSONArray jwords = jroot.getJSONArray("words");
for (int w = 0; w < jwords.length(); w++) {
JSONObject jword = (JSONObject) jwords.get(w);
long account = jword.getLong("account");
if (!wordClassFrequency.containsKey(account))
wordClassFrequency.put(account, new HashMap<>());
String word = jword.getString("word");
Map<String, Integer> classFrequency = wordClassFrequency.get(account).get(word);
if (classFrequency == null) {
classFrequency = new HashMap<>();
wordClassFrequency.get(account).put(word, classFrequency);
}
classFrequency.put(jword.getString("class"), jword.getInt("frequency"));
}
fromJson(new JSONObject(json));
}
loaded = true;
Log.i("Classifier loaded");
Log.i("Classifier data loaded");
}
static synchronized void clear(Context context) {
@ -383,6 +329,64 @@ public class MessageClassifier {
return new File(context.getFilesDir(), "classifier.json");
}
static JSONObject toJson() throws JSONException {
JSONArray jmessages = new JSONArray();
for (Long account : classMessages.keySet())
for (String clazz : classMessages.get(account).keySet()) {
JSONObject jmessage = new JSONObject();
jmessage.put("account", account);
jmessage.put("class", clazz);
jmessage.put("count", classMessages.get(account).get(clazz));
jmessages.put(jmessage);
}
JSONArray jwords = new JSONArray();
for (Long account : classMessages.keySet())
for (String word : wordClassFrequency.get(account).keySet()) {
Map<String, Integer> classFrequency = wordClassFrequency.get(account).get(word);
for (String clazz : classFrequency.keySet()) {
JSONObject jword = new JSONObject();
jword.put("account", account);
jword.put("word", word);
jword.put("class", clazz);
jword.put("frequency", classFrequency.get(clazz));
jwords.put(jword);
}
}
JSONObject jroot = new JSONObject();
jroot.put("messages", jmessages);
jroot.put("words", jwords);
return jroot;
}
static void fromJson(JSONObject jroot) throws JSONException {
JSONArray jmessages = jroot.getJSONArray("messages");
for (int m = 0; m < jmessages.length(); m++) {
JSONObject jmessage = (JSONObject) jmessages.get(m);
long account = jmessage.getLong("account");
if (!classMessages.containsKey(account))
classMessages.put(account, new HashMap<>());
classMessages.get(account).put(jmessage.getString("class"), jmessage.getInt("count"));
}
JSONArray jwords = jroot.getJSONArray("words");
for (int w = 0; w < jwords.length(); w++) {
JSONObject jword = (JSONObject) jwords.get(w);
long account = jword.getLong("account");
if (!wordClassFrequency.containsKey(account))
wordClassFrequency.put(account, new HashMap<>());
String word = jword.getString("word");
Map<String, Integer> classFrequency = wordClassFrequency.get(account).get(word);
if (classFrequency == null) {
classFrequency = new HashMap<>();
wordClassFrequency.get(account).put(word, classFrequency);
}
classFrequency.put(jword.getString("class"), jword.getInt("frequency"));
}
}
private static class Stat {
int matchedWords = 0;
int totalFrequency = 0;

Loading…
Cancel
Save