Concordance¶
A term's dispersion across the text, with keyword-in-context.
lexograph.presets.concordance.concordance(text, terms, *, ignore_case=True, normalize=False, figsize=None, background='white')
¶
Draw a lexical-dispersion plot of where each term falls in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
terms
|
Sequence[str]
|
The terms to plot, one row each (top to bottom in this order). |
required |
ignore_case
|
bool
|
Match terms case-insensitively. |
True
|
normalize
|
bool
|
If |
False
|
figsize
|
tuple[float, float] | None
|
Figure size in inches. Defaults to a height that grows with the number of terms. |
None
|
background
|
str
|
Figure and axes background colour. |
'white'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
Figure
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Contract
- Returns a Figure with exactly one axes.
- There is exactly one y-row per term, in the given order.
Examples:
>>> from lexograph import load_demo_text
>>> fig = concordance(load_demo_text(), ["Bennet", "Bingley", "wife"])
>>> type(fig).__name__
'Figure'
>>> len(fig.axes[0].get_yticks())
3
Source code in lexograph/presets/concordance.py
lexograph.layout.dispersion.term_offsets(text, terms, *, ignore_case=True)
¶
Return the token offsets at which each term occurs, in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
terms
|
Sequence[str]
|
The terms to locate (each matched as a whole token). |
required |
ignore_case
|
bool
|
Match case-insensitively. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, FloatArray]
|
A mapping from each input term to a float array of the token indices at |
dict[str, FloatArray]
|
which it occurs (empty if it never does). The mapping preserves the |
dict[str, FloatArray]
|
order of |
Examples:
>>> offsets = term_offsets("the cat and the dog and the cat", ["cat", "dog"])
>>> offsets["cat"].tolist()
[1.0, 7.0]
>>> offsets["dog"].tolist()
[4.0]
Source code in lexograph/layout/dispersion.py
lexograph.layout.dispersion.kwic(text, term, *, width=5, ignore_case=True)
¶
Return keyword-in-context lines for term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The source text. |
required |
term
|
str
|
The term to find (matched as a whole token). |
required |
width
|
int
|
How many context tokens to keep on each side. |
5
|
ignore_case
|
bool
|
Match case-insensitively. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
One |
list[KWIC]
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> lines = kwic("the small cat sat on the cat mat", "cat", width=2)
>>> len(lines)
2
>>> lines[0].left, lines[0].keyword, lines[0].right
('the small', 'cat', 'sat on')
Source code in lexograph/layout/dispersion.py
lexograph.layout.dispersion.KWIC
dataclass
¶
One keyword-in-context line.
Attributes:
| Name | Type | Description |
|---|---|---|
offset |
int
|
The token index of the keyword. |
left |
str
|
The context tokens to the left, space-joined. |
keyword |
str
|
The matched token, as it appeared in the text. |
right |
str
|
The context tokens to the right, space-joined. |