Commit b41b9873 authored by Zied SELLAMI's avatar Zied SELLAMI
Browse files

Add interactionDataModel feature

parent d1adf5ea
package org.linagora.priorityInbox.feature;
import java.io.File;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
......@@ -13,6 +11,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import javax.mail.internet.InternetAddress;
import org.apache.commons.io.FileUtils;
import org.linagora.priorityInbox.data.Email;
import org.linagora.priorityInbox.text.Text;
......@@ -24,12 +24,14 @@ public class DynamicFeature {
private static HashMap<String, HashMap<String, TextDataModel>> WORDS_DATA = new HashMap<String, HashMap<String, TextDataModel>>();
private static HashMap<String, HashMap<String, TextDataModel>> SENDER_DATA = new HashMap<String, HashMap<String, TextDataModel>>();
private static HashMap<String, HashMap<String, InteractionDataModel>> INTERACTION_DATA = new HashMap<String, HashMap<String, InteractionDataModel>>();
private static synchronized HashMap<String, TextDataModel> getWordsData(String userId) {
HashMap<String, TextDataModel> wordsData = WORDS_DATA.get(userId);
if (wordsData == null) {
Path wordsDataPath = Paths.get(modelDirectory, userId, "textModel");
wordsData = readModelFromFile(wordsDataPath.toFile());
wordsData = readTextDataModelFromFile(wordsDataPath.toFile());
if (wordsData != null) {
WORDS_DATA.put(userId, wordsData);
}
......@@ -41,16 +43,28 @@ public class DynamicFeature {
HashMap<String, TextDataModel> wordsData = SENDER_DATA.get(userId);
if (wordsData == null) {
Path wordsDataPath = Paths.get(modelDirectory, userId, "senderModel");
wordsData = readModelFromFile(wordsDataPath.toFile());
wordsData = readTextDataModelFromFile(wordsDataPath.toFile());
if (wordsData != null) {
SENDER_DATA.put(userId, wordsData);
}
}
return wordsData;
}
private static synchronized HashMap<String, InteractionDataModel> getInteractionsData(String userId) {
HashMap<String, InteractionDataModel> interactionData = INTERACTION_DATA.get(userId);
if (interactionData == null) {
Path wordsDataPath = Paths.get(modelDirectory, userId, "interactionModel");
interactionData = readInteractionDataModelFromFile(wordsDataPath.toFile());
if (interactionData != null) {
INTERACTION_DATA.put(userId, interactionData);
}
}
return interactionData;
}
public static synchronized void saveModels() {
long start = System.currentTimeMillis();
// Saving words data
for (Entry<String, HashMap<String, TextDataModel>> entry : WORDS_DATA.entrySet()) {
String userId = entry.getKey();
......@@ -86,7 +100,26 @@ public class DynamicFeature {
}
}
long end = System.currentTimeMillis();
//Saving interaction data
for (Entry<String, HashMap<String, InteractionDataModel>> entry : INTERACTION_DATA.entrySet()) {
String userId = entry.getKey();
StringBuffer modelBuffer = new StringBuffer();
for (InteractionDataModel interactionDataModel : entry.getValue().values()) {
modelBuffer.append(interactionDataModel.toStringData());
}
try {
FileUtils.write(Paths.get(modelDirectory, userId, "interactionModel").toFile(),
modelBuffer.toString(), Charset.forName("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
String readableSize = "";
long size = 0;
try {
......@@ -117,14 +150,86 @@ public class DynamicFeature {
return userId;
}
private static void updateInteractionScore(Email email) {
try {
HashMap<String, InteractionDataModel> models = new HashMap<String, InteractionDataModel>();
String userId = toUserId(email.getUser(), email.getAlternativeAddress());
models = getInteractionsData(userId);
if (models == null) {
models = new HashMap<String, InteractionDataModel>();
INTERACTION_DATA.put(userId, models);
}
String sender = email.getFrom().getAddress();
if(sender != null) {
if(sender == email.getUser() || email.getAlternativeAddress().contains(sender)) {
//update interaction as Sent Email
for(InternetAddress to: email.getTo()) {
String address = to.getAddress();
if(address != null) {
InteractionDataModel interactionDataModel = models.get(address);
if(interactionDataModel == null) {
models.put(address, new InteractionDataModel(address, 0, 1));
}else {
interactionDataModel.setSentTo(interactionDataModel.getSentTo() + 1);
}
}
}
for(InternetAddress cc: email.getCc()) {
String address = cc.getAddress();
if(address != null) {
InteractionDataModel interactionDataModel = models.get(address);
if(interactionDataModel == null) {
models.put(address, new InteractionDataModel(address, 0, 1));
}else {
interactionDataModel.setSentTo(interactionDataModel.getSentTo() + 1);
}
}
}
for(InternetAddress bcc: email.getBcc()) {
String address = bcc.getAddress();
if(address != null) {
InteractionDataModel interactionDataModel = models.get(address);
if(interactionDataModel == null) {
models.put(address, new InteractionDataModel(address, 0, 1));
}else {
interactionDataModel.setSentTo(interactionDataModel.getSentTo() + 1);
}
}
}
}else {
//update interaction as Received Email
InteractionDataModel interactionDataModel = models.get(sender);
if(interactionDataModel == null) {
models.put(sender, new InteractionDataModel(sender, 1, 0));
}else {
interactionDataModel.setReceivedFrom(interactionDataModel.getReceivedFrom() + 1);
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static double getSenderWeight(Email email) {
double senderWeight = 0d;
try {
updateInteractionScore(email);
HashMap<String, InteractionDataModel> interactionModels = new HashMap<String, InteractionDataModel>();
HashMap<String, TextDataModel> models = new HashMap<String, TextDataModel>();
TextDataModel senderTextDataModel = null;
String userId = toUserId(email.getUser(), email.getAlternativeAddress());
interactionModels = getInteractionsData(userId);
models = getSendersData(userId);// readModelFromFile(dataModelFile);
int maxDocumentSize = 0;
......@@ -174,7 +279,12 @@ public class DynamicFeature {
double idf = Math.log10((double) senderTextDataModel.getTotalDocumentSize()
/ (double) senderTextDataModel.getDocumentContainingWord());
double tfidf = tf * idf;
senderWeight = tfidf;
int interactionScore = 0;
if(sender != null) {
InteractionDataModel interactionDataModel = interactionModels.get(sender);
interactionScore = interactionDataModel.getInteractionScore();
}
senderWeight = tfidf * interactionScore;
// System.out.println("Sender weight: " + tfidf);
} catch (Exception e) {
......@@ -263,7 +373,7 @@ public class DynamicFeature {
return contentWeight;
}
public static HashMap<String, TextDataModel> readModelFromFile(File file) {
public static HashMap<String, TextDataModel> readTextDataModelFromFile(File file) {
HashMap<String, TextDataModel> models = new HashMap<String, TextDataModel>();
try {
List<String> lines = FileUtils.readLines(file, Charset.forName("UTF-8"));
......@@ -286,6 +396,29 @@ public class DynamicFeature {
return models;
}
public static HashMap<String, InteractionDataModel> readInteractionDataModelFromFile(File file) {
HashMap<String, InteractionDataModel> models = new HashMap<String, InteractionDataModel>();
try {
List<String> lines = FileUtils.readLines(file, Charset.forName("UTF-8"));
for (String line : lines) {
String[] elements = line.split("\t");
String word = elements[0];
int receivedFrom = Integer.parseInt(elements[1]);
int sentTo = Integer.parseInt(elements[2]);
InteractionDataModel interactionDataModel = new InteractionDataModel(word, receivedFrom, sentTo);
models.put(word, interactionDataModel);
}
} catch (Exception e) {
//e.printStackTrace();
System.err.println("READING MODEL FROM DISK. NO MODEL SAVED YET.");
}
return models;
}
public static String detectLanguage(Email email) {
String content = "";
......
package org.linagora.priorityInbox.feature;
public class InteractionDataModel {
private String address = null;
private int receivedFrom = 0;
private int sentTo = 0;
public InteractionDataModel(String address, int receivedFrom, int sentTo) {
this.address = address;
this.receivedFrom = receivedFrom;
this.sentTo = sentTo;
}
public InteractionDataModel(String address) {
this.address = address;
}
public InteractionDataModel() {
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getReceivedFrom() {
return receivedFrom;
}
public void setReceivedFrom(int receivedFrom) {
this.receivedFrom = receivedFrom;
}
public int getSentTo() {
return sentTo;
}
public void setSentTo(int sentTo) {
this.sentTo = sentTo;
}
public int getInteractionScore() {
return Math.min(sentTo, receivedFrom);
}
public String toStringData() {
return address + "\t" + receivedFrom + "\t" + sentTo + "\n";
}
}
Markdown is supported
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