Instructions for transcriptManager

(produced at 00:02 a.m. UTC on 2024-03-25)

This task is part of project08 which is due at 23:00 EDT on 2024-04-02.

You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more.

You can download the starter code for this task using this link.

You can submit this task using this link.

Put all of your work for this task into the file transcriptManager.py
(you will create this file from scratch)

Overview

In this task, you will practice reading and writing files by writing a series of functions which deal with chat transcripts. Here's an example of what a transcript file might contain:

you has joined the chatroom
me has joined the chatroom
me: Hi!
you: Bye~
you has left the chatroom
you has joined the chatroom
you: hey
you: hey!
you: hey listen!
me: whaaaat?
you: we're in an example!
me has left the chatroom
me has joined the chatroom
you: it's an example!
me: hooooray! ^.^;
greatPerson632 has entered the chatroom
you: isn't this great?
me: um... sure, I guess it's great.
me has left the chatroom
greatPerson632: YES, IT IS GREAT.
you has left the chatroom
greatPerson632 has left the chatroom

Notice several things:

  1. There are a mix of chat lines where someone is talking, and event lines describing something that happens (like someone joining or leaving).
  2. For chat lines, each line starts with the username of whoever is sending the message, followed by a colon and a single space.

In this project, we will treat any line with a colon in it anywhere as a chat message, and all other lines will be "events."

Every function that you write in this file will need to read info from a file, and write its result to another file. To do this, you are required to use the with open(...) as ...: pattern to ensure the file will be closed. Your functions will not need to return or print anything. As usual, each function must be documented.

Functions to Define

Part A: Listing Events

Define a 2-parameter function called onlyEvents which takes the names of an input file and an output file as its parameters. It should open the input file, read in the transcript there, and write only the events (no messages) to the output file, in the same order they appear in in the input file.

Recall from above that anything with a colon (':') in it counts as a message, and anything without a colon counts as an "event."

These onlyEvents examples demonstrate how it should work. Note that your function must use the with open(...) as ...: pattern to open both files.

Part B: Listing Senders

Define a 2-parameter function called listSenders which takes input and output filenames as arguments. It should read the transcript in the input file, and write into the output file a list of the usernames that appear in the transcript. Note that it should only list the names of users who send a message (those who are only involved in events are not counted) and it should only list each username once, no matter how many messages they send. Usernames should be listed in the order in which they first send a message in the file (this can be done naturally without using any kind of sorting).

These listSenders examples demonstrate how it should work. Note that your function must use the with open(...) as ...: pattern to open both files.

Part C: Searching Messages

Define a 3-parameter function named messagesWithWord which takes an input filename, a string to search for, and an output filename as its three parameters. It should read the transcript in the input file, and search for messages where the message part (not the username) contains the given string, regardless of case. Those messages (including their username part) should be written into the output file, in the same order they appear in the input file.

These messagesWithWord examples demonstrate how it should work. Note that your function must use the with open(...) as ...: pattern to open both files.

Part D: Filtering by Sender

Define a 3-parameter function named messagesFromUser which takes an input filename, a username, and an output filename as its three parameters. It should read the transcript in the input file, search for messages which were sent by the specified username (matching case exactly) and write those messages into the output file, without the username, the colon, or the space that precedes each message.

These messagesFromUser examples demonstrate how it should work Note that your function must use the with open(...) as ...: pattern to open both files..

The end

That's it: once you've defined those four functions you're done. The supplied test_transcriptManager.py file can be used to test your functions. Also note that there is a resetFiles.py file which can be used to reset the testing transcripts we've provided back to their original states, in case you accidentally write code that messes them up.

Examples

onlyEvents Examples

Examples of how onlyEvents should work. Note that any line which does not include a ':' counts as an event. Not all files necessarily have events in them. Note also that what is shown here for each example is the contents of a specific file (the one named as the output file). These functions do not print or return anything. The same is true for all of the examples in this project.

In []:
onlyEvents('nobody.txt', 'onlyEvents-nobody.txt')
File
onlyEvents-nobody.txt
nobody has joined the chatroom nobody has left the chatroom
In []:
onlyEvents('numbers.txt', 'onlyEvents-numbers.txt')
File
onlyEvents-numbers.txt
In []:
onlyEvents('youMe.txt', 'onlyEvents-youMe.txt')
File
onlyEvents-youMe.txt
you has joined the chatroom me has joined the chatroom you has left the chatroom you has joined the chatroom me has left the chatroom me has joined the chatroom greatPerson632 has entered the chatroom me has left the chatroom you has left the chatroom greatPerson632 has left the chatroom
In []:
onlyEvents('forest.txt', 'onlyEvents-forest.txt')
File
onlyEvents-forest.txt
squirrel has joined the chatroom crow has joined the chatroom snail has joined the chatroom dog has joined the chatroom squirrel runs up a tree dog wags his tail excitedly deer has entered the chatroom dog chases deer deer has left the chatroom dog has left the chatroom squirrel has left the chatroom crow has left the chatroom ... snail has left the chatroom

listSenders Examples

Examples of how listSenders should work. Note that the senders appear in the order they first speak in the transcript. Also note that users who never send a message, like the snail in the forest, are not listed.

