Girl007
Girl007

Reputation: 175

Convert names into romanji version using python

I am currently using a japanese conversion library called pykakasi

I am trying to write a function so that it converts the japenese name to a romanised version:

Please find my function below:

from pykakasi import kakasi

def jp_romanise(word):
    result = kakasi().convert(word)
    return result

print("jp_romanise:", jp_romanise("佐藤和也"))

when I print the following japenese name I am expecting it to give me the romanised version of the name but instead I am getting:

jp_romanise: [{'orig': '佐藤', 'hira': 'さとう', 'kana': 'サトウ', ***'hepburn': 'satou', 'kunrei'***: 'satou', 'passport': 'sato'}, {'orig': '和也', 'hira': 'かず

I only want to return the hepburn romanised name (Satou Kunrei) as highlighted in bold above - how can I get my function to grab this info

My expected Output:

Satou Kazuya

Upvotes: 0

Views: 199

Answers (1)

Mike Scotty
Mike Scotty

Reputation: 10782

kakasi().convert() returns a list ob dictionaries. You have to grab the "hepburn" parts and then combine them to a final string:

from pykakasi import kakasi

def jp_romanise(word):
    result = []
    items = kakasi().convert(word)
    for item in items:
        result.append(item['hepburn'].capitalize())
    return " ".join(result)

print("jp_romanise:", jp_romanise("佐藤和也"))

Output:

jp_romanise: Satou Kazuya

Upvotes: 1

Related Questions