-1

I need help.

Code:

def split(lst, n):
    n = min(n, len(lst))
    k, m = divmod(len(lst), n)
    for i in range(n):
        yield lst[i*k+min(i, m):(i+1)*k+min(i+1, m)] 

siemak = "I look around at my culture, at the ways that people treat each other and at the ways that people treat the land, and I feel that we live in one of those times when language is closed and guarded by gatekeepers. I read Thoreaus Walden and realize that the language we use to speak of the natural world, and of our relationship with the natural world, has changed very little in 150 years. And if the language we use to speak of the natural world is not innovative and engaging then is it any wonder that few young people get excited about nature? I feel that the time has come for language to shine again, to bloom like a flower and lead the way as we begin to speak confidently about the future we want. But this can only happen when we create new words that will serve as vessels for new ideas and new dreams. Long-stable systems and stale old conventions are already breaking down before our eyes, and in the midst of this teetering balance we have an amazing opportunity to rebuild our culture and our relationship with the natural world through language."

porto = list(split(siemak,20))
print(porto)

Result:

['I look around at my culture, at the ways that people ', 'treat each other and at the ways that people treat th', 'e land, and I feel that we live in one of those times', ' when language is closed and guarded by gatekeepers. ', 'I read Thoreaus Walden and realize that the language ', 'we use to speak of the natural world, and of our rela', 'tionship with the natural world, has changed very lit', 'tle in 150 years. And if the language we use to speak', ' of the natural world is not innovative and engaging ', 'then is it any wonder that few young people get excit', 'ed about nature? I feel that the time has come for la', 'nguage to shine again, to bloom like a flower and lea', 'd the way as we begin to speak confidently about the ', 'future we want. But this can only happen when we crea', 'te new words that will serve as vessels for new ideas', ' and new dreams. Long-stable systems and stale old co', 'nventions are already breaking down before our eyes, ', 'and in the midst of this teetering balance we have an', ' amazing opportunity to rebuild our culture and our r', 'elationship with the natural world through language.']

How to move the last parts of a word to the next line so that they form correct words???

