Created
May 25, 2023 13:01
-
-
Save williamjeong2/016648668f2b8526b6c03e078de94b16 to your computer and use it in GitHub Desktop.
wrap_text_korean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import textwrap | |
import re | |
def wrap_text_korean(text, width): | |
wrapped_lines = [] | |
for line in text.splitlines(): | |
words = re.findall(r'\S+|\s+', line) | |
wrapped = '' | |
current_width = 0 | |
for word in words: | |
word_width = len(word) | |
if current_width + word_width <= width: | |
wrapped += word | |
current_width += word_width | |
else: | |
wrapped += '\n' + word | |
current_width = word_width | |
wrapped_lines.append(wrapped.strip()) | |
return '\n'.join(wrapped_lines) | |
# 테스트를 위해 예시 텍스트를 사용합니다 | |
example_text = "한국어 텍스트 래핑 테스트를 위한 예시 문장입니다. 이 문장은 주어진 너비에 맞게 줄 바꿈됩니다." | |
# 함수를 호출하여 텍스트를 래핑합니다 | |
wrapped_text = wrap_text_korean(example_text, width=) | |
# 결과를 출력합니다 | |
print(wrapped_text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment