text_find
text_find
A module that reads a text file to locate the first occurrence of ‘keyword’ in text and returns its character index.
Note: The returned index refers to the text read from the processed file (e.g., lowercased / cleaned by earlier steps), not the raw file text.
Functions
| Name | Description |
|---|---|
| text_find | Finds the first whole-word instance of keyword in a text file. |
text_find
text_find.text_find(input_path, keyword)Finds the first whole-word instance of keyword in a text file.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| input_path | str | Path to the input text file. Must be a string ending with .txt | required |
| keyword | str | The keyword to search for. Must be non-empty string containing only letters and spaces (UTF-8 Unicode letters allowed). | required |
Returns
| Name | Type | Description |
|---|---|---|
| 0-based character index of the first match in the file text, or -1 | if not found. |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If input_path or keyword is not a string. | |
| ValueError | If input_path is not a .txt file, or keyword is invalid. | |
| FileNotFoundError | If the input file does not exist. | |
| OSError | If there is an error reading the file. |
Examples
>>> text_find("example.txt", "hello")
0
# Returns 0 if "hello" appears at start of example>>> text_find("example.txt", "world")
6
# Returns 6 if "world" appears at character index 6>>> text_find("example.txt", "missing")
-1
# Returns -1 if "missing" is not found in example.txt