[Python3] I want to generate harassment names from Japanese!

Introduction

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.

Development environment

macOS Catalina 10.15.6 MacBook Pro (16-inch, 2019) Python 3.8.6

Background and purpose

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"

Rough flow

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.

Advance preparation

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")

Implementation

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!

problem

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.

For the opening article

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"?

in conclusion

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

[Python3] I want to generate harassment names from Japanese!
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to debug with Python
I want to start a lot of processes from python
I want to send a message from Python to LINE Bot
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
I want to connect to PostgreSQL from various languages
I want to use MATLAB feval with python
I want to memoize including Python keyword arguments
I want to create a window in Python
I want to perform SageMaker inference from PHP
I want to make a game with Python
How to generate a Python object from JSON
I want to merge nested dicts in Python
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
What I did when updating from Python 2.6 to 2.7
I want to sell Mercari by scraping python
I want to write to a file with Python
I want to display the progress in Python!
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
I want to get / execute variables / functions / classes of external files from Python
I want to write in Python! (1) Code format check
I want to see the file name from DataLoader
Even beginners want to say "I fully understand Python"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to handle optimization with python and cplex
[Python] I want to merge Excel files anyway (pandas.merge)
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
Preferences to generate animated GIFs from Python on Mac
I wanted to use the Python library from MATLAB
I want to randomly sample a file in Python
I want to inherit to the back with python dataclass
I want to work with a robot in python.
[Python] I want to make a nested list a tuple
I tried to automatically generate a password with Python3
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
[Python] I made a system to introduce "recipes I really want" from the recipe site!
I want to tell people who want to import from a higher directory with Python direnv
I wrote python in Japanese
Post from Python to Slack
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)