In []:
listSenders('numbers.txt', 'listSenders-numbers.txt')
File
listSenders-numbers.txt
one two three four five six seven eight
In []:
listSenders('youMe.txt', 'listSenders-youMe.txt')
File
listSenders-youMe.txt
me you greatPerson632
In []:
listSenders('forest.txt', 'listSenders-forest.txt')
File
listSenders-forest.txt
squirrel crow dog deer

messagesWithWord Examples

Examples of how messagesWithWord should work. Note that the search is case-insensitive and that it doesn't literally have to match a word, it can match part of a word. Not shown here: lines should not be included if only the username matches.

In []:
messagesWithWord('numbers.txt', 'hey', 'messagesWithWord-numbers-hey.txt')
File
messagesWithWord-numbers-hey.txt
four: hey six: hey, um, ...I'm afraid of seven
In []:
messagesWithWord('youMe.txt', 'hey', 'messagesWithWord-youMe-hey.txt')
File
messagesWithWord-youMe-hey.txt
you: hey you: hey! you: hey listen!
In []:
messagesWithWord('forest.txt', 'BARK', 'messagesWithWord-forest-BARK.txt')
File
messagesWithWord-forest-BARK.txt
dog: BARK BARK BARK dog: BARK BARK BARK, BARK! dog: BARKBARKBARKBARK
In []:
messagesWithWord('forest.txt', 'caw', 'messagesWithWord-forest-caw.txt')
File
messagesWithWord-forest-caw.txt
crow: CAW crow: CAW, CAW

messagesFromUser Examples

Examples of how messagesFromUser should work. Note that some users may not have any messages, in which case the result file is empty. Also note that the result file does not include the username for each message, the colon after the username, or the space immediately following that colon.

In []:
messagesFromUser('numbers.txt', 'one', 'messagesFromUser-numbers-one.txt')
File
messagesFromUser-numbers-one.txt
hi
In []:
messagesFromUser('numbers.txt', 'two', 'messagesFromUser-numbers-two.txt')
File
messagesFromUser-numbers-two.txt
hello
In []:
messagesFromUser('numbers.txt', 'eight', 'messagesFromUser-numbers-eight.txt')
File
messagesFromUser-numbers-eight.txt
aaah :(
In []:
messagesFromUser('youMe.txt', 'you', 'messagesFromUser-youMe-you.txt')
File
messagesFromUser-youMe-you.txt
Bye~ hey hey! hey listen! we're in an example! it's an example! isn't this great?
In []:
messagesFromUser('youMe.txt', 'me', 'messagesFromUser-youMe-me.txt')
File
messagesFromUser-youMe-me.txt
Hi! whaaaat? hooooray! ^.^; um... sure, I guess it's great.
In []:
messagesFromUser( 'youMe.txt', 'greatPerson632', 'messagesFromUser-youMe-greatPerson632.txt' )
File
messagesFromUser-youMe-greatPerson632.txt
YES, IT IS GREAT.
In []:
messagesFromUser( 'forest.txt', 'squirrel', 'messagesFromUser-forest-squirrel.txt' )
File
messagesFromUser-forest-squirrel.txt
chatter chatter chatter chatter chatter chatter
In []:
messagesFromUser('forest.txt', 'snail', 'messagesFromUser-forest-snail.txt')
File
messagesFromUser-forest-snail.txt

Rubric

Group goals:
 
unknown All functions are documented
Each function you define must include a non-empty documentation string as the very first thing in the function.
 
unknown onlyEvents must write the correct data into the appropriate file
The data written to the appropriate file when your onlyEvents function is run must match what the solution writes.
 
unknown listSenders must write the correct data into the appropriate file
The data written to the appropriate file when your listSenders function is run must match what the solution writes.
 
unknown messagesWithWord must write the correct data into the appropriate file
The data written to the appropriate file when your messagesWithWord function is run must match what the solution writes.
 
unknown messagesFromUser must write the correct data into the appropriate file
The data written to the appropriate file when your messagesFromUser function is run must match what the solution writes.
 
unknown listSenders must write the correct data into the appropriate file
The data written to the appropriate file when your listSenders function is run must match what the solution writes.
 
unknown messagesWithWord must write the correct data into the appropriate file
The data written to the appropriate file when your messagesWithWord function is run must match what the solution writes.
 
unknown messagesFromUser must write the correct data into the appropriate file
The data written to the appropriate file when your messagesFromUser function is run must match what the solution writes.
 
unknown Define onlyEvents
Use def to define onlyEvents
 
unknown Use a with statement
Within the definition of onlyEvents, use a with statement in at least 2 places.
 
unknown Call open
Within with statements within the definition of onlyEvents, call open in at least one place.
 
unknown Define listSenders
Use def to define listSenders
 
unknown Use a with statement
Within the definition of listSenders, use a with statement in at least 2 places.
 
unknown Call open
Within with statements within the definition of listSenders, call open in at least one place.
 
unknown Define messagesWithWord
Use def to define messagesWithWord
 
unknown Use a with statement
Within the definition of messagesWithWord, use a with statement in at least 2 places.
 
unknown Call open
Within with statements within the definition of messagesWithWord, call open in at least one place.
 
unknown Define messagesFromUser
Use def to define messagesFromUser
 
unknown Use a with statement
Within the definition of messagesFromUser, use a with statement in at least 2 places.
 
unknown Call open
Within with statements within the definition of messagesFromUser, call open in at least one place.