Segment¶
Turn raw text into ordered units: characters, tokens, or sentences.
lexograph.segment.units.segment(text, unit='sentences', *, punkt=False)
¶
Segment text into ordered units of the requested kind.
This is the single entry point to the segmentation step of the spine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
unit
|
UnitKind
|
|
'sentences'
|
punkt
|
bool
|
Use NLTK Punkt for sentence splitting (ignored for other kinds). |
False
|
Returns:
| Type | Description |
|---|---|
list[Unit]
|
The ordered list of units. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Contract
- The returned list preserves source order.
"chars"keeps every character;"tokens"and"sentences"drop pure-whitespace units.
Examples:
>>> segment("One. Two.", unit="sentences")
['One.', 'Two.']
>>> segment("One two", unit="tokens")
['One', 'two']
>>> len(segment("abc", unit="chars"))
3
Source code in lexograph/segment/units.py
lexograph.segment.units.sentences(text, *, punkt=False)
¶
Split text into sentences, in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
punkt
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
list[Unit]
|
The ordered list of sentences, each stripped of surrounding whitespace. |
list[Unit]
|
Empty or whitespace-only sentences are dropped. |
Examples:
>>> sentences("Mr. Bennet replied that he had not. He said no more.")
['Mr. Bennet replied that he had not.', 'He said no more.']
Source code in lexograph/segment/units.py
lexograph.segment.units.tokens(text)
¶
Return the word tokens of text in order.
A token is a run of word characters with optional internal apostrophes or hyphens; punctuation and whitespace are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
Returns:
| Type | Description |
|---|---|
list[Unit]
|
The ordered list of word tokens. |
Examples:
Source code in lexograph/segment/units.py
lexograph.segment.units.characters(text)
¶
Return every character of text in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
Returns:
| Type | Description |
|---|---|
list[Unit]
|
One single-character string per character, including whitespace and |
list[Unit]
|
punctuation (the punctuation-spiral preset filters these itself). |
Examples: