# Copyright 2017 Google Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Russian minimally supervised number grammar.
#
# Supports cardinals and ordinals in all inflected forms.
#
# The language-specific acceptor G was compiled with digit, teen, decade,
# century, and big power-of-ten preterminals. The lexicon transducer is
# highly ambiguous, but no LM is used.

import 'util/arithmetic.grm' as a;

# Intersects the universal factorization transducer (F) with language-specific
# acceptor (G).

d = a.DELTA_STAR;
f = a.IARITHMETIC_RESTRICTED;
g = LoadFst['ru/verbalizer/g.fst'];
fg = Optimize[d @ Optimize[f @ Optimize[f @ Optimize[f @ g]]]];
test1 = AssertEqual["230" @ fg, "(+ 200 30 +)"];

# Compiles lexicon transducers (L).

cardinal_name = StringFile['ru/verbalizer/cardinals.tsv'];
cardinal_l = Optimize[(cardinal_name " ")* cardinal_name];

ordinal_name = StringFile['ru/verbalizer/ordinals.tsv'];
ordinal_l = Optimize[(cardinal_name " ")* ordinal_name];

# Composes L with the leaf transducer (P), then composes that with FG.

p = a.LEAVES;

export CARDINAL_NUMBER_NAME = Optimize[fg @ (p @ cardinal_l)];

export ORDINAL_NUMBER_NAME = Optimize[fg @ (p @ ordinal_l)];
