Module: Sanscript::Transliterate
- Defined in:
- lib/sanscript/transliterate.rb,
lib/sanscript/transliterate/schemes.rb
Overview
Sanskrit transliteration module. Derived from Sanscript (github.com/sanskrit/sanscript.js), which is released under the MIT and GPL Licenses.
“Sanscript is a Sanskrit transliteration library. Currently, it supports other Indian languages only incidentally.”
Class Attribute Summary collapse
-
.all_alternates ⇒ Hash
readonly
The alternate-character data for all schemes.
-
.brahmic_schemes ⇒ Array<Symbol>
readonly
The names of all Brahmic schemes.
-
.defaults ⇒ Hash
readonly
The default transliteration options.
-
.roman_schemes ⇒ Array<Symbol>
readonly
The names of all roman schemes.
-
.scheme_names ⇒ Array<Symbol>
readonly
The names of all supported schemes.
-
.schemes ⇒ Hash
readonly
The data for all schemes.
Class Method Summary collapse
-
.add_brahmic_scheme(name, scheme) ⇒ Hash
Add a Brahmic scheme to Sanscript.
-
.add_roman_scheme(name, scheme) ⇒ Hash
Add a roman scheme to Sanscript.
-
.brahmic_scheme?(name) ⇒ Boolean
Check whether the given scheme encodes Brahmic Sanskrit.
-
.roman_scheme?(name) ⇒ Boolean
Check whether the given scheme encodes romanized Sanskrit.
-
.transliterate(data, from, to, **opts) ⇒ String
Transliterate from one script to another.
Class Attribute Details
.all_alternates ⇒ Hash (readonly)
Returns the alternate-character data for all schemes.
27 28 29 |
# File 'lib/sanscript/transliterate.rb', line 27 def all_alternates @all_alternates end |
.brahmic_schemes ⇒ Array<Symbol> (readonly)
Returns the names of all Brahmic schemes.
18 19 20 |
# File 'lib/sanscript/transliterate.rb', line 18 def brahmic_schemes @brahmic_schemes end |
.defaults ⇒ Hash (readonly)
Returns the default transliteration options.
30 31 32 |
# File 'lib/sanscript/transliterate.rb', line 30 def defaults @defaults end |
.roman_schemes ⇒ Array<Symbol> (readonly)
Returns the names of all roman schemes.
21 22 23 |
# File 'lib/sanscript/transliterate.rb', line 21 def roman_schemes @roman_schemes end |
.scheme_names ⇒ Array<Symbol> (readonly)
Returns the names of all supported schemes.
15 16 17 |
# File 'lib/sanscript/transliterate.rb', line 15 def scheme_names @scheme_names end |
.schemes ⇒ Hash (readonly)
Returns the data for all schemes.
24 25 26 |
# File 'lib/sanscript/transliterate.rb', line 24 def schemes @schemes end |
Class Method Details
.add_brahmic_scheme(name, scheme) ⇒ Hash
Add a Brahmic scheme to Sanscript.
Schemes are of two types: “Brahmic” and “roman”. Brahmic consonants have an inherent vowel sound, but roman consonants do not. This is the main difference between these two types of scheme.
A scheme definition is a Hash that maps a group name to a list of characters. For illustration, see ‘transliterate/schemes.rb`.
You can use whatever group names you like, but for the best results, you should use the same group names that Sanscript does.
73 74 75 76 77 78 79 80 |
# File 'lib/sanscript/transliterate.rb', line 73 def add_brahmic_scheme(name, scheme) name = name.to_sym scheme = scheme.deep_dup @schemes[name] = scheme.deep_freeze @brahmic_schemes.add(name) @scheme_names.add(name) scheme end |
.add_roman_scheme(name, scheme) ⇒ Hash
Add a roman scheme to Sanscript.
88 89 90 91 92 93 94 95 96 |
# File 'lib/sanscript/transliterate.rb', line 88 def add_roman_scheme(name, scheme) name = name.to_sym scheme = scheme.deep_dup scheme[:vowel_marks] = scheme[:vowels][1..-1] unless scheme.key?(:vowel_marks) @schemes[name] = scheme.deep_freeze @roman_schemes.add(name) @scheme_names.add(name) scheme end |
.brahmic_scheme?(name) ⇒ Boolean
Check whether the given scheme encodes Brahmic Sanskrit.
46 47 48 |
# File 'lib/sanscript/transliterate.rb', line 46 def brahmic_scheme?(name) @brahmic_schemes.include?(name.to_sym) end |
.roman_scheme?(name) ⇒ Boolean
Check whether the given scheme encodes romanized Sanskrit.
@param name [Symbol] the scheme name
@return [Boolean]
54 55 56 |
# File 'lib/sanscript/transliterate.rb', line 54 def roman_scheme?(name) @roman_schemes.include?(name.to_sym) end |
.transliterate(data, from, to, **opts) ⇒ String
Transliterate from one script to another.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sanscript/transliterate.rb', line 142 def transliterate(data, from, to, **opts) from = from.to_sym to = to.to_sym return data if from == to raise SchemeNotSupportedError, from unless @schemes.key?(from) raise SchemeNotSupportedError, to unless @schemes.key?(to) data = data.to_str.dup = @defaults.merge(opts) map = make_map(from, to) data.gsub!(/(<.*?>)/, "##\\1##") if [:skip_sgml] # Easy way out for "{\m+}", "\", and ".h". if from == :itrans data.gsub!(/\{\\m\+\}/, ".h.N") data.gsub!(/\.h/, "") data.gsub!(/\\([^'`_]|$)/, "##\\1##") end if map[:from_roman?] transliterate_roman(data, map, ) else transliterate_brahmic(data, map) end end |