Convert some Japanese names to antonyms

Introduction

When I was a college student, I used to call a part of my friend's name as an antonym for some reason.

After repeating it several times, I fell in love with a friend's acupuncture points and laughed so much that I couldn't move. I wanted that friend to be happy for the first time in a long time, and I wanted to automate it (?), So I implemented it programmatically.

Convert some Japanese names to antonyms

Database

From LinkData, KeitarouNakayama has published "Antonym of one kanji character" ”was used. It is a public domain with 380 kinds of kanji (190 pairs).

Implementation

The code that implements the function looks like this:

import random
import re
import requests

JSON_URL = "http://linkdata.org/api/1/rdf1s3497i/antonym_rdf.json"
LABEL = "http://www.w3.org/2000/01/rdf-schema#label"
WORD = "http://ja.dbpedia.org/page/Chinese characters"
ANTONYM_WORD = "https://www.wikidata.org/wiki/Property:P461"

res = requests.get(JSON_URL)
data = res.json()

antonym_data = {}
for i in data:
  if i not in antonym_data:
      antonym_data[data[i][WORD][0]["value"]] = []
  antonym_data[data[i][WORD][0]["value"]].append(data[i][ANTONYM_WORD][0]["value"])

def be_antonyms_name(pure_name):
  antonym_name = ""
  for one_kanji in pure_name: 
    if one_kanji in antonym_data:
      antonym_name += random.choice(antonym_data[one_kanji])
    else :
      antonym_name += one_kanji
  return antonym_name

Run

Let's run it.

names = ["Oda Nobunaga", "Ryoma Sakamoto", "Saigo Takamori", "Minamoto no Yoritomo", "Ono no Imoko"]
for name in names:
  print(be_antonyms_name(name))
Nobutaka Oda
Sakasue Ryuneko
Ryu Togo
Gen Yoriyu
Ohno Sister

It has been converted correctly (?).

in conclusion

Please use it by all means! It can also be applied to sentences!

Postscript

--Updated on April 16, 2020 Python article summary (automatically updated daily), [Recent articles with many likes](https: / /qiita.com/kamata1729/items/eaf1d7b945b3a61a4fdd#%E3%81%84%E3%81%84%E3%81%AD%E3%81%8C%E5%A4%9A%E3%81%84%E6% 9C% 80% E8% BF% 91% E3% 81% AE% E8% A8% 98% E4% BA% 8B).

--Updated on April 16, 2020 [Python] Qiita Weekly Likes Ranking [Automatic Update], April 09 ~ [In the ranking] with April 16 as the aggregation period (https://qiita.com/kou_pg_0131/items/9d7f2ffeafb36cf59a77#22%E4%BD%8D-%E6%97%A5%E6%9C%AC% E4% BA% BA% E3% 81% AE% E5% 90% 8D% E5% 89% 8D% E3% 81% AE% E4% B8% 80% E9% 83% A8% E3% 82% 92% E5% AF% BE% E7% BE% A9% E8% AA% 9E% E3% 81% AB% E5% A4% 89% E6% 8F% 9B% E3% 81% 99% E3% 82% 8B), 22nd place I was introduced as.

Recommended Posts

Convert some Japanese names to antonyms
Program to convert Japanese to station name
Convert DataFrame column names from Japanese to English using Googletrans
Convert to HSV
Method to convert between Western calendar and Japanese calendar
Python practice Convert the input year to the Japanese calendar
Convert 202003 to 2020-03 with pandas
Convert kanji to kana
Convert jupyter to py
Convert keras-yolo3 to onnx
Convert dict to array
[Python3] I want to generate harassment names from Japanese!
Convert json to excel
Program to weaken Japanese
Convert hexadecimal string to binary
[python] Convert date to string
[Python] Convert list to Pandas [Pandas]
Convert HTML to text file
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert IP address to decimal
Convert SDF to CSV quickly
Convert genbank file to gff file
Convert python 3.x code to python 2.x