Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions nisaba/interim/grm2/rewrite/base_rule_cascade.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ bool BaseRuleCascade<Arc>::TopRewrite(absl::string_view input,
debug->clear();
std::vector<Label> labels;
if (!TopRewrite(input, &labels)) return false;
if (!::fst::LabelsToString(labels, output, token_type_)) return false;
if (!::fst::LabelsToString<Label>(labels, output, token_type_)) return false;
LabelsToDebugString(*output, labels, debug);
return true;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ bool BaseRuleCascade<Arc>::OneTopRewrite(absl::string_view input,
debug->clear();
std::vector<Label> labels;
if (!OneTopRewrite(input, &labels)) return false;
if (!::fst::LabelsToString(labels, output, token_type_)) return false;
if (!::fst::LabelsToString<Label>(labels, output, token_type_)) return false;
LabelsToDebugString(*output, labels, debug);
return true;
}
Expand Down Expand Up @@ -332,7 +332,7 @@ bool BaseRuleCascade<Arc>::TopRewrites(absl::string_view input,
for (const auto &labels : labelss) {
output->emplace_back();
debug->emplace_back();
if (!::fst::LabelsToString(labels, &output->back(), token_type_)) {
if (!::fst::LabelsToString<Label>(labels, &output->back(), token_type_)) {
return false;
}
LabelsToDebugString(output->back(), labels, &debug->back());
Expand Down Expand Up @@ -394,7 +394,7 @@ bool BaseRuleCascade<Arc>::TopRewrites(absl::string_view input,
for (const auto &labels : labelss) {
output->emplace_back();
debug->emplace_back();
if (!::fst::LabelsToString(labels, &output->back(), token_type_)) {
if (!::fst::LabelsToString<Label>(labels, &output->back(), token_type_)) {
return false;
}
LabelsToDebugString(output->back(), labels, &debug->back());
Expand Down Expand Up @@ -455,7 +455,7 @@ bool BaseRuleCascade<Arc>::Rewrites(absl::string_view input,
for (const auto &labels : labelss) {
output->emplace_back();
debug->emplace_back();
if (!::fst::LabelsToString(labels, &output->back(), token_type_)) {
if (!::fst::LabelsToString<Label>(labels, &output->back(), token_type_)) {
return false;
}
LabelsToDebugString(output->back(), labels, &debug->back());
Expand Down
2 changes: 1 addition & 1 deletion nisaba/interim/grm2/thrax/rewrite-tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ bool RewriteTester::AppendLabel(Label label, std::string *path) const {
std::string utf8_string;
std::vector<Label> labels;
labels.push_back(label);
if (!LabelsToUTF8String(labels, &utf8_string)) {
if (!LabelsToUTF8String<Label>(labels, &utf8_string)) {
LOG(ERROR) << "LabelsToUTF8String: Bad code point: " << label;
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion nisaba/interim/grm2/thrax/walker/util/function/symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>

#include "absl/synchronization/mutex.h"
#include "fst/icu.h"
#include "fst/string.h"

namespace thrax {
Expand Down Expand Up @@ -194,7 +195,7 @@ void SymbolTableBuilder::GenerateUtf8SymbolTable() {
std::vector<int> labels;
labels.push_back(c);
std::string utf8_label;
if (::fst::LabelsToUTF8String(labels, &utf8_label)) {
if (::fst::LabelsToUTF8String<int>(labels, &utf8_label)) {
if (IsUnicodeSpaceOrControl(c)) {
char c_str[7];
snprintf(c_str, sizeof(c_str), "0x%04x", c);
Expand Down
1 change: 1 addition & 0 deletions nisaba/scripts/abjad_alphabet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import string

import pynini

from nisaba.scripts.abjad_alphabet import util as u
from nisaba.scripts.utils import far
from nisaba.scripts.utils import rewrite
Expand Down
1 change: 1 addition & 0 deletions nisaba/scripts/abjad_alphabet/fst_properties_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import itertools

import pynini

from absl.testing import absltest
from absl.testing import parameterized
from nisaba.scripts.abjad_alphabet import util as u
Expand Down
4 changes: 2 additions & 2 deletions nisaba/scripts/abjad_alphabet/nfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
To try for Urdu:

```sh
bazel build -c opt nlp/grm2/thrax:rewrite-tester \
bazel build -c opt third_party/opengrm/thrax:rewrite-tester \
nisaba/scripts/abjad_alphabet:nfc

bazel-bin/nisaba/interim/grm2/thrax/rewrite-tester \
Expand All @@ -28,7 +28,7 @@
"""

import pynini
from pynini.export import multi_grm
from opengrm.pynini.export import multi_grm
from nisaba.scripts.abjad_alphabet import util as u
from nisaba.scripts.utils import rule

Expand Down
5 changes: 2 additions & 3 deletions nisaba/scripts/abjad_alphabet/reading_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
To try for Urdu:

```sh
bazel build -c opt nlp/grm2/thrax:rewrite-tester \
bazel build -c opt third_party/opengrm/thrax:rewrite-tester \
nisaba/scripts/abjad_alphabet:reading_norm

bazel-bin/nisaba/interim/grm2/thrax/rewrite-tester \
Expand All @@ -29,9 +29,8 @@
"""

from absl import flags

import pynini
from pynini.export import grm
from opengrm.pynini.export import grm
import nisaba.scripts.abjad_alphabet.util as u
from nisaba.scripts.utils import rule

Expand Down
4 changes: 2 additions & 2 deletions nisaba/scripts/abjad_alphabet/reading_norm_utf8.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
To try for Urdu:

```sh
bazel build -c opt nlp/grm2/thrax:rewrite-tester \
bazel build -c opt third_party/opengrm/thrax:rewrite-tester \
nisaba/scripts/abjad_alphabet:reading_norm

bazel-bin/nisaba/interim/grm2/thrax/rewrite-tester \
Expand All @@ -28,7 +28,7 @@
```
"""

from pynini.export import grm
from opengrm.pynini.export import grm
from nisaba.scripts.abjad_alphabet import reading_norm


Expand Down
4 changes: 2 additions & 2 deletions nisaba/scripts/abjad_alphabet/reversible_roman.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

```shell
ABJAD_ALPHABET_DIR=nisaba/scripts/abjad_alphabet
bazel build -c opt nlp/grm2/thrax:rewrite-tester \
bazel build -c opt third_party/opengrm/thrax:rewrite-tester \
${ABJAD_ALPHABET_DIR}:reversible_roman

bazel-bin/nisaba/interim/grm2/thrax/rewrite-tester \
Expand All @@ -29,7 +29,7 @@
"""

import pynini
from pynini.export import multi_grm
from opengrm.pynini.export import multi_grm
from nisaba.scripts.abjad_alphabet import util
from nisaba.scripts.utils import file
from nisaba.scripts.utils import rewrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# proto-file: testing/testdata.proto
# proto-file: third_party/opengrm/testing/testdata.proto
# proto-message: Rewrites

# Reading normalization test data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# proto-file: testing/testdata.proto
# proto-file: third_party/opengrm/testing/testdata.proto
# proto-message: Rewrites

# Unit tests for visual norm of abjad / alphabet script languages.
Expand Down
4 changes: 2 additions & 2 deletions nisaba/scripts/abjad_alphabet/visual_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
To try for Urdu:

```sh
bazel build -c opt nlp/grm2/thrax:rewrite-tester \
bazel build -c opt third_party/opengrm/thrax:rewrite-tester \
nisaba/scripts/abjad_alphabet:visual_norm_byte

bazel-bin/nisaba/interim/grm2/thrax/rewrite-tester \
Expand All @@ -31,7 +31,7 @@

from absl import flags
import pynini
from pynini.export import grm
from opengrm.pynini.export import grm
from nisaba.scripts.abjad_alphabet import visual_norm_common
import nisaba.scripts.abjad_alphabet.util as u
from nisaba.scripts.utils import rewrite
Expand Down
60 changes: 55 additions & 5 deletions nisaba/scripts/brahmic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load("@rules_proto//proto:defs.bzl", "proto_library")

load("@pip_deps//:requirements.bzl", "requirement")

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(
"//nisaba/scripts/brahmic:constant.bzl",
"FIXED_RULE_SCRIPTS",
Expand Down Expand Up @@ -89,7 +90,45 @@ nisaba_compile_multi_grm_py(
"//nisaba/scripts/utils:file",
"//nisaba/scripts/utils:rewrite",
"@org_opengrm_pynini//pynini",
"@org_opengrm_pynini//pynini/lib:pynutil",
"@org_opengrm_pynini//pynini/pynini/lib:pynutil",
],
)

nisaba_compile_multi_grm_py(
name = "iso_sttable",
src = "iso.py",
outs = {
"byte": "iso_sttable.far",
"utf8": "iso_sttable_utf8.far",
},
data = [
":nfc.far",
":nfc_utf8.far",
":sigma_utf8.far",
] + [
"//nisaba/scripts/brahmic/data/{}:{}.tsv".format(script, file_name)
for script in SCRIPTS
for file_name in [
"coda",
"consonant",
"dead_consonant",
"inherent_vowel",
"standalone",
"subjoined_consonant",
"virama",
"vowel",
"vowel_length_sign",
"vowel_sign",
]
] + ["//nisaba/scripts/brahmic/data:symbol.tsv"],
far_type = "sttable",
visibility = ["//visibility:public"],
deps = [
":util",
"//nisaba/scripts/utils:file",
"//nisaba/scripts/utils:rewrite",
"@org_opengrm_pynini//pynini",
"@org_opengrm_pynini//pynini/pynini/lib:pynutil",
],
)

Expand Down Expand Up @@ -173,12 +212,13 @@ nisaba_grm_textproto_test(
py_library(
name = "util",
srcs = ["util.py"],
visibility = ["//visibility:public"],
deps = [
":script_config_py_pb2",
"//nisaba/scripts/utils:file",
"@com_google_protobuf//:protobuf_python",
"//third_party/py/google/protobuf",
"@org_opengrm_pynini//pynini",
"@org_opengrm_pynini//pynini/lib:byte",
"@org_opengrm_pynini//pynini/pynini/lib:byte",
"@rules_python//python/runfiles",
requirement("pandas"),
],
Expand Down Expand Up @@ -223,13 +263,14 @@ py_library(
"//nisaba/scripts/utils:char",
"//nisaba/scripts/utils:rewrite",
"@org_opengrm_pynini//pynini",
"@org_opengrm_pynini//pynini/lib:pynutil",
"@org_opengrm_pynini//pynini/pynini/lib:pynutil",
],
)

nisaba_compile_grm_py(
name = "sigma",
out = "sigma_utf8.far",
visibility = ["//visibility:public"],
deps = [
":char_util",
":util",
Expand Down Expand Up @@ -321,7 +362,7 @@ nisaba_compile_script_lang_multi_grm_py(
"//nisaba/scripts/utils:rule",
"@io_abseil_py//absl/flags",
"@org_opengrm_pynini//pynini",
"@org_opengrm_pynini//pynini/lib:pynutil",
"@org_opengrm_pynini//pynini/pynini/lib:pynutil",
],
)

Expand Down Expand Up @@ -422,6 +463,7 @@ py_library(
":iso.far",
":nfc.far",
":reading_norm.far",
":sigma_utf8.far",
":visual_norm.far",
":wellformed.far",
] + [
Expand Down Expand Up @@ -492,6 +534,7 @@ cc_library(
"//nisaba/interim/grm2/thrax:grm-manager-lib",
"//nisaba/port:file_util",
"//nisaba/port:status_macros",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -528,3 +571,10 @@ filegroup(
":wellformed.far",
],
)

bzl_library(
name = "constant_bzl",
srcs = ["constant.bzl"],
parse_tests = False,
visibility = ["//visibility:private"],
)
22 changes: 22 additions & 0 deletions nisaba/scripts/brahmic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import string

import pynini

from nisaba.scripts.brahmic import util as u
from nisaba.scripts.utils import far

Expand Down Expand Up @@ -58,6 +59,10 @@ def visual_norm(self) -> far.Far:
def wellformed(self) -> far.Far:
return far.Far(u.FAR_DIR / 'wellformed.far')

@functools.cached_property
def sigma(self) -> far.Far:
return far.Far(u.FAR_DIR / 'sigma_utf8.far')


_FARS = _FarStore()

Expand All @@ -82,6 +87,23 @@ def WellFormed(script: str) -> far.Far.FstWrapper:
return _FARS.wellformed.Fst(script.upper())


def Sigma(script: str = 'Brahmic') -> far.Far.FstWrapper:
"""Returns a sigma-star acceptor for a script.

The returned FstWrapper accepts any word composed entirely of
characters from the given script's alphabet. If no script is
specified, accepts words from any supported Brahmic script.

Args:
script: ISO 15924 script code (e.g. 'Mlym', 'Deva') or 'Brahmic' for the
union of all scripts. Defaults to 'Brahmic'.

Returns:
An FstWrapper with AcceptText() support.
"""
return _FARS.sigma.Fst(script.upper() + '_STAR', token_type='utf8')


class ScriptError(ValueError):
pass

Expand Down
Loading
Loading