use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.
the class Archive method prepareAllDocs.
/**
* prepares all docs for indexing, incl. applying filters, removing dups and
* sorting
*
* @throws Exception
*/
private void prepareAllDocs(Collection<Document> docs, IndexOptions io) throws Exception {
allDocs = new ArrayList<>();
allDocs.addAll(docs);
allDocs = EmailUtils.removeDupsAndSort(allDocs);
log.info(allDocs.size() + " documents after removing duplicates");
if (addressBook == null && !io.noRecipients) {
log.warn("no address book previously set up!");
// set up without the benefit of ownaddrs
setupAddressBook(allDocs);
}
if (io.filter != null && addressBook != null) {
// may return null
Contact ownCI = addressBook.getContactForSelf();
// if we don't
// have own info
io.filter.setOwnContactInfo(ownCI);
}
// if no filter, accept doc (default)
List<Document> newAllDocs = new ArrayList<>();
for (Document d : allDocs) if (io.filter == null || (io.filter != null && io.filter.matches(d)))
newAllDocs.add(d);
EmailUtils.cleanDates(newAllDocs);
log.info(newAllDocs.size() + " documents after filtering");
allDocs = newAllDocs;
// may not be essential
Collections.sort(allDocs);
allDocsAsSet = null;
}
use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.
the class Filter method matches.
public boolean matches(Document d) {
if (d instanceof DatedDocument) {
DatedDocument dd = (DatedDocument) d;
if (!matchesDate(dd))
return false;
}
if (keywords != null && keywords.size() > 0) {
log.warn("Filtering by keywords during fetch&index is currently disabled");
Util.softAssert(false, log);
// String s = d.getContents().toLowerCase();
// // check for all keywords, if any absent return false
// for (String keyword: keywords)
// if (s.indexOf(keyword) < 0)
// return false;
}
// extra checks for email doc
if (d instanceof EmailDocument) {
// check if any of the people involved in this message are one of personContacts
EmailDocument ed = (EmailDocument) d;
if (personContacts.size() > 0) {
// if this is the case, we will explicitly apply the filter again, so its ok.
if (addressBook != null) {
List<String> list = ed.getAllAddrs();
Set<Contact> contactsInThisMessage = new LinkedHashSet<>();
for (String s : list) {
Contact c = addressBook.lookupByEmail(s);
if (c != null)
contactsInThisMessage.add(c);
}
contactsInThisMessage.retainAll(personContacts);
if (contactsInThisMessage.size() == 0)
return false;
}
}
if (sentMessagesOnly) {
if (ownContact != null) {
String fromEmail = ed.getFromEmailAddress();
Set<String> ownAddrs = ownContact.getEmails();
if (!ownAddrs.contains(fromEmail))
return false;
} else {
log.warn("WARNING: user error: trying to use sent-only option without setting user's own contact info");
// in this case, we assume a match implicitly because we don't want to filter out all messages
}
}
}
return true;
}
use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.
the class SearchResult method filterForCorrespondents.
/**
* returns only the docs where the name or email address in the given field matches correspondentsStr in the given field(s).
* correspondentsStr can be or-delimited and specify multiple strings.
*/
public static SearchResult filterForCorrespondents(SearchResult inputSet, String correspondentsStr, boolean checkToField, boolean checkFromField, boolean checkCcField, boolean checkBccField) {
Set<Contact> searchedContacts = new LinkedHashSet<>();
AddressBook ab = inputSet.archive.addressBook;
Set<String> correspondents = Util.splitFieldForOr(correspondentsStr);
for (String s : correspondents) {
// this lookup will normalize, be case-insensitive, etc.
Collection<Contact> contacts = ab.lookupByEmailOrName(s);
if (contacts != null)
searchedContacts.addAll(contacts);
}
// keep on removing those documents from allDocs which do not have any contact that matches ANY of searchedContacts
inputSet.matchedDocs = inputSet.matchedDocs.entrySet().stream().filter(k -> {
EmailDocument ed = (EmailDocument) k.getKey();
Collection<Contact> contactsOfInterest = new LinkedHashSet<>();
if (// add from addresses
checkFromField && ed.from != null)
contactsOfInterest.addAll(Arrays.stream(ed.from).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
if (// add to address
checkToField && ed.to != null)
contactsOfInterest.addAll(Arrays.stream(ed.to).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
if (// add ccd addresses
checkCcField && ed.cc != null)
contactsOfInterest.addAll(Arrays.stream(ed.cc).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
if (// add bcc address
checkToField & ed.bcc != null)
contactsOfInterest.addAll(Arrays.stream(ed.bcc).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
// Collection<Contact> contactsInMessage = EmailUtils.getContactsForMessage(ab, ed);
return contactsOfInterest.stream().anyMatch(searchedContacts::contains);
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return inputSet;
}
use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.
the class JSONUtils method jsonForNewNewAlgResults.
public static String jsonForNewNewAlgResults(AddressBook ab, String resultsFile) throws IOException, JSONException {
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(resultsFile)));
JSONObject result = new JSONObject();
JSONArray groups = new JSONArray();
int groupNum = 0;
while (true) {
String line = lnr.readLine();
if (line == null)
break;
line = line.trim();
// line: group 8, freq 49: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu
// ignore lines without a ':'
int idx = line.indexOf(":");
if (idx == -1)
continue;
String vars = line.substring(0, idx);
// vars: freq=5 foo bar somevar=someval
// we'll pick up all tokens with a = in them and assume they mean key=value
StringTokenizer varsSt = new StringTokenizer(vars);
JSONObject group = new JSONObject();
while (varsSt.hasMoreTokens()) {
String str = varsSt.nextToken();
// str: freq=5
int x = str.indexOf("=");
if (x >= 0) {
String key = str.substring(0, x);
String value = "";
// we should handle the case of key= (empty string)
if (x < str.length() - 1)
value = str.substring(x + 1);
group.put(key, value);
}
}
String groupsStr = line.substring(idx + 1);
// groupsStr: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu
StringTokenizer st = new StringTokenizer(groupsStr);
JSONArray groupMembers = new JSONArray();
int i = 0;
while (st.hasMoreTokens()) {
String s = st.nextToken();
Contact ci = ab.lookupByEmail(s);
if (ci == null) {
System.out.println("WARNING: no contact info for email address: " + s);
continue;
}
groupMembers.put(i++, ci.toJson());
}
group.put("members", groupMembers);
groups.put(groupNum, group);
groupNum++;
}
result.put("groups", groups);
return result.toString();
}
use of edu.stanford.muse.AddressBookManager.Contact in project epadd by ePADD.
the class JSONUtils method jsonForAddressBook.
private static JSONObject jsonForAddressBook(AddressBook ab) throws JSONException {
JSONObject result = new JSONObject();
JSONArray entries = new JSONArray();
List<Contact> allContacts = ab.allContacts();
// ankita wants the first contact to always be me
// ** CHANGED CODE BY ANKITA **
int i = 1;
// Set<String> myAddresses = ab.getOwnAddrsSet();
Contact me = ab.getContactForSelf();
Set<String> myAddresses = me.getEmails();
for (Contact c : allContacts) {
if (myAddresses.equals(c.getEmails()))
entries.put(0, c.toJson());
else
entries.put(i++, c.toJson());
}
// entries.put(0, myAddresses);
/*
* OLD CODE:
int i = 0;
Contact me = ab.getOwnContact();
if (me != null)
{
entries.put (0, me.toJson()); // ORIGINAL: entries.put(0, me);
i++;
}
for (Contact c: allContacts)
{
if (me == c)
continue;
entries.put(i++, c.toJson());
}
*/
result.put("entries", entries);
return result;
}
Aggregations