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'
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','? ']]
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 、
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