nlg_enum #
The nlg_enum method allows to enumerate over a list of items and create a sentence with it.
It automatically format the sentence with bullet-point style when the number of elements is above a provided threshold.
nlg = NlgTools(lang='en')
my_list = ["six apples", "three bananas", "two peaches"]
nlg.nlg_enum(my_list)
# "six apples, three bananas and two peaches"
Parameters #
Parameter | Type | Default Value |
---|---|---|
The list of elements to iterate over | list | |
max_elem (maximum number of elements of the list to display) | int | None (all elements) |
sep (the seperator between each element except last one) | string | “,” |
last_sep (separator before last item) | string | “and” (en) “et” (fr) |
begin_w (will be inserted before enumeration) | string | "" |
end_w (will be inserted after enumeration) | string | "" |
nb_elem_bullet (if the number of displayed elements is above or equals this number, the output will be a bullet-point list) | int | None |
end_of_bullet (will be inserted at the end of each bullet except last) | string | "" |
end_of_last_bullet (will be inserted at the end of last bullet) | string | "" |
capitalize_bullets (firsts letter of each bullets will be capitalized) | boolean | True |
text_if_empty_list (will be displayed if the list is empty) | string | "" |
Usage #
nlg = NlgTools()
my_list = ['apples', 'bananas', 'peaches']
text = nlg.nlg_enum(
my_list,
max_elem=2,
nb_elem_bullet=2,
begin_w='Fruits I like :',
end_w='Delicious, right ?',
end_of_bullet=',',
end_of_last_bullet='.'
)
nlg.write_text(text)
"""
Fruits I like :
- Apples,
- Bananas.
Delicious, right ?
"""
my_list = ['apples', 'bananas']
text = nlg.nlg_enum([TextVar(
fruit,
nlg.nlg_syn('so', ''),
nlg.nlg_syn('succulent', 'tasty'))
) for fruit in my_list],
begin_w='I find', end_w='.'
)
nlg.write_text(text)
"""
One of the following:
I find apples so tasty and bananas succulent.
I find apples tasty and bananas so succulent.
I find apples so succulent and bananas tasty.
I find apples succulent and bananas so tasty.
"""