ChatLogReader 2 - Part 1
ChatLogReader 21 - Part 1 - From Chatlogs to Tokens
I have been working on a tool for managing various chatlogs that I collected over the last 20 years. This provided some interesting challenges mostly due to the fact that the chatlogs are stored in around 10 different formats. All of the formats have different quirks and present their information in different ways.
The goal was to combine them into a common format and store them in a sqlite database, so I can search through them and immerse myself in nostalgia (and potentially cringe).
This will be a multi part blog article discussing the interesting parts of the project. This part discusses the general design and one step in the processing.
In case you're already interested, the complete project can be found on gitlab: gitlab.com/xicalango/clr2.
Chatlog formats
Just to give you an example of what the different exports look like, here is a list of some of the different formats in my collection, with a short description of what is found in the file.
Kopete XML format
<!DOCTYPE Kopete-History>
<kopete-history version="0.9" >
<head>
<date month="3" year="2007" />
<contact contactId="987654321" type="myself" />
<contact contactId="123456789" />
</head>
<msg nick="User2" in="1" from="123456789" time="14 21:53:14" >hi</msg>
<msg nick="User2" in="1" from="123456789" time="14 21:53:25" >sagen</msg>
<msg nick="User1" in="0" from="987654321" time="14 21:53:32" >weiß</msg>
<msg nick="User1" in="0" from="987654321" time="14 21:54:4" >und</msg>
</kopete-history>
- Format:
XML - Header contains start month and year:
<date month="3" year="2007" /> - Subsequent messages only show day and time:
time="14 21:53:14" - Header shows ICQ numbers of participants and tags which one is the "receiver":
contactId="987654321" type="myself" - List of messages shows full information: Username, ICQ number, whether the message was received or not:
nick="User1" in="0" from="987654321"
Plaintext log
Session Start (987654321:User2): Thu Dec 21 13:41:52 2006
[13:41] User2: hey^^
[13:42] User1: hay ;)
[14:17] *** Thu Dec 21 14:17:39 2006 - User2 ist nun Offline.
[18:00] *** Sie wurden getrennt. Thu Dec 21 18:00:54 2006.
[18:37] *** Sie wurden getrennt. Thu Dec 21 18:37:08 2006.
[21:37] *** Sie wurden getrennt. Thu Dec 21 21:37:46 2006.
Session Close (User2): Thu Dec 21 22:09:57 2006
Session Start (987654321:User2): Thu Dec 28 18:56:30 2006
[18:56] User2: hey^^
[18:56] User2: wie gehts?
[18:56] User1: hay
[18:56] User1: passd scho
[21:41] *** Sie wurden getrennt. Thu Dec 28 21:41:32 2006.
[21:41] *** Thu Dec 28 21:41:39 2006 - User2 ist nun Online.
[22:07] *** Thu Dec 28 22:07:56 2006 - User2 ist nun Offline.
Session Close (User2): Fri Dec 29 00:05:26 2006
- Format:
plaintext - Header contains start timestamp:
Thu Dec 21 13:41:52 2006 - Messages only have time:
13:41(accurate to the minute only) - Header contains only ICQ number and username of messaging partner:
987654321:User2, but not of the receiver - Chatlogs automatically separated by "sessions"
Second Plaintext log
User1 messages history
Date : 08.08.2005 Time : 14:52
To : User1
lustig
Date : 08.08.2005 Time : 14:51
From : User2
Und wie wars?
Date : 08.08.2005 Time : 14:51
To : User1
hi
Date : 08.08.2005 Time : 14:50
From : User2
Moin moin
- Format:
plaintext - Header does not contain useful information
- Each message has a minute-accurate timestamp:
Date : 08.08.2005 Time : 14:52 - Each message has the user information stored, and whether the message was sent or received:
To : User1/From : User2 - Note: the messages are stored in reverse order: The oldest messages is at the bottom
HTML format
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Conversation with 123456789 at Mo 04 Jan 2010 15:20:15 CET on 987654321 (icq)</title></head><body><h3>Conversation with 137749571 at Mo 04 Jan 2010 15:20:15 CET on 987654321 (icq)</h3>
<font color="#A82F2F"><font size="2">(15:20:15)</font> <b>User2:</b></font> hey ho<br/>
<font color="#16569E"><font size="2">(15:20:26)</font> <b>User1:</b></font> hoi<br/>
- Format:
HTML - Header has start timestamp:
Mo 04 Jan 2010 15:20:15 CET - Messages only have a time:
(15:20:15) - Header shows protocol:
(icq) - Header shows ICQ numbers:
123456789/987654321 - Messages only show usernames:
User2/User1 - Messages may have different colors depending on sender/receiver:
#A82F2F/#16569E - Separation of chatlogs by date
facebook format
{
"participants": [
{
"name": "User2"
},
{
"name": "User1"
}
],
"messages": [
{
"sender_name": "User1",
"timestamp_ms": 1340201234567,
"content": "hm"
},
{
"sender_name": "User2",
"timestamp_ms": 1340201235678,
"content": "erkl\u00c3\u00a4rb\u00c3\u00a4r"
}
],
"title": "Test Conversation",
"thread_path": "inbox/12345678912345678"
}
- Format:
JSON - Timestamp associated with each message
- No separation by "conversation" or "session"
- Header contains list of participants
- Each message also contains the name of the participant
- Conversations are not separated
- Note the encoding of special characters:
erkl\u00c3\u00a4rb\u00c3\u00a4r. We will touch on this later
Find more example input formats here: https://gitlab.com/xicalango/clr2/-/tree/main/clr-ingest/res
Format differences
From the chatlogs above we can already see a bunch of differences in how the information is presented:
- Different formats: xml, html, plain text, json
- Different ways on how the timestamp is stored:
- Some chatlogs have a full timestamp associated with each message
- Some chatlogs have a start timestamp in the header, and each message only has a fraction of the remaining information
- Also the accuracy of the timestamp varies a lot
- Different ways on how user information is stored:
- Some show user information with username and user id
- Some have no clear connection between username and user id
- Separation of chatlogs
- Some chatlogs are one huge file with all messages
- Some chatlogs are separated by date
- Some chatlogs have a concept of
sessionand separate the messages by that
Design
To work with this heterogeneous set of chatlogs where the information is stored so differently my plan was to separate the "information extraction" from the "chatlog assembly".
That is, one part of the program is just concerned to extract the information as found in the chatlog and convert them into a well known set of "information bits" (which i called "tokens"). The tokens are designed to carry a single bit of information, closely mirroring how they appear in the input source, but well-defined and unified in their format.
For example tokens could be time related: TimeInfo(DateTime("2010-01-04", "14:20:15", "CET")), user related: UserInfo{ id = "123456789", name = "User2" }, message related: MessageContent("hallo"), or just contain meta information, either for structure: MetaStart/MetaEnd, MessageStart/MessageEnd, or for metadata: Platform("icq").
Depending on the context, tokens could mean different things as well, e.g. a TimeInfo token found between MetaStart/MetaEnd tokens gives time information about the whole conversation, while it being found between MessageStart/MessageEnd will relate the time information to a specific message.
Those tokens would be passed on to a different component which would take the tokens in arbitrary order and assmeble the chatlogs into a common format.
That way I just need to implement a "tokenizer" per chatlog format that generates a "stream of tokens" which then gets passed into a generic "Chatlog assembler" which then creates the final chatlog from the stream of tokens.
In a diagram the pipeline looks like this:
*************** +++++++++++++++++++++++++++
* XML chatlog * --------> + XML chatlog "tokenizer" + --------+
*************** +++++++++++++++++++++++++++ |
|
********************* +++++++++++++++++++++++++++++++++ | ******************** +++++++++++++++++++++ ************
* Plaintext chatlog * --> + Plaintext chatlog "tokenizer" + --+--> * Stream of tokens * --> + Chatlog assembler + --> * Chatlogs * *
********************* +++++++++++++++++++++++++++++++++ | ******************** +++++++++++++++++++++ ************ *
| *************
**************** ++++++++++++++++++++++++++++ |
* JSON chatlog * -------> + JSON chatlog "tokenizer" + -------+
**************** ++++++++++++++++++++++++++++
For each input format there exists a specific tokenizer, but the tokens are sent to a singular "Chatlog assembler".
Token example
To illustrate the "tokenization" with an example:
Taking an XML export:
<!DOCTYPE Kopete-History>
<kopete-history version="0.9" >
<head>
<date month="3" year="2007" />
<contact contactId="987654321" type="myself" />
<contact contactId="123456789" />
</head>
<msg nick="User2" in="1" from="123456789" time="14 21:53:14" >hi</msg>
<msg nick="User1" in="0" from="987654321" time="14 21:54:4" >hallo</msg>
</kopete-history>
This could be transformed into the following sequence of tokens:
ConversationStart
MetaStart
TimeInfo(Month(3))
TimeInfo(Year(2007))
UserInfo{ id = "987654321", tag = Receiver }
UserInfo{ id = "123456789", tag = Sender }
MetaEnd
MessageStart
UserInfo{ id = "123456789", name = "User2" }
MessageTag(Received)
TimeInfo(Day(14))
TimeInfo(Time("21:53:14"))
MessageContent("hi")
MessageEnd
MessageStart
UserInfo{ id = "987654321", name = "User1" }
MessageTag(Sent)
TimeInfo(Day(14))
TimeInfo(Time("21:54:04"))
MessageContent("hallo")
MessageEnd
ConversationEnd
While scanning through the XML file we convert the various information bits into a stream of tokens.
For example once we read the <date month="3" year="2007" /> part in the XML source, we can emit the tokens TimeInfo(Month(3)) and TimeInfo(Year(2007)).
To give another example here's how the HTML format could be translated:
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Conversation with 123456789 at Mo 04 Jan 2010 15:20:15 CET on 987654321 (icq)</title></head><body><h3>Conversation with 137749571 at Mo 04 Jan 2010 15:20:15 CET on 987654321 (icq)</h3>
<font color="#A82F2F"><font size="2">(15:20:15)</font> <b>User2:</b></font> hey ho<br/>
<font color="#16569E"><font size="2">(15:20:26)</font> <b>User1:</b></font> hoi<br/>
This could be tokenized as:
ConversationStart
MetaStart
UserInfo{ id = "123456789", tag = Sender }
UserInfo{ id = "987654321", tag = Receiver }
TimeInfo(DateTime("2010-01-04", "14:20:15", "CET"))
Platform("icq")
MetaEnd
MessageStart
UserInfo{ name = "User2" }
MessageTag(Received)
TimeInfo(Time("15:20:15"))
MessageContent("hey ho")
MessageEnd
MessageStart
UserInfo{ name = "User1" }
MessageTag(Sent)
TimeInfo(Time("15:20:26"))
MessageContent("hoi")
MessageEnd
ConversationEnd
Even though the input formats are vastly different we can reduce them to the same set of tokens.
Implementing tokenizers
Implementing a tokenizer for a given format is usually a straightforward proces. If the input format is a structured format (like XML or JSON) one can employ an existing parser for those formats (e.g. rust packages serde_json or xml) and use them to construct the tokens.
XML based parsers emit events2, e.g. one event could be StartElement("head"). This can be transformed into a token MetaStart. Or an event StartElement("date") can be transformed into a TimeInfo token, while extracting the month and year attributes.
For other formats a combination of regex and line-based processing usually works good enough as well. Especially the HTML format is well-structured, so it can be parsed using regex.
Find the implementation of different tokenizers here: https://gitlab.com/xicalango/clr2/-/tree/main/clr-ingest/src/parser/tokenizer
Tokens to chatlog
Now we have a stream of tokens, how do we assemble this into a unified chatlog? Given that this is its own challenge this will be discussed in part 2. Stay tuned!
In case you're wondering where the "2" is coming from: This is my third attempt at implementing this.
In retrospective maybe "event" would have been a better name than "token" in my implementation as well...