Skip to content

Text Analysis

Text analysis helpers.

Modules

average_word_length

Average Word Length

Calculate average word length in text.

Functions

average_word_length(text: str) -> float

Calculate the average word length in text.

Parameters:

Name Type Description Default
text str

Input text string

required

Returns:

Type Description
float

Average length of words

Raises:

Type Description
ValueError

If text contains no words

Example

average_word_length("hello world") 5.0

char_frequency

Character Frequency Analysis

Count character frequency in text.

Functions

char_frequency(text: str) -> dict[str, int]

Count the frequency of each character in text.

Parameters:

Name Type Description Default
text str

Input text string

required

Returns:

Type Description
dict[str, int]

Dictionary mapping characters to their frequency count

Example

char_frequency("hello")

is_palindrome

Palindrome Checker

Check if text is a palindrome.

Functions

is_palindrome(text: str, ignore_case: bool = True, ignore_spaces: bool = True) -> bool

Check if text is a palindrome.

Parameters:

Name Type Description Default
text str

Input text string

required
ignore_case bool

Whether to ignore case differences

True
ignore_spaces bool

Whether to ignore spaces and non-alphanumeric

True

Returns:

Type Description
bool

True if text is a palindrome, False otherwise

Example

is_palindrome("A man a plan a canal Panama") True

longest_word

Longest Word Finder

Find the longest word in text.

Functions

longest_word(text: str) -> str

Find the longest word in text.

Parameters:

Name Type Description Default
text str

Input text string

required

Returns:

Type Description
str

The longest word

Raises:

Type Description
ValueError

If text contains no words

Example

longest_word("hello world") 'hello'

shortest_word

Shortest Word Finder

Find the shortest word in text.

Functions

shortest_word(text: str) -> str

Find the shortest word in text.

Parameters:

Name Type Description Default
text str

Input text string

required

Returns:

Type Description
str

The shortest word

Raises:

Type Description
ValueError

If text contains no words

Example

shortest_word("hello world") 'hello'

word_count

Word Counting

Count words in text.

Functions

word_count(text: str) -> int

Count the number of words in text.

Parameters:

Name Type Description Default
text str

Input text string

required

Returns:

Type Description
int

Number of words

Example

word_count("hello world") 2

options: show_root_heading: true show_source: false heading_level: 2