Thank you for browsing. Nice to meet you, this is Qiita's first post.
I love doing useless things. I'm new to Python, so if you have any suggestions for coding or knowledge, please. This is a poor article, but I hope you read it.
macOS Catalina 10.15.6 MacBook Pro (16-inch, 2019) Python 3.8.6
What are "Logihara" and "Fukihara"? Pros and cons of the newly named harassment
When I was looking at the news site, I saw an article like the one above.
"Sexual harassment", "power harassment", "logihara", etc. are all famous, but recently many other harassments have been increasing. .. .. Of course, enlightenment about harassment is important, but even so, it is difficult to understand if words that are not familiar to us are brought into conversation as harassment. (It's just my impression)
Therefore, the purpose of this article is to generate the harassment name "○○ hara" from the harassment target word.
Example "Power harassment" → "Power" → "Power harassment" "Logical harassment"-> "Logic"-> "Logical harassment"
The general flow of the system is
Japanese-English translation of input sentences ↓ Convert English words to Katakana English ↓ Cut out the first two characters of Katakana English ↓ Output by attaching to "Hara"
It becomes.
Use googletrans for Japanese-English translation and alkana for katakana English translation.
terminal
$ pip3 install googletrans
For googletrans, I referred to this article. [[Python] Countermeasures for "AttributeError:'NoneType' object has no attribute'group'" of googletrans [2020/12/02 postscript]](https://qiita.com/_yushuu/items/83c51e29771530646659 【python】googletransの『AttributeError: 'NoneType' object has no attribute 'group'』対策【2020/12/02追記】)
terminal
$ git clone https://github.com/cod-sushi/alkana.py
$ cd alkana.py
$ python3 -m pip install -U .
For alkana, I referred to the site of the creator outside Qiita. [[python] I made a package "alkana.py" that converts English words to katakana](https://cod-sushi.com/alkana-py/ [python] A package that converts English words to katakana "alkana" I made ".py")
harassment.py
from googletrans import Translator
import alkana
input_str = input('harassment word : ')
tr = Translator(service_urls=['translate.googleapis.com'])
while True:
try:
text = tr.translate(input_str, dest="en").text
break
except Exception as e:
tr = Translator(service_urls=['translate.googleapis.com'])
kana = alkana.get_kana(text)
print('generate =======> ' + kana[:2] + 'Hara')
Enter Japanese for harassment on the 4th line For this reason, I examined the spelling of harassment. It's cooler to output in English. (Impression)
Lines 6 to 13 perform Japanese-English conversion using googletrans.
In the 15th line, alkana is used to convert to Katakana English.
When the above code is executed. .. ..
terminal
harassment word :power
generate =======>Power harassment
It is output properly!
However, there are not only four-letter ones in "OO Hara" but also five-letter ones such as "Genhara" and "Photohara". If you do this as before. .. ..
terminal
harassment word :Photo
generate =======>Fohara
Well, it's a little difficult to post to Qiita.
Therefore, if the second letter of Katakana English contains lowercase letters, I decided to cut out up to the third letter.
harassment.py
lc_list = {'A','I','U','E','Oh','Tsu','YA','Yu','Yo'}
if kana[1] in lc_list :
print('generate =======> ' + kana[:3] + 'Hara')
else :
print('generate =======> ' + kana[:2] + 'Hara')
It lists lowercase katakana and branches depending on whether the second character of kana is in the list.
terminal
harassment word :Photo
generate =======>Photohara
The output was successful.
Now that the system is complete, what about "Fukihara" in the title of the article at the beginning? In the article, "Fukihara" is supposed to be "moody harassment", but "moody" is naturally Japanese, and it is considered inappropriate when applied to rules such as "sexual harassment" and "power harassment". I will.
So, when I applied "moody" to googletrans, it became "Bad mood", and alkana could not convert it to katakana English. With this system, only the words that exist in alkana can be translated into Katakana English. (Although it seems that it supports about 50,000 English words, it's amazing!)
So if you output "mood" instead,
terminal
harassment word :mood
generate =======>Muhara
It's a conversion from the mood. There are some urban legends, but they are. aside from that,
Frustrated → frustlated → frustrated Anger → anger → Anhara Crumpled → crumpled → Kurahara
Something like this was output. What do you use instead of "Fukihara"?
This time, it's easy, but I generated a new harassment name from Japanese with Python3. Personally, I am very happy with the system that is useless.
Finally, I would like to end with the full code. Thank you for your hard work.
harassment.py
from googletrans import Translator
import alkana
input_str = input('harassment word : ')
tr = Translator(service_urls=['translate.googleapis.com'])
while True:
try:
text = tr.translate(input_str, dest="en").text
print(text)
break
except Exception as e:
tr = Translator(service_urls=['translate.googleapis.com'])
kana = alkana.get_kana(text)
lc_list = {'A','I','U','E','Oh','Tsu','YA','Yu','Yo'}
if kana[1] in lc_list :
print('generate =======> ' + kana[:3] + 'Hara')
else :
print('generate =======> ' + kana[:2] + 'Hara')
Recommended Posts