Util¶
Utility functions to format and marshal data.
-
lantern.util.
combine_columns
(columns)¶ Combine
columns
into a single string.Example
>>> combine_columns(['eape', 'xml']) 'example'
Parameters: columns (iterable) – ordered columns to combine Returns: String of combined columns
-
lantern.util.
group
(text, size)¶ Group
text
into blocks ofsize
.Example
>>> group("test", 2) ['te', 'st']
Parameters: - text (str) – text to separate
- size (int) – size of groups to split the text into
Returns: List of n-sized groups of text
Raises: ValueError – If n is non positive
-
lantern.util.
iterate_ngrams
(text, n)¶ Generator to yield ngrams in
text
.Example
>>> for ngram in iterate_ngrams("example", 4): ... print(ngram) exam xamp ampl mple
Parameters: - text (str) – text to iterate over
- n (int) – size of window for iteration
Returns: Generator expression to yield the next ngram in the text
Raises: ValueError – If n is non positive
-
lantern.util.
remove
(text, exclude)¶ Remove
exclude
symbols fromtext
.Example
>>> remove("example text", string.whitespace) 'exampletext'
Parameters: - text (str) – The text to modify
- exclude (iterable) – The symbols to exclude
Returns: text
withexclude
symbols removed
-
lantern.util.
split_columns
(text, n_columns)¶ Split
text
inton_columns
many columns.Example
>>> split_columns("example", 2) ['eape', 'xml']
Parameters: - text (str) – The text to split
- n_columns (int) – The number of columns to create
Returns: List of columns
Raises: ValueError – If n_cols is <= 0 or >= len(text)