The primary way to use this library is via the neologdn.normalize() function. It normalizes Japanese text based on mecab-neologd rules, including converting half-width kana to full-width, normalizing symbols, and handling various hyphen/tilde types.
Key Features:
- Kana Normalization: Converts half-width kana (e.g.,
ハンカクカナ) to full-width (ハンカクカナ). - Symbol Normalization: Standardizes full-width symbols (e.g.,
!?@# to !?@#). - Repetition Shortening: Using the
repeat parameter, you can shorten contiguous repeating characters (e.g., ウェーーーーイ to ウェーイ). - Tilde Handling: Use the
tilde parameter to control how tildes are treated.
import neologdn
# Basic normalization
neologdn.normalize("ハンカクカナ") # => 'ハンカクカナ'
neologdn.normalize("全角記号!?@#") # => '全角記号!?@#'
# Shortening repeated characters
# repeat=1 keeps only 1 instance of the repeated character sequence
neologdn.normalize("かわいいいいいいいいい", repeat=6) # => 'かわいいいいいい'
neologdn.normalize("無駄無駄無駄無駄ァ", repeat=1) # => '無駄ァ'
# Tilde options
# tilde="normalize": converts to standard tilde (~)
neologdn.normalize("1995〜2001年", tilde="normalize") # => '1995~2001年'
# tilde="normalize_zenkaku": converts to full-width tilde (〜)
neologdn.normalize("1995~2001年", tilde="normalize_zenkaku") # => '1995〜2001年'
# tilde="ignore": does not convert the tilde
neologdn.normalize("1995〜2001年", tilde="ignore") # => '1995〜2001年'
# tilde="remove": removes the tilde entirely
neologdn.normalize("1995〜2001年", tilde="remove") # => '19952001年'
# Default behavior (removes tilde)
neologdn.normalize("1995〜2001年") # => '19952001年'