Maybe another solution??? Thanks.

  • Check out [this library](https://pypi.org/project/PyHyphen/) designed for hyphenation. There are multiple algorithms, from simple "never break words" (easy to implement: decrease last index until it corresponds to a whitespace character) to much more complex ones (lib above). – STerliakov May 08 '23 at 11:12
  • What do you want to do with "Incomprehensibilities" (consisting of 21 letters AFAIR) in case "equal lines" turn out to be 15 char long (short sentence)? – STerliakov May 08 '23 at 11:13
  • 1
    What do you mean by "equal lines"? same number of words? same number of characters? or ??? – Nick May 08 '23 at 11:14
  • @Nick Sorry, I already understood that my comment reads wrong, added clarification for what I mean. "20 equal lines" for string of 200 chars would give 10 chars per row, and that word should span 3 lines. – STerliakov May 08 '23 at 11:16
  • Does this answer your question? [chunk string in Python without breaking words](https://stackoverflow.com/questions/32295475/chunk-string-in-python-without-breaking-words) – sbottingota May 08 '23 at 11:26
  • The general idea is to split a long text into an equal number of lines: line1 line2 line3 etc. The above code divides the text into lines, but sometimes it "cuts off" the last word, which makes the text incomprehensible. – AndrewAndrew May 08 '23 at 11:55
  • @sbottingota Thanks, but it's about dividing the text into a given number of lines (the number of characters does not have to be the same in each line. Not necessarily 20 lines, but any given number of lines) - without "cutting" words. – AndrewAndrew May 08 '23 at 12:17

2 Answers2

0

You could just use the textwrap module:

import math
import textwrap
s = "I look around at my culture, at the ways that people treat each other and at the ways that people treat the land, and I feel that we live in one of those times when language is closed and guarded by gatekeepers. I read Thoreaus Walden and realize that the language we use to speak of the natural world, and of our relationship with the natural world, has changed very little in 150 years. And if the language we use to speak of the natural world is not innovative and engaging then is it any wonder that few young people get excited about nature? I feel that the time has come for language to shine again, to bloom like a flower and lead the way as we begin to speak confidently about the future we want. But this can only happen when we create new words that will serve as vessels for new ideas and new dreams. Long-stable systems and stale old conventions are already breaking down before our eyes, and in the midst of this teetering balance we have an amazing opportunity to rebuild our culture and our relationship with the natural world through language."
def split_n_lines(s, n):
    chunk_size = math.ceil(len(s) / n)
    return textwrap.wrap(s, chunk_size)

print(split_n_lines(s, 20))

Output:

['I look around at my culture, at the ways that people treat each other and at the ways that people treat the land, and I feel that we live in one of those times when language is closed and guarded by gatekeepers. I read Thoreaus Walden and realize that the language we use ', 'to speak of the natural world, and of our relationship with the natural world, has changed very little in 150 years. And if the language we use to speak of the natural world is not innovative and engaging then is it any wonder that few young people get excited about nature? I feel ', 'that the time has come for language to shine again, to bloom like a flower and lead the way as we begin to speak confidently about the future we want. But this can only happen when we create new words that will serve as vessels for new ideas and new dreams. Long-stable systems ', 'and stale old conventions are already breaking down before our eyes, and in the midst of this teetering balance we have an amazing opportunity to rebuild our culture and our relationship with the natural world through language. ']

It's a bit imprecise (sometimes not exactly the number of lines you want), but quite an easy solution.

I suppose if you wanted to be exact, you could do a bit of troubleshooting:

def split_n_lines_precise(s, n):
    split_lines = split_n_lines(s, n)
    if len(split_lines) == n:
        return split_lines
    else:
        return split(n_lines(s, n - 1)

but this is a bit sketchy for some strings.

sbottingota
  • 533
  • 1
  • 3
  • 18
  • Thanks. For me, the result is 21. However, the point is to divide flawlessly, exactly into the given number of lines (not necessarily 20, but any given number of lines). – AndrewAndrew May 08 '23 at 12:04
0

What about this? It's not nice or optimized, but tries to keep the lengths of the lines equal (except the last one :/).

siemak = "I look around at my culture, at the ways that people treat each other and at the ways that people treat the land, and I feel that we live in one of those times when language is closed and guarded by gatekeepers. I read Thoreaus Walden and realize that the language we use to speak of the natural world, and of our relationship with the natural world, has changed very little in 150 years. And if the language we use to speak of the natural world is not innovative and engaging then is it any wonder that few young people get excited about nature? I feel that the time has come for language to shine again, to bloom like a flower and lead the way as we begin to speak confidently about the future we want. But this can only happen when we create new words that will serve as vessels for new ideas and new dreams. Long-stable systems and stale old conventions are already breaking down before our eyes, and in the midst of this teetering balance we have an amazing opportunity to rebuild our culture and our relationship with the natural world through language."

def split_n_lines(lst, n):
    slst = lst.split()
    char_line = sum(len(l) for l in slst) / (n + 1)
    c = 0
    sumc = 0
    ret = []
    while len(slst):
        ret.append(slst.pop(0))
        c += len(ret[-1])
        if c >= char_line:
            yield ret
            sumc+= c
            c = 0
            ret = []
    yield ret


for line in split_n_lines(siemak, 20):
    print(line)

Output:

['I', 'look', 'around', 'at', 'my', 'culture,', 'at', 'the', 'ways', 'that', 'people']
['treat', 'each', 'other', 'and', 'at', 'the', 'ways', 'that', 'people', 'treat', 'the']
['land,', 'and', 'I', 'feel', 'that', 'we', 'live', 'in', 'one', 'of', 'those', 'times', 'when']
['language', 'is', 'closed', 'and', 'guarded', 'by', 'gatekeepers.', 'I', 'read']
['Thoreaus', 'Walden', 'and', 'realize', 'that', 'the', 'language', 'we', 'use']
['to', 'speak', 'of', 'the', 'natural', 'world,', 'and', 'of', 'our', 'relationship']
['with', 'the', 'natural', 'world,', 'has', 'changed', 'very', 'little', 'in']
['150', 'years.', 'And', 'if', 'the', 'language', 'we', 'use', 'to', 'speak', 'of', 'the']
['natural', 'world', 'is', 'not', 'innovative', 'and', 'engaging', 'then']
['is', 'it', 'any', 'wonder', 'that', 'few', 'young', 'people', 'get', 'excited', 'about']
['nature?', 'I', 'feel', 'that', 'the', 'time', 'has', 'come', 'for', 'language', 'to']
['shine', 'again,', 'to', 'bloom', 'like', 'a', 'flower', 'and', 'lead', 'the', 'way']
['as', 'we', 'begin', 'to', 'speak', 'confidently', 'about', 'the', 'future', 'we']
['want.', 'But', 'this', 'can', 'only', 'happen', 'when', 'we', 'create', 'new', 'words']
['that', 'will', 'serve', 'as', 'vessels', 'for', 'new', 'ideas', 'and', 'new', 'dreams.']
['Long-stable', 'systems', 'and', 'stale', 'old', 'conventions', 'are']
['already', 'breaking', 'down', 'before', 'our', 'eyes,', 'and', 'in', 'the', 'midst']
['of', 'this', 'teetering', 'balance', 'we', 'have', 'an', 'amazing', 'opportunity']
['to', 'rebuild', 'our', 'culture', 'and', 'our', 'relationship', 'with', 'the']
['natural', 'world', 'through', 'language.']
slapslash
  • 54
  • 5
  • That isn't 20 lines. Also, I think you would want to use `" ".join()` to have a list of strings, not a matrix. – sbottingota May 08 '23 at 12:16
  • You are perfectly right sir. I have to look, what gone wrong. I made the output this way, as it's more readable... so one could easy spot. the aren't 20 lines i.e ;) – slapslash May 08 '23 at 13:30
  • Fixed the code to actually output 20 lines. Although I have to say, that there are cases (25 lines i.e.), where the given number of lines will not match! – slapslash May 08 '23 at 14:13