Joomla 4. Компонент K2 и форк K2ForJ4 (18 янв 2024)

Если вас, как и меня, достало выслушивать (вычитывать) бесконечные обещания разработчика K2 опубликовать версию компонента K2 под Joomla 4 (без чего невозможно реализовать апгрейд from Joomla 3 to Joomla 4) - воспользуйтесь форком K2ForJ4. Который в данный момент установлен и без каких-либо проблем работает на этом веб-сайте.

Как скриптом разбить текст по предложениям и количеству знаков

Больше
2 года 7 мес. назад #1 от Aleksej
Не думаю, что в разделе форума под названием Python будет много сообщений (хотя чем черт не шутит). Не кодю я на пайтоне, пока что, во всяком случае. А дальше посмотрим. Но вот прикольный скрипт от Hubbitus-a, разбивающий длинный текст строго по предложениям, сообразуясь с указываемым количеством знаков в каждом; не могу не опубликовать, мало ли кому понадобится. 
Code:
import re from functools import reduce text = '''Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. ''' print(len(text)) # By https://stackoverflow.com/questions/25735644/python-regex-for-splitting-text-into-sentences-sentence-tokenizing/25736082#25736082 sentences = re.split('(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=[.?])\s', text) char_limit = 200 def groupByMaxSymbolsLen(result, item):  # print(f"result=[{result}]; item=[{item.strip()}]")  if (len(result[-1]) + len(item) + 1) <= char_limit:   result[-1] = (result[-1] + ' ' + item).strip()  else:   result.append(item)  return result r = reduce(groupByMaxSymbolsLen, sentences, ['']) print(r) print(len(r))

Пожалуйста Войти или Регистрация, чтобы присоединиться к беседе.

Dev banner 3
Работает на Kunena форум