This task is part of project08 which is due at 23:00 EST on 2024-11-05.
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)
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:
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.
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.
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.
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.
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..
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.
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 []:FileonlyEvents('nobody.txt', 'onlyEvents-nobody.txt')
onlyEvents-nobody.txtnobody has joined the chatroom nobody has left the chatroomIn []:FileonlyEvents('numbers.txt', 'onlyEvents-numbers.txt')
onlyEvents-numbers.txtIn []:FileonlyEvents('youMe.txt', 'onlyEvents-youMe.txt')
onlyEvents-youMe.txtyou 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 chatroomIn []:FileonlyEvents('forest.txt', 'onlyEvents-forest.txt')
onlyEvents-forest.txtsquirrel 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 []:FilelistSenders('numbers.txt', 'listSenders-numbers.txt')
listSenders-numbers.txtone two three four five six seven eightIn []:FilelistSenders('youMe.txt', 'listSenders-youMe.txt')
listSenders-youMe.txtme you greatPerson632In []:FilelistSenders('forest.txt', 'listSenders-forest.txt')
listSenders-forest.txtsquirrel 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 []:FilemessagesWithWord('numbers.txt', 'hey', 'messagesWithWord-numbers-hey.txt')
messagesWithWord-numbers-hey.txtfour: hey six: hey, um, ...I'm afraid of sevenIn []:FilemessagesWithWord('youMe.txt', 'hey', 'messagesWithWord-youMe-hey.txt')
messagesWithWord-youMe-hey.txtyou: hey you: hey! you: hey listen!In []:FilemessagesWithWord('forest.txt', 'BARK', 'messagesWithWord-forest-BARK.txt')
messagesWithWord-forest-BARK.txtdog: BARK BARK BARK dog: BARK BARK BARK, BARK! dog: BARKBARKBARKBARKIn []:FilemessagesWithWord('forest.txt', 'caw', 'messagesWithWord-forest-caw.txt')
messagesWithWord-forest-caw.txtcrow: 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 []:FilemessagesFromUser('numbers.txt', 'one', 'messagesFromUser-numbers-one.txt')
messagesFromUser-numbers-one.txthiIn []:FilemessagesFromUser('numbers.txt', 'two', 'messagesFromUser-numbers-two.txt')
messagesFromUser-numbers-two.txthelloIn []:FilemessagesFromUser('numbers.txt', 'eight', 'messagesFromUser-numbers-eight.txt')
messagesFromUser-numbers-eight.txtaaah :(In []:FilemessagesFromUser('youMe.txt', 'you', 'messagesFromUser-youMe-you.txt')
messagesFromUser-youMe-you.txtBye~ hey hey! hey listen! we're in an example! it's an example! isn't this great?In []:FilemessagesFromUser('youMe.txt', 'me', 'messagesFromUser-youMe-me.txt')
messagesFromUser-youMe-me.txtHi! whaaaat? hooooray! ^.^; um... sure, I guess it's great.In []:FilemessagesFromUser( 'youMe.txt', 'greatPerson632', 'messagesFromUser-youMe-greatPerson632.txt' )
messagesFromUser-youMe-greatPerson632.txtYES, IT IS GREAT.In []:FilemessagesFromUser( 'forest.txt', 'squirrel', 'messagesFromUser-forest-squirrel.txt' )
messagesFromUser-forest-squirrel.txtchatter chatter chatter chatter chatter chatterIn []:FilemessagesFromUser('forest.txt', 'snail', 'messagesFromUser-forest-snail.txt')
messagesFromUser-forest-snail.txt
onlyEvents
must write the correct data into the appropriate file
onlyEvents
function is run must match what the solution writes.listSenders
must write the correct data into the appropriate file
listSenders
function is run must match what the solution writes.messagesWithWord
must write the correct data into the appropriate file
messagesWithWord
function is run must match what the solution writes.messagesFromUser
must write the correct data into the appropriate file
messagesFromUser
function is run must match what the solution writes.listSenders
must write the correct data into the appropriate file
listSenders
function is run must match what the solution writes.messagesWithWord
must write the correct data into the appropriate file
messagesWithWord
function is run must match what the solution writes.messagesFromUser
must write the correct data into the appropriate file
messagesFromUser
function is run must match what the solution writes.onlyEvents
def
to define onlyEvents
onlyEvents
, use a with statement in at least 2 places.open
onlyEvents
, call open
in at least one place.listSenders
def
to define listSenders
listSenders
, use a with statement in at least 2 places.open
listSenders
, call open
in at least one place.messagesWithWord
def
to define messagesWithWord
messagesWithWord
, use a with statement in at least 2 places.open
messagesWithWord
, call open
in at least one place.messagesFromUser
def
to define messagesFromUser
messagesFromUser
, use a with statement in at least 2 places.open
messagesFromUser
, call open
in at least one place.