Skip to content

Commit

Permalink
feat: count characters from text
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed May 15, 2024
1 parent 51cc417 commit 5ed1da2
Showing 1 changed file with 12 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tags": []
},
"source": [
"# Python - Count letters/words/phrases from text\n",
"# Python - Count characters from text\n",
"\n",
"<a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=Bubble+-+Send+data:+Error+short+description\">Bug report</a>"
]
Expand Down Expand Up @@ -53,7 +53,7 @@
"id": "31ea7cdb-e10d-43fc-b026-f69249a59736",
"metadata": {},
"source": [
"**Description:** Counts the appearance of a chosen word/letter or phrase in the text."
"**Description:** This notebook counts the appearance of a chosen word/letter or phrase in the text."
]
},
{
Expand Down Expand Up @@ -86,16 +86,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "potential-surfing",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-13T03:03:31.922485Z",
"iopub.status.busy": "2024-05-13T03:03:31.922115Z",
"iopub.status.idle": "2024-05-13T03:03:31.926216Z",
"shell.execute_reply": "2024-05-13T03:03:31.925609Z",
"shell.execute_reply.started": "2024-05-13T03:03:31.922390Z"
},
"tags": []
},
"outputs": [],
Expand All @@ -111,27 +104,20 @@
"### Setup variables\n",
"\n",
"- `text`: Inputed sentence\n",
"- `chosenText`: Chosen letter/word/phrase to count for"
"- `characters`: Chosen letter/word/phrase to count for"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "continuous-melbourne",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-13T03:03:32.217178Z",
"iopub.status.busy": "2024-05-13T03:03:32.216955Z",
"iopub.status.idle": "2024-05-13T03:03:32.229898Z",
"shell.execute_reply": "2024-05-13T03:03:32.228086Z",
"shell.execute_reply.started": "2024-05-13T03:03:32.217155Z"
},
"tags": []
},
"outputs": [],
"source": [
"text = \"This is an example text. This text contains the phrase 'example text' multiple times.\"\n",
"chosenText = \"example text\""
"characters = \"e\""
]
},
{
Expand All @@ -152,16 +138,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "crude-louisville",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-13T03:03:32.783218Z",
"iopub.status.busy": "2024-05-13T03:03:32.782984Z",
"iopub.status.idle": "2024-05-13T03:03:32.786859Z",
"shell.execute_reply": "2024-05-13T03:03:32.786134Z",
"shell.execute_reply.started": "2024-05-13T03:03:32.783194Z"
},
"papermill": {},
"tags": []
},
Expand All @@ -171,10 +150,9 @@
" count = 0\n",
" phrases = text.split(chosenText)\n",
" count = len(phrases) - 1\n",
" \n",
" return count\n",
"\n",
"occurrences = count_phrase_occurrences(text, chosenText)"
"occurrences = count_phrase_occurrences(text, characters)"
]
},
{
Expand All @@ -198,34 +176,19 @@
"id": "890f7c86-b7bb-4f5d-9a1b-e492dd9580fd",
"metadata": {},
"source": [
"### Display Result"
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "9c4e3b7b-6440-4844-8054-265f1aec65eb",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-13T03:03:34.437697Z",
"iopub.status.busy": "2024-05-13T03:03:34.437449Z",
"iopub.status.idle": "2024-05-13T03:03:34.441195Z",
"shell.execute_reply": "2024-05-13T03:03:34.440500Z",
"shell.execute_reply.started": "2024-05-13T03:03:34.437669Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'example text' appears 2 times in the text.\n"
]
}
],
"outputs": [],
"source": [
"print(f\"'{chosenText}' appears {occurrences} times in the text.\")"
"print(f\"'{characters}' appears {occurrences} times in the text.\")"
]
},
{
Expand Down

0 comments on commit 5ed1da2

Please sign in to comment.