Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arunesh conversation csv loader #49

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/cmn/conversation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
import csv

from cmn.message import Message


class Conversation(Object):
def __init__(self, id, messages, participants):
class Conversation:
def __init__(self, id):
self.id = id
self.messages = messages
self.participants = participants
self.messages = []
self.participants = set()
self.conv_size = 0

@staticmethod
def loader(path):
Expand All @@ -17,43 +16,35 @@ def loader(path):
@staticmethod
def csv_loader(filepath):
convs = {}

with open(filepath, mode="r", newline="", encoding="utf-8") as csvfile:
csv_reader = csv.DictReader(csvfile)
# conversation = Conversation()
for row in csv_reader:
# In the future, if we are having data which needs to be tweaked
# before we import, could be taken care here.
try:
# Assign the conversation id to Conversation Object
# conversation.conversation_id = row['conv_id']

message = Message(
row["msg_line"],
row["author_id"],
row["time"],
row["msg_char_count"],
row["msg_word_count"],
row["conv_size"],
row["nauthor"],
row["text"],
row["tagged_predator"],
row["predatory_conv"],
)

if id not in convs: convs[id] = Conversation(row["conv_id"], None, None)
convs[id].add_message(message)
conv_id = row["conv_id"]
if conv_id not in convs: convs[conv_id] = Conversation(conv_id)

convs[conv_id].messages.append(message)
convs[conv_id].participants.add(row["author_id"])
convs[conv_id].conv_size = row["conv_size"]

except KeyError as e:
print(f"Import Error: {e}")

return convs

def __repr__(self):
repr_string = f"Conversation ID: {self.id}\nNumber of messages: {len(self.messages)}\n"

authors_list = "\n".join(self.participants)
repr_string = f"Conversation ID: {self.id}\nConversation Size: {self.conv_size}\nAuthors Involved: {len(list(self.participants))}\n{authors_list}\n"
if not self.messages: repr_string += "No messages found for this conversation.\n"
else:
for message in self.messages: repr_string += f"\n{message}"

return repr_string
2 changes: 1 addition & 1 deletion src/cmn/message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Message(Object):
class Message:
def __init__(
self,
idx: str,
Expand Down