# 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.

import 'ru/verbalizer/number_names.grm' as n;
import 'universal/thousands_punct.grm' as t;
import 'util/byte.grm' as b;

nominatives = StringFile['ru/verbalizer/nominatives.tsv'];

sigma_star = b.kBytes*;

nominative_filter =
 CDRewrite[nominatives ("" : "" <-1>), "[BOS]" | " ", " " | "[EOS]", sigma_star]
;

cardinal = n.CARDINAL_NUMBER_NAME;
ordinal = n.ORDINAL_NUMBER_NAME;

# Putting these here since this grammar gets incorporated by all the others.

func I[expr] {
  return "" : expr;
}

func D[expr] {
  return expr : "";
}

# Since we know this is the default for Russian, it's fair game to set it.
separators = t.dot_thousands | t.no_delimiter;

export CARDINAL_NUMBERS = Optimize[
   separators
 @ cardinal
];

export ORDINAL_NUMBERS_UNMARKED = Optimize[
   separators
 @ ordinal
];


endings = StringFile['ru/verbalizer/ordinal_endings.tsv'];

not_dash = (b.kBytes - "-")+;
del_ending = CDRewrite[("-" not_dash) : "", "", "[EOS]", sigma_star];

# Needs nominative_filter here if we take out Kyle's models.
export ORDINAL_NUMBERS_MARKED = Optimize[
   Optimize[Optimize[separators @ ordinal] "-" not_dash]
 @ Optimize[sigma_star endings]
 @ del_ending]
;

export ORDINAL_NUMBERS =
  Optimize[ORDINAL_NUMBERS_MARKED | ORDINAL_NUMBERS_UNMARKED]
;
