← Back to DAGDA | Support | Source: https://bugs.python.org/issue26214

➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: textwrap should minimize number of breaks in extra long words
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Tuomas Salo, andrei.avk, iritkatriel, serhiy.storchaka, steven.daprano, xtreak
Priority: normal Keywords:

Created on 2016-01-27 08:08 by Tuomas Salo, last changed 2022-04-11 14:58 by admin.

Messages (6)
msg258999 - (view) Author: Tuomas Salo (Tuomas Salo) Date: 2016-01-27 08:08
This code:

    import textwrap
    textwrap.wrap("123 123 1234567", width=5)

currently* produces this output:

    ['123', '123 1', '23456', '7']

I would expect the textwrap module to only break words when absolutely necessary. That is, I would have expected it to produce one break less:

    ['123', '123', '12345', '67']

This is of course a matter of taste - the current implementation produces more efficiently filled lines.

(* I only have access to Python 2.7 and 3.4)
msg376368 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-04 14:21
You can do this already with the break_long_words arg of testwrap:

>>> import itertools, textwrap
>>> wr = textwrap.wrap
>>> list(itertools.chain(*(wr(x, 5) for x in wr("123 123 1234567", width=5, break_long_words=False))))
['123', '123', '12345', '67']
msg376387 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-09-04 17:11
The code with nested wraps is awesome. But it does not work well.

>>> list(itertools.chain(*(wr(x, 5) for x in wr("123 123 1234567 12", width=5, break_long_words=False))))
['123', '123', '12345', '67', '12']

It is expected that '67' and '12' should be in the same line: '67 12'.
msg376388 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-04 17:23
One more wrap: 

>>> wr(' '.join(itertools.chain(*(wr(x, 5) for x in wr("123 123 1234567 12", width=5, break_long_words=False)))), 5)
['123', '123', '12345', '67 12']
msg376395 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-04 19:09
To clarify, this solution is a linear-time greedy one, with three passes:
- the first pass puts each long word on its own line. 
- the second pass chops them up into words of at most width characters.
- the third pass wraps them, when there are no more long words.

This minimizes the number of breaks within words. It doesn't minimize the number of output lines (you'd need a dynamic programming programming algo for that - O(n^2)). So for this input:

wr("123 12 123456 1234", 5)

you will get 
['123', '12', '12345', '6', '1234']

where you may (or may not) have preferred:

['123', '12 1', '23456', '1234']
msg409241 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-27 18:51
It may be worth fixing wrap() to do the nicer style of wrapping for long words. If we decide to do that, it should be done via a new parameter because the same logic (TextWrapper class) is used for `shorten` and in that case it may be preferable to have the chunk of longer word rather than cutting it out entirely.
History
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70402
2021-12-27 18:51:25andrei.avksetnosy: + andrei.avk

messages: + msg409241
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.6
2020-09-19 18:57:44georg.brandlsetnosy: - georg.brandl
2020-09-19 18:53:48iritkatrielsetcomponents: + Library (Lib)
2020-09-04 19:09:52iritkatrielsetmessages: + msg376395
2020-09-04 17:23:43iritkatrielsetmessages: + msg376388
2020-09-04 17:11:56serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg376387
2020-09-04 14:21:48iritkatrielsetnosy: + iritkatriel
messages: + msg376368
2018-09-21 12:23:36xtreaksetnosy: + xtreak
2016-02-11 14:01:45steven.dapranosetnosy: + steven.daprano
2016-01-30 02:02:54terry.reedysettype: behavior -> enhancement
versions: - Python 2.7
2016-01-27 08:10:48SilentGhostsetnosy: + georg.brandl

versions: + Python 3.6, - Python 3.4
2016-01-27 08:09:25Tuomas Salosettitle: textwrap should minimize breaks -> textwrap should minimize number of breaks in extra long words
2016-01-27 08:08:41Tuomas Salocreate