forked from varnamproject/libvarnam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
varnamc
executable file
·1404 lines (1190 loc) · 40.4 KB
/
varnamc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# encoding: utf-8
##
## Copyright (C) Navaneeth.K.N
##
## This is part of libvarnam. See LICENSE.txt for the license
##
=begin
'varnamc' is a command line client to libvarnam. It allows you to quickly try
libvarnam's features.
Usage - varnamc options <args>
=end
def gem_available?(name)
require name
rescue LoadError
false
end
if not gem_available?('ffi')
puts "Can't find gem - ffi. To install run '[sudo] gem install ffi'"
exit(1)
end
$options = {}
$libvarnam_major_version = 3
def find_libvarnam
return $options[:library] if not $options[:library].nil?
# Trying to find out libvarnam in the predefined locations if
# absolute path to the library is not specified
libvarnam_search_paths = ['.', File.dirname(File.expand_path(__FILE__)), '/usr/local/lib', '/usr/local/lib/i386-linux-gnu', '/usr/local/lib/x86_64-linux-gnu', '/usr/lib/i386-linux-gnu', '/usr/lib/x86_64-linux-gnu', '/usr/lib']
libvarnam_names = ['libvarnam.so', "libvarnam.so.#{$libvarnam_major_version}", 'libvarnam.dylib', 'varnam.dll']
libvarnam_search_paths.each do |path|
libvarnam_names.each do |fname|
fullpath = File.join(path, fname)
if File.exists?(fullpath)
return fullpath
end
end
end
return nil
end
$options[:library] = find_libvarnam
if $options[:library].nil?
puts "varnamc - Can't find varnam shared library. Try specifying the full path using -l option"
puts optparse
else
puts "Using #{$options[:library]}" if $options[:verbose]
end
varnamruby_searchpaths = [".", File.dirname(File.expand_path(__FILE__)), "/usr/local/lib", "/usr/lib"]
varnamruby_loaded = false
varnamruby_searchpaths.each do |p|
begin
require "#{p}/varnamruby.rb"
varnamruby_loaded = true
break
rescue LoadError
# Trying next possibility
end
end
if not varnamruby_loaded
puts "Failed to find varnamruby.rb. Search paths: #{varnamruby_searchpaths}"
puts "This could be because you have a corrupted installation or a bug in varnamc"
exit(1)
end
require 'optparse'
require 'fileutils'
# Defining command line options
$options[:action] = nil
def set_action(a)
if $options[:action].nil?
$options[:action] = a
else
puts "varnamc : #{$options[:action]} and #{a} are mutually exclusive options. Only one action is allowed"
exit(1)
end
end
optparse = OptionParser.new do |opts|
opts.banner = "Usage: varnamc options args"
# ability to provide varnam library name
$options[:library] = nil
opts.on('-l', '--library FILE', 'Sets the varnam library') do |file|
if not File.exist?(file)
puts "varnamc : Can't find #{file}"
exit 1
end
$options[:library] = file
end
$options[:verbose] = false
opts.on('-v', '--verbose', 'Enable verbose output') do
$options[:verbose] = true
end
opts.on('-t', '--transliterate TEXT', 'Transliterate the given text') do |text|
set_action('transliterate')
$options[:text_to_transliterate] = text
end
$options[:indic_digits] = false
opts.on('--indic-digits', 'Turns on indic digit rendering while transliterating') do
$options[:indic_digits] = true
end
opts.on('-r', '--reverse-transliterate TEXT', 'Reverse transliterate the given text') do |text|
$options[:text_to_reverse_transliterate] = text
set_action('reverse-transliterate')
end
opts.on('-n', '--learn [TEXT]', 'Learn the given text') do |text|
$options[:text_to_learn] = text
set_action('learn')
end
opts.on('-a', '--train PATTERN=WORD', 'Train varnam to use PATTERN for WORD') do |str|
training_data = str.split('=')
if not training_data.size == 2
puts 'varnamc : Incorrect arguments'
exit(1)
end
$options[:training_data] = training_data
set_action('train')
end
opts.on('-f', '--learn-from FILE|DIRECTORY', 'Reads from the specified file/directory') do |path|
if File.exists? (path)
if File.directory?(path)
set_action('learn-from-directory')
else
set_action('learn-from-file')
end
$options[:learn_from] = path
else
puts "varnamc : #{path} is incorrect"
exit(1)
end
end
opts.on('--train-from FILE|DIRECTORY', 'Reads the specified file/directory and trains all the words specified') do |path|
if File.exists? (path)
if File.directory?(path)
set_action('train-from-directory')
else
set_action('train-from-file')
end
$options[:train_from] = path
else
puts "varnamc : #{path} is incorrect"
exit(1)
end
end
opts.on('-e','--export-words', 'Export words to the output directory') do
set_action('export-words')
end
opts.on('--export-full', 'Export words & patterns to the output directory') do
set_action('export-full')
end
opts.on('--import-learnings-from FILE|DIRECTORY', 'Import learned data from the specified file/directory') do |path|
if File.exists? (path)
if File.directory?(path)
set_action('import-learnings-from-directory')
else
set_action('import-learnings-from-file')
end
$options[:import_learnings_from] = path
else
puts "varnamc : #{path} is incorrect"
exit(1)
end
end
$options[:symbols_file] = nil
opts.on('-s', '--symbols VALUE', 'Sets the symbols file') do |value|
if File.exist?(value)
$options[:symbols_file] = value
else
$options[:lang_code] = value;
end
end
$options[:file_to_compile] = nil
opts.on('-c', '--compile FILE', 'Compile symbols file') do |file|
if not File.exist?(file)
puts "Can't find #{file}"
exit(1)
end
$options[:file_to_compile] = file
set_action('compile')
end
$options[:learnings_file] = nil
opts.on('--learnings-file FILE', 'Specify the file to store all learnings') do |file|
$options[:learnings_file] = file
end
$options[:word_to_detect_lang] = nil
opts.on('--detect-language WORD', 'Detect language of the word') do |word|
set_action('detect')
$options[:word_to_detect_lang] = word
end
$options[:output_directory] = nil
opts.on('-d', '--output-dir dir', 'Sets the output directory') do |directory|
if not Dir.exist?(directory)
puts "#{directory} is not a directory"
exit(1)
end
$options[:output_directory] = directory
end
# help screen
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
opts.on( '--schemes-available', 'Display all available schemes' ) do
set_action('schemes-available')
end
opts.on('--version', 'Display version' ) do
puts "libvarnam version #{VarnamLibrary.varnam_version()}"
exit
end
end
begin
optparse.parse!
rescue => e
puts "varnamc : incorrect arguments"
puts e.to_s
puts optparse
exit(1)
end
if $options[:action].nil?
puts "varnamc : no actions specified"
puts optparse
exit(1)
end
$suggestions_file = ''
def initialize_varnam_handle
if $options[:action] == 'compile'
$vst_file_name = $options[:file_to_compile].sub(File.extname($options[:file_to_compile]), "") + ".vst"
if not $options[:output_directory].nil?
$vst_file_name = get_file_path(File.basename($vst_file_name))
end
if File.exists?($vst_file_name)
File.delete($vst_file_name)
end
else
$vst_file_name = $options[:symbols_file]
end
initialized = false;
$varnam_handle = FFI::MemoryPointer.new :pointer
init_error_msg = FFI::MemoryPointer.new(:pointer, 1)
if not $vst_file_name.nil?
initialized = VarnamLibrary.varnam_init($vst_file_name, $varnam_handle, init_error_msg)
# Configuring suggestions
$options[:learnings_file] = get_learnings_file $vst_file_name
configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), Varnam::VARNAM_CONFIG_ENABLE_SUGGESTIONS, :string, $options[:learnings_file])
if configured != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
exit(1)
end
elsif not $options[:lang_code].nil?
sources_root = ENV["VARNAM_SOURCES_ROOT"]
unless sources_root.nil?
VarnamLibrary.varnam_set_symbols_dir "#{ENV['VARNAM_SOURCES_ROOT']}/schemes"
end
initialized = VarnamLibrary.varnam_init_from_id($options[:lang_code], $varnam_handle, init_error_msg)
if initialized == 0 and not $options[:learnings_file].nil?
# User has specified explicit learnings file. Use that instead of the default one
configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), Varnam::VARNAM_CONFIG_ENABLE_SUGGESTIONS, :string, $options[:learnings_file])
if configured != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
exit(1)
end
end
else
puts "varnamc : Can't load symbols file. Use --symbols option to specify the symbols file"
exit(1)
end
if (initialized != 0)
ptr = init_error_msg.read_pointer()
msg = ptr.nil? ? "" : ptr.read_string
puts "Varnam initialization failed #{msg}"
exit(1)
end
end
def get_file_path(fname)
if $options[:output_directory].nil?
return File.join(Dir.home, ".local/share/varnam", fname)
else
return File.join($options[:output_directory], fname)
end
end
def get_learnings_file(symbols_file_name)
return $options[:learnings_file] if not $options[:learnings_file].nil?
# we use the standard $HOME/.local/share/varnam/suggestions
make_suggestions_dir_if_required
return get_file_path(File.join("suggestions", "#{File.basename(symbols_file_name, ".")}.learnings"))
end
def make_suggestions_dir_if_required
out_dir = File.join(Dir.home, '.local/share/varnam/suggestions')
if not $options[:output_directory].nil?
out_dir = $options[:output_directory]
end
FileUtils.mkdir_p out_dir if not Dir.exists?(out_dir)
end
def do_action
if $options[:action] == 'schemes-available'
display_available_schemes
return
end
initialize_varnam_handle
if $options[:action] == 'transliterate'
transliterate
end
if $options[:action] == 'reverse-transliterate'
reverse_transliterate
end
if $options[:action] == 'compile'
start_compilation
end
if $options[:action] == 'learn'
learn_text
end
if $options[:action] == 'learn-from-file'
learn_from_file
end
if $options[:action] == 'learn-from-directory'
learn_from_directory
end
if $options[:action] == 'train-from-file'
train_from_file
end
if $options[:action] == 'train-from-directory'
train_from_directory
end
if $options[:action] == 'export-words' or $options[:action] == 'export-full'
export_words
end
if $options[:action] == 'train'
train_pattern_word
end
if $options[:action] == 'detect'
detect_language
end
if $options[:action] == 'import-learnings-from-file'
import_learnings_from_file
end
if $options[:action] == 'import-learnings-from-directory'
import_learnings_from_directory
end
end
$custom_lists = {}
$current_custom_list = []
# Starts a list context. Any tokens created inside will get added to this list
# It can have multiple list names and token will get added to all of these. One token
# can be in multiple lists
def list(*names, &block)
if not $current_custom_list.empty?
# This happens when user tries to nest list.
# Nesting list is not allowed
error "Can't create nested list"
exit (1)
end
if names.empty?
error "List should have a name"
exit (1)
end
names.each do |name|
if not name.is_a?(String) and not name.is_a?(Symbol)
error "List name should be a string or symbols"
exit (1)
end
$custom_lists[name] = [] if not $custom_lists.has_key?(name)
$current_custom_list << $custom_lists[name]
end
yield if block_given?
ensure
$current_custom_list = []
end
def push_to_current_custom_list(token)
if token.nil?
error "Can't add empty token"
exit (1)
end
$current_custom_list.each do |l|
l.push(token)
end
end
# We handle method missing to return appropriate lists
def self.method_missing(name, *args, &block)
return $custom_lists[name] if $custom_lists.has_key?(name)
super
end
# this contains default symbols key overridden in the scheme file
# key will be the token type
$overridden_default_symbols = []
def _ensure_sanity_of_array(array)
# Possibilities are
# [e1, e2]
# [e1, [e2,e3], e4]
error "An empty array won't workout" if array.size == 0
array.each do |element|
if element.is_a?(Array)
_ensure_sanity_of_array(element)
else
_ensure_type_safety(element)
end
end
end
def _ensure_sanity_of_element(element)
if element.is_a?(Array)
_ensure_sanity_of_array(element)
else
_ensure_type_safety(element)
if element.is_a?(String) and element.length == 0
error "Empty values are not allowed"
end
end
end
def _ensure_type_safety(element)
valid_types = [Fixnum, String, Array]
error "#{element.class} is not a valid type. Valid types are #{valid_types.to_s}" if not valid_types.include?(element.class)
end
def _ensure_sanity(hash)
if not hash.is_a?(Hash)
error "Expected a Hash, but got a #{hash.class}"
exit 1
end
hash.each_pair do |key, value|
_context.current_expression = "#{key} => #{value}"
_ensure_sanity_of_element (key)
_ensure_sanity_of_element (value)
warn "#{value} has more than three elements. Additional elements specified will be ignored" if value.is_a?(Array) and value.size > 3
_context.current_expression = nil
end
end
def _extract_keys_values_and_persist(keys, values, token_type, match_type = Varnam::VARNAM_MATCH_EXACT, priority, accept_condition)
keys.each do |key|
if key.is_a?(Array)
# This a possibility match
key.flatten!
_extract_keys_values_and_persist(key, values, token_type, Varnam::VARNAM_MATCH_POSSIBILITY, priority, accept_condition)
else
_persist_key_values(key, values, token_type, match_type, priority, accept_condition)
end
end
end
def _persist_key_values(pattern, values, token_type, match_type, priority, accept_condition)
return if _context.errors > 0
match = match_type == Varnam::VARNAM_MATCH_EXACT ? "EXACT" : "POSSIBILITY"
if (values.is_a?(Array))
values.flatten!
value1 = values[0]
value2 = values[1] if values.size >= 2
value3 = values[2] if values.size >= 3
else
value1 = values
value2 = ""
value3 = ""
end
tag = _context.current_tag
tag = "" if tag.nil?
created = VarnamLibrary.varnam_create_token($varnam_handle.get_pointer(0), pattern, value1, value2, value3, tag, token_type, match_type, priority, accept_condition, 1)
if created != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
return
end
_context.tokens[token_type] = [] if _context.tokens[token_type].nil?
vtoken = VarnamToken.new(token_type, pattern, value1, value2, value3, tag, match_type, priority, accept_condition)
_context.tokens[token_type].push(vtoken)
push_to_current_custom_list vtoken
end
def flush_unsaved_changes
saved = VarnamLibrary.varnam_flush_buffer($varnam_handle.get_pointer(0))
if saved != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
return
end
end
def infer_dead_consonants(infer)
configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), Varnam::VARNAM_CONFIG_USE_DEAD_CONSONANTS, :int, infer ? 1 : 0)
if configured != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
return
end
end
def ignore_duplicates(ignore)
configured = VarnamLibrary.varnam_config($varnam_handle.get_pointer(0), Varnam::VARNAM_CONFIG_IGNORE_DUPLICATE_TOKEN, :int, ignore ? 1 : 0)
if configured != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
return
end
end
def set_scheme_details()
d = VarnamLibrary::SchemeDetails.new
d[:langCode] = FFI::MemoryPointer.from_string($scheme_details[:langCode])
d[:identifier] = FFI::MemoryPointer.from_string($scheme_details[:identifier])
d[:displayName] = FFI::MemoryPointer.from_string($scheme_details[:displayName])
d[:author] = FFI::MemoryPointer.from_string($scheme_details[:author])
d[:compiledDate] = FFI::MemoryPointer.from_string(Time.now.to_s)
if $scheme_details[:isStable].nil?
d[:isStable] = 0
else
d[:isStable] = $scheme_details[:isStable]
end
done = VarnamLibrary.varnam_set_scheme_details($varnam_handle.get_pointer(0), d.pointer)
if done != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
return
end
end
$scheme_details = {}
def language_code(code)
$scheme_details[:langCode] = code
end
def identifier(id)
$scheme_details[:identifier] = id
end
def display_name(name)
$scheme_details[:displayName] = name
end
def author(name)
$scheme_details[:author] = name
end
def stable(value)
$scheme_details[:isStable] = 0
$scheme_details[:isStable] = 1 if value
end
def generate_cv
all_vowels = get_vowels
all_consonants = get_consonants
all_consonants.each do |c|
consonant_has_inherent_a_sound = c.pattern.end_with?('a') and not c.pattern[c.pattern.length - 2] == 'a'
all_vowels.each do |v|
next if v.value2.nil? or v.value2.length == 0
if consonant_has_inherent_a_sound
pattern = "#{c.pattern[0..c.pattern.length-2]}#{v.pattern}"
else
pattern = "#{c.pattern}#{v.pattern}"
end
values = ["#{c.value1}#{v.value2}"]
if c.match_type == Varnam::VARNAM_MATCH_POSSIBILITY or v.match_type == Varnam::VARNAM_MATCH_POSSIBILITY
match_type = Varnam::VARNAM_MATCH_POSSIBILITY
else
match_type = Varnam::VARNAM_MATCH_EXACT
end
accept_condition = nil
if not v.accept_condition == Varnam::VARNAM_TOKEN_ACCEPT_ALL and not c.accept_condition == Varnam::VARNAM_TOKEN_ACCEPT_ALL
accept_condition = v.accept_condition
elsif not v.accept_condition == Varnam::VARNAM_TOKEN_ACCEPT_ALL
accept_condition = v.accept_condition
else
accept_condition = c.accept_condition
end
priority = Varnam::VARNAM_TOKEN_PRIORITY_NORMAL
if v.priority < c.priority
priority = v.priority
else
priority = c.priority
end
_persist_key_values pattern, values, Varnam::VARNAM_TOKEN_CONSONANT_VOWEL, match_type, priority, accept_condition
end
end
end
def combine_array(array, is_pattern, replacements, current_item)
if replacements.empty?
error 'Replacements should be present when combining an array. This could be a bug within varnamc'
exit (1)
end
result = []
array.each do |a|
if a.is_a?(Array)
result.push(combine_array(a, is_pattern, replacements, current_item))
else
if is_pattern
if current_item.match_type == Varnam::VARNAM_MATCH_POSSIBILITY
result.push([a.to_s.gsub("*", replacements[0])])
else
result.push(a.to_s.gsub("*", replacements[0]))
end
else
new_key = a.to_s.gsub("\*1", replacements[0])
if replacements.length > 1 and not replacements[1].to_s.empty?
new_key = new_key.gsub("\*2", replacements[1])
end
if replacements.length > 2 and not replacements[2].to_s.empty?
new_key = new_key.gsub("\*3", replacements[2])
end
result.push (new_key)
end
end
end
return result
end
# Combines an array and a hash values
# This method also replaces the placeholder in hash
def combine(array, hash)
_ensure_sanity(hash)
if not array.is_a?(Array)
error "Expected an array, but got a #{array.class}"
exit 1
end
grouped = {}
array.each do |item|
hash.each_pair do |key, value|
new_key = nil
if key.is_a?(Array)
new_key = combine_array(key, true, [item.pattern], item)
else
if item.match_type == Varnam::VARNAM_MATCH_POSSIBILITY
new_key = [[key.to_s.gsub("*", item.pattern)]]
else
new_key = key.to_s.gsub("*", item.pattern)
end
end
new_value = nil
if value.is_a?(Array)
new_value = combine_array(value, false, [item.value1, item.value2, item.value3], item)
else
new_value = value.to_s.gsub("\*1", item.value1)
if not item.value2.nil? and not item.value2.to_s.empty?
new_value = new_value.gsub("\*2", item.value2)
end
if not item.value3.nil? and not item.value3.to_s.empty?
new_value = new_value.gsub("\*3", item.value3)
end
end
if grouped[new_value].nil?
grouped[new_value] = new_key
else
grouped[new_value].push(new_key)
end
end
end
# invert the hash
result = {}
grouped.each_pair do |key, value|
result[value] = key
end
return result
end
def _create_token(hash, token_type, options = {})
return if _context.errors > 0
priority = _get_priority options
accept_condition = _get_accept_condition options
hash.each_pair do |key, value|
if key.is_a?(Array)
_extract_keys_values_and_persist(key, value, token_type, priority, accept_condition)
else
_persist_key_values(key, value, token_type, Varnam::VARNAM_MATCH_EXACT, priority, accept_condition)
end
end
end
def _validate_number(number, name)
if not number.is_a?(Integer)
error "#{name} should be a number"
exit (1)
end
end
def _get_priority(options)
return Varnam::VARNAM_TOKEN_PRIORITY_NORMAL if options[:priority].nil? or options[:priority] == :normal
return Varnam::VARNAM_TOKEN_PRIORITY_LOW if options[:priority] == :low
return Varnam::VARNAM_TOKEN_PRIORITY_HIGH if options[:priority] == :high
_validate_number options[:priority], "priority"
return options[:priority]
end
def _get_accept_condition(options)
return Varnam::VARNAM_TOKEN_ACCEPT_ALL if options[:accept_if].nil? or options[:accept_if] == :all
return Varnam::VARNAM_TOKEN_ACCEPT_IF_STARTS_WITH if options[:accept_if] == :starts_with
return Varnam::VARNAM_TOKEN_ACCEPT_IF_IN_BETWEEN if options[:accept_if] == :in_between
return Varnam::VARNAM_TOKEN_ACCEPT_IF_ENDS_WITH if options[:accept_if] == :ends_with
_validate_number options[:accept_if], "accept_if"
end
def vowels(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_VOWEL, options)
end
def consonants(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_CONSONANT, options)
end
def period(p)
_create_token({"." => p}, Varnam::VARNAM_TOKEN_PERIOD, {})
end
def tag(name, &block)
_context.current_tag = name
block.call
_context.current_tag = nil
end
def consonant_vowel_combinations(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_CONSONANT_VOWEL, options)
end
def anusvara(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_ANUSVARA, options)
end
def visarga(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_VISARGA, options)
end
def virama(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_VIRAMA, options)
end
def symbols(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_SYMBOL, options)
end
def numbers(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_NUMBER, options)
end
def others(options={}, hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_OTHER, options)
end
def non_joiner(hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_NON_JOINER);
$overridden_default_symbols.push Varnam::VARNAM_TOKEN_NON_JOINER
end
def joiner(hash)
_ensure_sanity(hash)
_create_token(hash, Varnam::VARNAM_TOKEN_JOINER);
$overridden_default_symbols.push Varnam::VARNAM_TOKEN_JOINER
end
def get_tokens(token_type, criteria = {})
tokens = _context.tokens[token_type]
if criteria.empty?
return tokens
elsif criteria[:exact]
return tokens.find_all {|t| t.match_type == Varnam::VARNAM_MATCH_EXACT}
else
return tokens.find_all {|t| t.match_type == Varnam::VARNAM_MATCH_POSSIBILITY}
end
end
def get_vowels(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_VOWEL, criteria)
end
def get_consonants(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_CONSONANT, criteria)
end
def get_consonant_vowel_combinations(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_CONSONANT_VOWEL, criteria)
end
def get_anusvara(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_ANUSVARA, criteria)
end
def get_visarga(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_VISARGA, criteria)
end
def get_symbols(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_SYMBOL, criteria)
end
def get_numbers(criteria = {})
return get_tokens(Varnam::VARNAM_TOKEN_OTHER, criteria)
end
def get_virama
tokens = get_tokens(Varnam::VARNAM_TOKEN_VIRAMA, {})
if tokens.empty?
error 'Virama is not set'
exit (1)
end
return tokens[0]
end
def ffito_string(value)
str = ""
ptr = value.to_ptr
if not ptr.null?
str = ptr.read_string
str.force_encoding('UTF-8')
end
return str
end
def get_dead_consonants(criteria = {})
# dead consonants are infered by varnam. ruby wrapper don't know anything about it.
token_type = Varnam::VARNAM_TOKEN_DEAD_CONSONANT
token_ptr = FFI::MemoryPointer.new :pointer
done = VarnamLibrary.varnam_get_all_tokens($varnam_handle.get_pointer(0), token_type, token_ptr);
if done != 0
error_message = VarnamLibrary.varnam_get_last_error($varnam_handle.get_pointer(0))
error error_message
return
end
size = VarnamLibrary.varray_length(token_ptr.get_pointer(0))
i = 0
_context.tokens[token_type] = [] if _context.tokens[token_type].nil?
until i >= size
tok = VarnamLibrary.varray_get(token_ptr.get_pointer(0), i)
ptr = token_ptr.read_pointer
item = VarnamLibrary::Token.new(tok)
varnam_token = VarnamToken.new(item[:type],
ffito_string(item[:pattern]), ffito_string(item[:value1]),
ffito_string(item[:value2]), ffito_string(item[:value3]),
ffito_string(item[:tag]), item[:match_type])
_context.tokens[token_type].push(varnam_token)
i += 1
end
return get_tokens(token_type, criteria)
end
def print_warnings_and_errors
if _context.warnings > 0
_context.warning_messages.each do |msg|
puts msg
end
end
if _context.errors > 0
_context.error_messages.each do |msg|
puts msg
end
end
end
# Sets default symbols if user has not set overridden in the scheme file
def set_default_symbols
non_joiner "_" => "_" if not $overridden_default_symbols.include?(Varnam::VARNAM_TOKEN_NON_JOINER)
joiner "__" => "__" if not $overridden_default_symbols.include?(Varnam::VARNAM_TOKEN_JOINER)
symbols "-" => "-"
end
def start_compilation
puts "Compiling #{$options[:file_to_compile]}"
puts "Building #{$vst_file_name}"
at_exit {
print_warnings_and_errors if _context.errors > 0
puts "Completed with '#{_context.warnings}' warning(s) and '#{_context.errors}' error(s)"
}
load $options[:file_to_compile]
set_default_symbols
flush_unsaved_changes
set_scheme_details
if _context.errors > 0
returncode = 1
else
returncode = 0
end
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))