Break words based on an offset - Recursion

Shared by: devcanvas

python

1
def break_sentence(text):    
2
    after = 40
3
    offset = 5
4
   
5
    # sample = text[:after]
6
    if len(text) <= after:
7
        return text
8
    if len(text)-after < 15:
9
        offset = 10
10
    dif = after-offset
11
    if text[dif] == ' ':
12
        offset += 1
13
        # just a random comment
14
    
15
    words = text[dif:after+offset]
16
    new_break_point = words.find(' ')
17
    if new_break_point != -1:
18
        after = dif+new_break_point
19

20
    firstpart = text[:after]
21
    lastpart = text[after:]
22
    lastpart = lastpart.strip()
23

24
    if len(lastpart) == 0:
25
        return firstpart+lastpart
26
    if len(lastpart) <= after:
27
        return firstpart+"\n"+lastpart
28

29
    return firstpart+'\n'+break_sentence(lastpart)
30

31
    if len(lastpart) == 0:
32
        return firstpart+lastpart
33
    if len(lastpart) <= after:
34
        return firstpart+"\n"+lastpart
35

36
    return firstpart+'\n'+break_sentence(lastpart)
37

38
    if len(lastpart) == 0:
39
        return firstpart+lastpart
40
    if len(lastpart) <= after:
41
        return firstpart+"\n"+lastpart
42

43
    return firstpart+'\n'+break_sentence(lastpart)
44

45
    if len(lastpart) == 0:
46
        return firstpart+lastpart
47
    if len(lastpart) <= after:
48
        return firstpart+"\n"+lastpart
49

50
    return firstpart+'\n'+break_sentence(lastpart)
51

52