Neculai Fântânaru

Everything Depends On The Leader

Python: Split the text by a certain number of characters in an html paragraph

On May 02, 2022
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/8x0M1vi0

Install Python.

I have this paragraph which contains 490 characters (in the <p class = plain_text> </p> tag)

<p class=text_obisnuit>Mult mai mult decat un deliciu culinar, mai mult decat 
un aspect plin de delicatete, trebuie sa tin cont de simtirea pe care o degaja 
gustul dulce-acrisor al gumelor Fini, incat sa imbunatatesc experienta interioara
a ceea ce sunt eu, prin alti ochi, prin cunoasterea senzoriala a lucrurilor deja 
traite, infaptuite. Si numai din placerea de a explora vasta lume senzoriala a 
gustului, a aromelor si a perspectivelor vizuale, mi-am permis luxul de a-mi 
gasi un refugiu in domeniul artistic.</p>   

Will become:

<p class=text_obisnuit>Mult mai mult decat un deliciu culinar, mai mult decat 
un aspect plin de delicatete, trebuie sa tin cont de simtirea pe care o degaja 
gustul dulce-acrisor al gumelor Fini, incat sa imbunatatesc experienta interioara
a ceea ce sunt eu, prin alti ochi, prin cunoasterea senzoriala a lucrurilor deja 
traite, infaptuite.</p>  

<p class=text_obisnuit>Si numai din placerea de a explora vasta lume senzoriala a 
gustului, a aromelor si a perspectivelor vizuale, mi-am permis luxul de a-mi 
gasi un refugiu in domeniul artistic.</p>   

Python: (The code below will find each paragraph longer than 250 characters, and will divide it (approximately) into other paragraphs with the same size, up to 250 characters.

#-------------------------------------------------------------------------------
# Author:      Neculai Fantanaru
#
# Created:     02/05/2022
#-------------------------------------------------------------------------------

import requests
import re
import os
import nltk
from nltk import tokenize

cale_folder_html = r"e:Test"
extension_file = ".html"

def read_text_from_file(file_path):
    """
    Aceasta functie returneaza continutul unui fisier.
    file_path: calea catre fisierul din care vrei sa citesti
    """
    with open(file_path, encoding='utf8') as f:
        text = f.read()
        return text


def write_to_file(text, file_path):
    """
    Aceasta functie scrie un text intr-un fisier.
    text: textul pe care vrei sa il scrii
    file_path: calea catre fisierul in care vrei sa scrii
    """
    with open(file_path, 'wb') as f:
        f.write(text.encode('utf8', 'ignore'))


print('Going through folder')
amount = 0

for filename in os.listdir(cale_folder_html):
        if filename == 'y_key_e479323ce281e459.html' or filename == 'directory.html':
            continue
        if filename.endswith(extension_file):
            cale_fisier_html = cale_folder_html + "\\" + filename
            html_text = read_text_from_file(cale_fisier_html)
            articol_pattern = re.compile('<!-- ARTICOL START -->([\s\S]*?)<!-- ARTICOL FINAL -->[\s\S]*?')
            articol_text = re.findall(articol_pattern, html_text)

            if len(articol_text) > 0:
                articol_text = articol_text[0]
                p_pattern = re.compile('<p class="text_obisnuit">(.*?)</p>')
                paragrafe = re.findall(p_pattern, articol_text)

                # aici impartim fiecare paragraf in paragrafe mai mici daca depasesc o anumita limita de caractere
                paragrafe_split = list()
                pattern_paragraf_nou = '<p class="text_obisnuit">{}</p>'

                limita_caractere =  250
                for p in paragrafe:
                    if (len(p) > 250 and len(p) < 300) or (len(p) < 200):
                        paragrafe_split.append(pattern_paragraf_nou.format(p))
                    else:
                        propozitii = tokenize.sent_tokenize(p)
                        propozitii = [prop.strip().capitalize() for prop in propozitii]
                        propozitii = [prop[:-1].strip() + prop[-1] for prop in propozitii]

                        paragraf = ''
                        for propozitie in propozitii:
                            if len(paragraf) + len(propozitie) < limita_caractere:
                                if paragraf == '':
                                    paragraf = paragraf + propozitie
                                else:
                                    paragraf = paragraf + ' ' + propozitie
                            else:
                                paragrafe_split.append(pattern_paragraf_nou.format(paragraf))
                                paragraf = ''

                        if (len(paragraf) < limita_caractere):
                            paragrafe_split.append(pattern_paragraf_nou.format(paragraf))

                if len(paragrafe) == len(paragrafe_split):
                    continue
                else:
                    # construim textul dintre ARTICOL START/FINAL folosind paragrafele noi
                    articol_start_final = '<!-- ARTICOL START -->\n{}\n<!-- ARTICOL FINAL -->'
                    text = '\n'.join(paragrafe_split)

                    # inlocuim ce era intre ARTICOL START/FINAL cu noul text
                    html_text = re.sub(r'<!-- ARTICOL START -->[\s\S]*?<!-- ARTICOL FINAL -->', articol_start_final.format(text), html_text)
                    write_to_file(html_text, cale_fisier_html)
                    print("Am modificat: {}".format(filename))
                    amount += 1

print("Am modificat {} fisiere.".format(amount))

That's all folks.

Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.