From e0e18efa699eb01dc85c83369c7ff335721febf0 Mon Sep 17 00:00:00 2001 From: Jorin Weatherston Date: Tue, 24 Apr 2018 16:06:19 -0700 Subject: [PATCH 1/2] Major improvements to command line API and timeline parsing --- booksoup/FbTime.py | 15 +++++++++++---- demo_interaction_frequency.py | 20 +++++++++++++++++--- demo_interaction_timeline.py | 5 +++-- demo_sentiment_timeline.py | 5 +++-- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/booksoup/FbTime.py b/booksoup/FbTime.py index e1d1874..3631f11 100644 --- a/booksoup/FbTime.py +++ b/booksoup/FbTime.py @@ -17,8 +17,12 @@ def interaction_freq(self): for date_str in self.span_meta: time = date_str.split("at ")[1][:5] + amOrPm = date_str.split(":")[1][2:4] hour = time.split(":")[0] - times[hour+":00"] += 1 + if len(hour) == 1: + times["0"+hour+":00"+amOrPm] += 1 + else: + times[hour+":00"+amOrPm] += 1 return times # Returns a dict where each key is a date and each value is the number of @@ -33,8 +37,11 @@ def interaction_timeline(self, name, messages): # Creates a dictionary of times on the hour where each value is 0. def generate_time_dict(self): times = {} - for h in range(0,24): - time = self.__pad(h) + ":" + "00" + for h in range(1,13): + time = self.__pad(h) + ":" + "00am" + times[time] = 0 + for h in range(1,13): + time = self.__pad(h) + ":" + "00pm" times[time] = 0 return times @@ -80,7 +87,7 @@ def span_meta_to_date(self, span_str, interval="month"): span_str = ''.join(span_str.rsplit(',', 1)) date_arr = span_str.split(", ")[1].split(" ")[:3] - date_str = date_arr[2]+"-"+self.__pad(list(calendar.month_name).index(date_arr[1])) + date_str = date_arr[2]+"-"+self.__pad(list(calendar.month_name).index(date_arr[0])) if interval == "day": date_str += "-"+self.__pad(date_arr[0]) return date_str diff --git a/demo_interaction_frequency.py b/demo_interaction_frequency.py index b7c39b2..e22bd2e 100644 --- a/demo_interaction_frequency.py +++ b/demo_interaction_frequency.py @@ -3,16 +3,30 @@ from booksoup import BookSoup import numpy as np import matplotlib.pyplot as plt +import sys # Enter the path to the top level of your facebook data folder below. -me = BookSoup("facebook-data") +me = BookSoup(sys.argv[2]) # Enter the name of the conversation or the numerical ID below. -contact = me.load_conversation(274) +contact = me.load_conversation(sys.argv[1]) times = contact.interaction_freq() +def get24HourTime(elem): + amOrPm = elem.split(":")[1][2:4] + hour = int(elem.split(":")[0]) + if amOrPm == "am": + if hour == 12: + return hour+12 + return hour + else: + if hour == 12: + return hour + return hour+12 -objects = sorted(times.keys()) + +objects = sorted(times.keys(), key=get24HourTime) +print objects y_pos = np.arange(len(objects)) vals = [times[t] for t in objects] diff --git a/demo_interaction_timeline.py b/demo_interaction_timeline.py index ae6e4c7..e77de73 100644 --- a/demo_interaction_timeline.py +++ b/demo_interaction_timeline.py @@ -3,16 +3,17 @@ from booksoup import BookSoup import numpy as np import matplotlib.pyplot as plt +import sys times = [] objects = [] vals = [] # Enter the path to the top level of your facebook data folder below. -me = BookSoup("facebook-data") +me = BookSoup(sys.argv[2]) # Enter the name of the conversation or the numerical ID below. -conversation = me.load_conversation(108) +conversation = me.load_conversation(sys.argv[1]) for participant in conversation.participants: timeline = conversation.interaction_timeline(participant) diff --git a/demo_sentiment_timeline.py b/demo_sentiment_timeline.py index 90f562f..14c932b 100644 --- a/demo_sentiment_timeline.py +++ b/demo_sentiment_timeline.py @@ -3,16 +3,17 @@ from booksoup import BookSoup import numpy as np import matplotlib.pyplot as plt +import sys times = [] objects = [] vals = [] # Enter the path to the top level of your facebook data folder below. -me = BookSoup("facebook-data") +me = BookSoup(sys.argv[2]) # Enter the name of the conversation or the numerical ID below. -conversation = me.load_conversation(104) +conversation = me.load_conversation(sys.argv[1]) for participant in conversation.participants: timeline = conversation.sentiment_timeline(participant) From a5a598b38394ce00dcc4abfbdd06e8222035a6f2 Mon Sep 17 00:00:00 2001 From: Jorin Weatherston Date: Wed, 25 Apr 2018 15:31:56 -0700 Subject: [PATCH 2/2] Added day interval support to sentiment timeline --- booksoup/BookSoup.py | 2 +- booksoup/Conversation.py | 7 +++++-- booksoup/FbTime.py | 14 +++++++++++++- booksoup/Sentiment.py | 6 +++--- demo_interaction_frequency.py | 17 ++--------------- demo_sentiment_timeline.py | 5 +++-- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/booksoup/BookSoup.py b/booksoup/BookSoup.py index bcfa02c..e02b34b 100644 --- a/booksoup/BookSoup.py +++ b/booksoup/BookSoup.py @@ -45,7 +45,7 @@ def load_conversation(self, search_name, interval="month"): for link in convo_links: if link.text != search_name: continue - contact = Conversation(os.path.join(self.__path, link["href"])) + contact = Conversation(os.path.join(self.__path, link["href"]), interval=interval) self.conversations[contact.name] = contact return contact return None diff --git a/booksoup/Conversation.py b/booksoup/Conversation.py index fbe5a00..54da48f 100644 --- a/booksoup/Conversation.py +++ b/booksoup/Conversation.py @@ -30,12 +30,15 @@ def interaction_freq(self): def interaction_timeline(self, name): return self.__fbt.interaction_timeline(name, self.messages) - def sentiment_timeline(self, name): - return self.__sent.sentiment_timeline(name) + def sentiment_timeline(self, name, interval): + return self.__sent.sentiment_timeline(name, interval) def avg_sentiment(self, name): return self.__sent.avg_sentiment(name) + def get24HourTime(self, elem): + return self.__fbt.get24HourTime(elem) + # Returns a list of participants in the conversation. def __scrape_participants(self): users = [] diff --git a/booksoup/FbTime.py b/booksoup/FbTime.py index 3631f11..23b79e1 100644 --- a/booksoup/FbTime.py +++ b/booksoup/FbTime.py @@ -89,5 +89,17 @@ def span_meta_to_date(self, span_str, interval="month"): date_arr = span_str.split(", ")[1].split(" ")[:3] date_str = date_arr[2]+"-"+self.__pad(list(calendar.month_name).index(date_arr[0])) if interval == "day": - date_str += "-"+self.__pad(date_arr[0]) + date_str += "-"+self.__pad(date_arr[1]) return date_str + + def get24HourTime(self, elem): + amOrPm = elem.split(":")[1][2:4] + hour = int(elem.split(":")[0]) + if amOrPm == "am": + if hour == 12: + return hour+12 + return hour + else: + if hour == 12: + return hour + return hour+12 \ No newline at end of file diff --git a/booksoup/Sentiment.py b/booksoup/Sentiment.py index aade6ca..f466438 100644 --- a/booksoup/Sentiment.py +++ b/booksoup/Sentiment.py @@ -11,9 +11,9 @@ def __init__(self, messages, fbt): self.fbt = fbt self.messages = messages - def sentiment_timeline(self, name): - timeline = self.fbt.generate_date_dict() - sentiment_counts = self.fbt.generate_date_dict() + def sentiment_timeline(self, name, interval="month"): + timeline = self.fbt.generate_date_dict(interval) + sentiment_counts = self.fbt.generate_date_dict(interval) for message in self.messages: if message.content is None or message.name != name: continue diff --git a/demo_interaction_frequency.py b/demo_interaction_frequency.py index e22bd2e..39394c5 100644 --- a/demo_interaction_frequency.py +++ b/demo_interaction_frequency.py @@ -9,24 +9,11 @@ me = BookSoup(sys.argv[2]) # Enter the name of the conversation or the numerical ID below. -contact = me.load_conversation(sys.argv[1]) +contact = me.load_conversation(sys.argv[1], sys.argv[3]) times = contact.interaction_freq() -def get24HourTime(elem): - amOrPm = elem.split(":")[1][2:4] - hour = int(elem.split(":")[0]) - if amOrPm == "am": - if hour == 12: - return hour+12 - return hour - else: - if hour == 12: - return hour - return hour+12 - -objects = sorted(times.keys(), key=get24HourTime) -print objects +objects = sorted(times.keys(), key=contact.get24HourTime) y_pos = np.arange(len(objects)) vals = [times[t] for t in objects] diff --git a/demo_sentiment_timeline.py b/demo_sentiment_timeline.py index 14c932b..e048a52 100644 --- a/demo_sentiment_timeline.py +++ b/demo_sentiment_timeline.py @@ -11,12 +11,13 @@ # Enter the path to the top level of your facebook data folder below. me = BookSoup(sys.argv[2]) +interval = sys.argv[3] # Enter the name of the conversation or the numerical ID below. -conversation = me.load_conversation(sys.argv[1]) +conversation = me.load_conversation(sys.argv[1], interval=interval) for participant in conversation.participants: - timeline = conversation.sentiment_timeline(participant) + timeline = conversation.sentiment_timeline(participant, interval) sorted_keys = sorted(timeline.keys()) times.append(timeline) objects.append(sorted_keys)