I want to convert horizontal text to vertical text and post it on Twitter etc.

Almost all SNS such as Twitter are written horizontally. Let's write a Python script that converts this to vertical writing while also studying Python programming.

Tweet

I would like to write the following tweets vertically.

tweet = '''\
Normally Tweet is written horizontally,
I want to write this vertically.
That would make it harder to find, right?
Why don't you try it with Python?
'''
tweet

'Usually Tweet is written horizontally, but \ nI want to write it vertically. \ n Then it will be difficult to search, right? \ nWhy don't you try it with Python? \ n'

Decompose each character into one character

You can break it down into characters one by one by doing the following.

for line in tweet.split("\n"):
    print([line[i] for i in range(len(line))])

['Public','Communication',' is','','T','w','e','e','t','',' is','horizontal','book' ,'Ki',' Na',' N',' Da',' Ke',' Do',' Sa',','] ['Ko',' Re',' to',' Vertical',' Book',' Ki',' to',' Shi',' Ta','I','Wa',' Ke',' Yo','. '] ['So',' U',' Su',' Ru',' and',',',' Inspection',' Search',' Sa',' Re',' Ni',' Ku',' Ku',' na',' ru','n','ji','ya','ne','? '] ['I','',',' Chi',' Yo','',' P',' y',' t',' h',' o',' n',' at',' ',' tsu',' te',' mi',' na',' i',' or','? '] []

However, only the last line will be blank, so let's exclude it.

for line in tweet.split("\n"):
    if len(line) > 0:
        print([line[i] for i in range(len(line))])

['Public','Communication',' is','','T','w','e','e','t','',' is','horizontal','book' ,'Ki',' Na',' N',' Da',' Ke',' Do',' Sa',','] ['Ko',' Re',' to',' Vertical',' Book',' Ki',' to',' Shi',' Ta','I','Wa',' Ke',' Yo','. '] ['So',' U',' Su',' Ru',' and',',',' Inspection',' Search',' Sa',' Re',' Ni',' Ku',' Ku',' na',' ru','n','ji','ya','ne','? '] ['I','',',' Chi',' Yo','',' P',' y',' t',' h',' o',' n',' at',' ',' tsu',' te',' mi',' na',' i',' or','? ']

Express the above process smartly using list comprehension notation and put it in a variable called data.

data = [[line[i] for i in range(len(line))] for line in tweet.split("\n") if len(line) > 0]
print(data)

[['Public','Communication',' is','',' T','w','e','e','t','',' is','horizontal',' book ',' Ki',' Na',' N',' Da',' Ke',' Do',' Sa',','], ['Ko',' Re',' to',' Vertical ',' calligraphy',' ki',' ni','shi','ta','i','wa','ke','yo','. '], ['So',' U',' Su',' Ru',' and',',',' Inspection',' Search',' Sa',' Re',' Ni',' Ku ',' Ku',' Na',' Ru',' N',' Ji',' Ya',' Ne','? '], ['I','',' Chi',' Yo','',' P',' y',' t',' h',' o',' n','de' ,'Ya','tsu','te','mi','na','i','ka','? ']]

Transpose with numpy

I want to transpose the matrix, so I hope I can use numpy.

import numpy as np
np.array(data)

array ([list (['normal','communication',' is','','T','w','e','e','t','',' is',' horizontal ',' Book',' Ki',' Na',' N',' Da',' Ke',' Do',' Sa',',']), list (['ko','re','to','vertical','book','ki',' to','shi','ta','i','wa','ke' ,'Yo','.']), list (['so','u','su','ru',' and',',','inspection','search','sa','re',' ni',' ku' ,'Ku','na','ru','n','ji','ya','ne','?']), list (['i','tsu','chi','yo','',' P','y','t','h','o','n','de', 'Ya','tsu','te','mi','na','i','ka','?'])], dtype=object)

As mentioned above, it becomes an array of lists and it does not work.

Therefore, measure the number of rows and columns, and prepare an array corresponding to that size.

max_length = 0 #Count the number of characters in the longest sentence
for d in data:
    if max_length < len(d):
        max_length = len(d)

From the above, we can see that tweet can be represented by an array oflen (data)rows and max_length columns. So let's prepare a numpy array of the same size. Fill it with whitespace characters in advance.

import numpy as np
np.full((len(data), max_length), " ")
array([['\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000'],
       ['\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000'],
       ['\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000'],
       ['\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000',
        '\u3000', '\u3000', '\u3000']], dtype='<U1')

After that, copy the characters of data to the numpy array character by character.

However, at this time, make sure that the order of the sentences is reversed.

data2 = np.full((len(data), max_length), " ")
for i in range(len(data)):
    for j in range(max_length):
        if j < len(data[i]):
            data2[len(data) - i - 1][j] = data[i][j]
data2

array ([['i','tsu','chi','yo','',' P','y','t','h','o','n','de' ,'Ya', 'T','T','Mi','Na','I','Ka','? ',' \ u3000'], ['So',' U',' Su',' Ru',' and',',',' Inspection',' Search',' Sa',' Re',' Ni',' Ku',' Ku', 'Na',' Ru',' N','Ji',' Ya','Ne','? ',' \ u3000'], ['Ko',' Re',' to',' Vertical',' Book',' Ki',' to',' Shi',' Ta','I','Wa',' Ke',' Yo', '。', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000', '\u3000'], ['Public','Communication',' is','','T','w','e','e','t','',' is','horizontal','book' , 'Ki',' Na',' N',' Da',' Ke',' Do',' Sa',',']], dtype ='<U1')

If you can do so far, you can write vertically using the transpose of the matrix.

data2.T #Transpose of matrix

array ([['i','so','ko','normal'], ['T',' U',' Re','Tsu'], ['Chi',' Su','O',' is'], ['Yo',' Ru','Vertical',''], ['',' And',' Book',' T'], ['P',',',' ki','w'], ['y','inspection','to','e'], ['t','search','shi','e'], ['h','sa','ta','t'], ['o','re','i',''], ['n','to','wa','wa'], ['De',' Ku',' Ke','Horizontal'], ['Ya',' Ku',' Yo',' Book'], ['T','Na','. ',' Ki'], ['Te',' Ru','\ u3000','Na'], ['Mi','N','\ u3000','N'], ['Na','Ji','\ u3000','Da'], ['I','ya','\ u3000','ke'], ['Ka',' Ne','\ u3000','Do'], ['? ','? ',' \ u3000','sa'], ['\u3000', '\u3000', '\u3000', '、']], dtype='<U1')

Output transposed matrix as a string

delimiter = " "
for line in data2.T:
    print(delimiter.join(line))

Isoko Fu Tsuretsu Chisu Vertical And book T P, ki w y to check e t search e h sata t o Rei n Next to the big Yakuyo book What? Ki Teruna Minn It's the same Yake Kane ?? ?? Sa       、

This can be rewritten smartly using list comprehension notation.

delimiter = " "
print("\n".join([delimiter.join(line) for line in data2.T]))

Isoko Fu Tsuretsu Chisu Vertical And book T P, ki w y to check e t search e h sata t o Rei n Next to the big Yakuyo book What? Ki Teruna Minn It's the same Yake Kane ?? ?? Sa       、

Summary

I tried the above operation as a function called yoko2tate.

def yoko2tate(tweet, delimiter = " "):
    data = [[line[i] for i in range(len(line))] for line in tweet.split("\n") if len(line) > 0]
    max_length = 0
    for d in data:
        if max_length < len(d):
            max_length = len(d)
    data2 = np.full((len(data), max_length), " ")
    for i in range(len(data)):
        for j in range(max_length):
            if j < len(data[i]):
                data2[len(data) - i - 1][j] = data[i][j]
    return "\n".join([delimiter.join(line) for line in data2.T])

The usage example looks like this.

tweet = '''\
Ah...Ahりのまま 今 起こったことを話すぜ!

"When I entered a tweet, it became vertical writing."

Nana...何を言っているか分からねーと思うが、おれも何をされたのか分からNanaかった...
'''

print(yoko2tate(tweet))

"Ah" .Tsu. . I . .-. What To Of the word Power Teshima Was Rura now Or Minute vertical rise Calligraphy Raki Nita -Nako Toto Thought U "story Is , Ze O! Re Also what To Sa Re Ta of Or Minutes Or Et al. Nana Or Tsu Ta .     .     .

Recommended Posts

I want to convert horizontal text to vertical text and post it on Twitter etc.
I want to tweet on Twitter with Python, but I'm addicted to it
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
I want to create a pipfile and reflect it in docker
I want to create a histogram and overlay the normal distribution curve on it. matplotlib edition
I made a POST script to create an issue on Github and register it in the Project
I want to use Linux on mac
I want to format and check Python code to my liking on VS Code
I want to develop Android apps on Android
I want to pass an argument to a python function and execute it from PHP on a web server
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to write an element to a file with numpy and check it.
I made an image classification model and tried to move it on mobile
I want to do pyenv + pipenv on Windows
I want to log file I / O on Linux
I want to replace the variables in the python template file and mass-produce it in another file.
I want to convert an image to WebP with lollipop
I want to develop an Android application on Android (debugging)
I want to find a popular package on PyPi
Post to your account using the API on Twitter
I want to AWS Lambda with Python on Mac!
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I want to restart CentOS 8 on time every day.
I tried to install scrapy on Anaconda and couldn't
I want to do Wake On LAN fully automatically
POST the image selected on the website with multipart / form-data and save it to Amazon S3! !!
I want to copy an English paper from pdf and put it in Google Translate
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
Use pyOCR to convert the description on the card into text
I want to disable interrupts on Raspberry Pi (≒ DI / EI)
I want to announce my graduation thesis on IPython Notebook
How to convert Youtube to mp3 and download it super-safely [Python]
I want to convert an ISO-8601 character string to Japan time
I want to know the features of Python and pip
I want to count unique values in arrays and tuples
I want to map the EDINET code and securities number
I want Sphinx to be convenient and used by everyone
I want to know the legend of the IT technology world
I want to use VS Code and Spyder without anaconda! !! !!
Script to convert between Xcode language files and tab-delimited text
[For black screen phobia] I want to know about Linux and shells, so I illustrated it loosely.
How to check and change Linux permissions (permissions) (chmod) (I want to be saved from Permiss on denied)