Skip to content

DiDi035/DiacritiX

Repository files navigation

DiacritiX

CI Gem Version

A Ruby gem for Vietnamese diacritics. It solves two problems:

  1. Accent placement — determines which letter in a Vietnamese word should receive the accent mark (e.g., in "ngoay", the accent goes on the "a")
  2. IME input — transforms numeric-suffixed text into accented Vietnamese using the VNI input method

Why?

Vietnamese accent placement follows specific rules based on vowel combinations and consonant pairs. This gem encodes those rules so you don't have to.

Installation

gem install diacriti_x

Or add to your Gemfile:

gem 'diacriti_x'

Usage

As a library

require 'diacriti_x'

# Find which letter gets the accent
result = DiacritiX::Accentifier.call('ngoay')
result.success? # => true
result.result   # => 3 (index of 'a')

# Transform VNI input into Vietnamese
vni = DiacritiX::IME::VNI.new
vni.call('Viet65 Nam')  # => "Việt Nam"
vni.call('toi1')        # => "tói"
vni.call('di9')         # => "đi"

VNI key mapping

Key Diacritic Example
1 Acute (á) a1á
2 Grave (à) a2à
3 Hook above (ả) a3
4 Tilde (ã) a4ã
5 Dot below (ạ) a5
6 Circumflex (â) a6â
7 Horn (ư, ơ) u7ư
8 Breve (ă) a8ă
9 Đ stroke d9đ

CLI

# Highlight which letter receives the accent
accentify "ngoay"
# => ngo[a]y (the 'a' is highlighted in green)

# Transform VNI input into Vietnamese
ruby bin/ime/vni "Vie6t5 Nam"
# => Việt Nam

Accent placement rules

The Accentifier applies rules in priority order — first match wins:

  1. Special Vietnamese vowels — ơ, ư always take the accent
  2. Vietnamese vowels — ă, â, ê, ô take priority over plain Latin vowels
  3. Vowel pairs — in oa, oe, oo, uy, ươ the second vowel gets it
  4. Consonant pairs — after gi, qu the accent goes on the next vowel
  5. Fallback — first vowel found

Development

git clone https://github.com/DiDi035/DiacritiX.git
cd DiacritiX
bundle install
bundle exec rake test
bundle exec rubocop

About

✍️ A simple gem to specify which letter in a Vietnamese word should accents be place above

Resources

Stars

1 star

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages