Skip to content

Commit 2bdab78

Browse files
martin-martinacsany
andcommitted
Apply TR suggestions
Co-authored-by: Philipp <filip.axani+github@gmail.com>
1 parent c208e6e commit 2bdab78

3 files changed

Lines changed: 18 additions & 17 deletions

File tree

chatterbot/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Build a Chatbot with Python and Chatterbot
1+
# Build a Chatbot with Python and ChatterBot
22

3-
This repository contains code related to the tutorial on [building a Python chatbot with chatterbot](https://realpython.com/build-a-chatbot-python-chatterbot/).
3+
This repository contains code related to the tutorial on [building a Python chatbot with ChatterBot](https://realpython.com/build-a-chatbot-python-chatterbot/).
44

55
Create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/), then install the necessary dependencies:
66

77
```sh
88
$ python -m venv venv
99
$ source venv/bin/activate
10-
(venv) $ python -m pip install chatterbot pytz
10+
(venv) $ python -m pip install -r requirements.txt
1111
```
1212

13-
Then you can train and start the interactive CLI bot by running `bot.py`:
13+
Then you can train and start the interactive command-line interface chatbot by running `bot.py`:
1414

1515
```sh
1616
(venv) $ python bot.py
@@ -37,4 +37,4 @@ After training, you'll see an interactive prompt that you can chat with:
3737
🪴 Ah, gotcha!
3838
```
3939

40-
The bot will learn from the replies you give and improve its accuracy. You can quite the interactive prompt by typing any of the `exit_conditions` defined in `bot.py`.
40+
The bot will learn from the replies you give and improve its accuracy. You can quit the interactive prompt by typing any of the `exit_conditions` defined in `bot.py`.

chatterbot/chat.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
9/15/22, 15:30 - Martin: I'm sure they chat with the other plants if they can
9494
9/15/22, 15:31 - Philipp: Yeah, I heard that trees communicate in the woods with their roots
9595
9/15/22, 15:31 - Philipp: But thinking about that makes me feel bad that my plants are potted 😬
96-
9/15/22, 15:31 - Martin: ://
96+
9/15/22, 15:31 - Martin: 😢
9797
9/15/22, 15:32 - Martin: Yeah...
9898
9/15/22, 15:32 - Martin: The trees use mushrooms mycelium in the ground that's hooked to their roots to chat
9999
9/15/22, 15:32 - Martin: Pretty cool 😎

chatterbot/cleaner.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def clean_corpus(chat_export_file):
55
"""Prepare a WhatsApp chat export for training with chatterbot."""
66
message_corpus = remove_chat_metadata(chat_export_file)
7-
cleaned_corpus = remove_whatsapp_boilerplate(message_corpus)
7+
cleaned_corpus = remove_non_message_text(message_corpus)
88
return cleaned_corpus
99

1010

@@ -25,9 +25,9 @@ def remove_chat_metadata(chat_export_file):
2525
Returns:
2626
tuple: The text of each message in the conversation
2727
"""
28-
date_time = r"(\d+\/\d+\/\d+,\s\d+:\d+)" # "8/26/22, 17:47"
28+
date_time = r"(\d+\/\d+\/\d+,\s\d+:\d+)" # e.g. "8/26/22, 17:47"
2929
dash_whitespace = r"\s-\s" # " - "
30-
username = r"([\w\s]+)" # "Jane Doe"
30+
username = r"([\w\s]+)" # e.g. "Jane Doe"
3131
metadata_end = r":\s" # ": "
3232
pattern = date_time + dash_whitespace + username + metadata_end
3333

@@ -37,20 +37,21 @@ def remove_chat_metadata(chat_export_file):
3737
return tuple(cleaned_corpus.split("\n"))
3838

3939

40-
def remove_whatsapp_boilerplate(message_collection):
40+
def remove_non_message_text(export_text_lines):
4141
"""Remove conversation-irrelevant text from chat export.
4242
43-
WhatsApp chat exports come with a boilerplate intro line.
43+
WhatsApp chat exports come with a standardized intro line,
44+
and an empty line at the end of the file.
4445
Text exports also replace media messages with text that isn't
45-
relevant for the conversation. This function removes both.
46+
relevant for the conversation. This function removes all that.
4647
4748
Args:
48-
message_collection (tuple): Message texts
49+
export_text_lines (tuple): All lines from the export file
4950
5051
Returns:
51-
tuple: Messages without WhatsApp boilerplate
52+
tuple: Messages that are a relevant part of the conversation
5253
"""
53-
messages = message_collection[1:]
54+
messages = export_text_lines[1:-1]
5455

55-
boilerplate = ("<Media omitted>",)
56-
return tuple((msg for msg in messages if msg not in boilerplate))
56+
filter_out_messages = ("<Media omitted>",)
57+
return tuple((msg for msg in messages if msg not in filter_out_messages))

0 commit comments

Comments
 (0)