From 5a72a6d5ded284c56452fed9db3a06e30b90ae08 Mon Sep 17 00:00:00 2001 From: malayaleecoder Date: Fri, 20 May 2016 00:08:06 +0530 Subject: [PATCH 1/3] Transliteration for sentences --- varnamc | 55 +++++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/varnamc b/varnamc index e15bcf6..a4d46b1 100755 --- a/varnamc +++ b/varnamc @@ -978,46 +978,41 @@ def start_compilation exit(returncode) end -def ensure_single_word(text) - if text.split(' ').length > 1 - puts "varnamc : Expected a single word." - exit(1) - end -end - def transliterate if $options[:text_to_transliterate].nil? puts "Nothing to transliterate" exit 1 end - totl = $options[:text_to_transliterate] - ensure_single_word totl - - if $options[:indic_digits] - configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), - Varnam::VARNAM_CONFIG_USE_INDIC_DIGITS, :string, "1") - if configured != 0 - error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) - error error_message - exit(1) - end - end + sentence = $options[:text_to_transliterate] + sentence = sentence.split(' ') + + sentence.each do |totl| + if $options[:indic_digits] + configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), + Varnam::VARNAM_CONFIG_USE_INDIC_DIGITS, :string, "1") + if configured != 0 + error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) + error error_message + exit(1) + end + end - words_ptr = FFI::MemoryPointer.new :pointer - done = VarnamLibrary.varnam_transliterate($varnam_handle.get_pointer(0), totl, words_ptr); - if done != 0 - error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) - puts error_message - exit(1) - end + words_ptr = FFI::MemoryPointer.new :pointer + done = VarnamLibrary.varnam_transliterate($varnam_handle.get_pointer(0), totl, words_ptr); + if done != 0 + error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) + puts error_message + exit(1) + end - size = VarnamLibrary.varray_length(words_ptr.get_pointer(0)) - 0.upto(size - 1) do |i| - word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), i) + size = VarnamLibrary.varray_length(words_ptr.get_pointer(0)) + word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), 0) vword = VarnamLibrary::Word.new(word_ptr) word = VarnamWord.new(vword[:text], vword[:confidence]) - puts " " + word.text + print " " + word.text end + + puts "\n" end def reverse_transliterate From 63079efadc84bc7131a30851fcfc572a3624bb76 Mon Sep 17 00:00:00 2001 From: malayaleecoder Date: Fri, 20 May 2016 10:42:01 +0530 Subject: [PATCH 2/3] Show multiple suggestions for a word --- varnamc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/varnamc b/varnamc index a4d46b1..a2cdaf2 100755 --- a/varnamc +++ b/varnamc @@ -1004,12 +1004,21 @@ def transliterate puts error_message exit(1) end - size = VarnamLibrary.varray_length(words_ptr.get_pointer(0)) - word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), 0) - vword = VarnamLibrary::Word.new(word_ptr) - word = VarnamWord.new(vword[:text], vword[:confidence]) - print " " + word.text + + if sentence.size != 1 + word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), 0) + vword = VarnamLibrary::Word.new(word_ptr) + word = VarnamWord.new(vword[:text], vword[:confidence]) + print " " + word.text + else + 0.upto(size - 1) do |i| + word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), i) + vword = VarnamLibrary::Word.new(word_ptr) + word = VarnamWord.new(vword[:text], vword[:confidence]) + puts " " + word.text + end + end end puts "\n" From 67ae641490fb0ad02fbaa93178e00fe758156075 Mon Sep 17 00:00:00 2001 From: malayaleecoder Date: Sun, 22 May 2016 23:38:15 +0530 Subject: [PATCH 3/3] Enable Varnam for sentences --- varnamc | 65 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/varnamc b/varnamc index a2cdaf2..7ba300e 100755 --- a/varnamc +++ b/varnamc @@ -346,8 +346,8 @@ def do_action end initialize_varnam_handle - if $options[:action] == 'transliterate' - transliterate + if $options[:action] == 'transliterate' + transliterate end if $options[:action] == 'reverse-transliterate' reverse_transliterate @@ -983,20 +983,19 @@ def transliterate puts "Nothing to transliterate" exit 1 end - sentence = $options[:text_to_transliterate] - sentence = sentence.split(' ') - - sentence.each do |totl| - if $options[:indic_digits] - configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), - Varnam::VARNAM_CONFIG_USE_INDIC_DIGITS, :string, "1") - if configured != 0 - error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) - error error_message - exit(1) - end + sentence_to_tl = $options[:text_to_transliterate] + sentence_to_tl = sentence_to_tl.split(' ') + if $options[:indic_digits] + configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), + Varnam::VARNAM_CONFIG_USE_INDIC_DIGITS, :string, "1") + if configured != 0 + error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) + error error_message + exit(1) end + end + sentence_to_tl.each do |totl| words_ptr = FFI::MemoryPointer.new :pointer done = VarnamLibrary.varnam_transliterate($varnam_handle.get_pointer(0), totl, words_ptr); if done != 0 @@ -1006,22 +1005,21 @@ def transliterate end size = VarnamLibrary.varray_length(words_ptr.get_pointer(0)) - if sentence.size != 1 + if sentence_to_tl.size != 1 word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), 0) vword = VarnamLibrary::Word.new(word_ptr) word = VarnamWord.new(vword[:text], vword[:confidence]) - print " " + word.text - else + print " " + word.text + else 0.upto(size - 1) do |i| word_ptr = VarnamLibrary.varray_get(words_ptr.get_pointer(0), i) vword = VarnamLibrary::Word.new(word_ptr) word = VarnamWord.new(vword[:text], vword[:confidence]) - puts " " + word.text + puts " " + word.text end end end - - puts "\n" + print "\n" end def reverse_transliterate @@ -1029,20 +1027,23 @@ def reverse_transliterate puts "Nothing to reverse transliterate" exit 1 end - tortl = $options[:text_to_reverse_transliterate] - ensure_single_word tortl + sentence_to_rtl = $options[:text_to_reverse_transliterate] + sentence_to_rtl = sentence_to_rtl.split(' ') - output_ptr = FFI::MemoryPointer.new(:pointer, 1) - done = VarnamLibrary.varnam_reverse_transliterate($varnam_handle.get_pointer(0), tortl, output_ptr); - if done != 0 - error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) - puts error_message - exit(1) - end + sentence_to_rtl.each do |tortl| + output_ptr = FFI::MemoryPointer.new(:pointer, 1) + done = VarnamLibrary.varnam_reverse_transliterate($varnam_handle.get_pointer(0), tortl, output_ptr); + if done != 0 + error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0)) + puts error_message + exit(1) + end - ptr = output_ptr.read_pointer() - output = ptr.nil? ? "" : ptr.read_string - puts output + ptr = output_ptr.read_pointer() + output = ptr.nil? ? "" : ptr.read_string + print " " + output + end + print "\n" end def learn_text