Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No public description #471

Closed
wants to merge 1 commit into from
Closed
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
1 change: 0 additions & 1 deletion nisaba/scripts/brahmic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ py_library(
"@org_opengrm_pynini//pynini",
"@org_opengrm_pynini//pynini/lib:byte",
"@rules_python//python/runfiles",
requirement("pandas"),
],
)

Expand Down
1 change: 0 additions & 1 deletion nisaba/scripts/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ py_library(
":rewrite",
"@org_opengrm_pynini//pynini",
requirement("networkx"),
requirement("pandas"),
],
)

Expand Down
39 changes: 27 additions & 12 deletions nisaba/scripts/utils/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@
CDRewrites of the FSTs from each rule.
"""

import csv
import itertools as it
import os
from typing import Iterable, Iterator, List, NamedTuple

import networkx as nx
import pandas as pd

import pynini
import pathlib
import nisaba.scripts.utils.file as uf
import nisaba.scripts.utils.rewrite as ur


Rule = NamedTuple('Rule', [('lhs', str), ('rhs', str)])
RuleSet = Iterable[Rule]
RuleSets = List[RuleSet]
Expand All @@ -84,12 +85,20 @@ def rules_from_string_file(file: os.PathLike) -> Iterator[Rule]:
def rules_from_string_path(file: os.PathLike) -> Iterator[Rule]:
"""Yields string rules from a text file with unweighted string maps."""
with pathlib.Path(file).open('rt') as f:
df = pd.read_csv(f, sep='\t', comment='#', escapechar='\\',
names=['lhs', 'rhs'], na_filter=False)
for row in df.itertuples(index=False, name='Rule'):
if not row.lhs:
csv_reader = csv.reader(
(row for row in f if not row.startswith('#')),
delimiter='\t',
escapechar='\\',
)
for row in csv_reader:
if not row:
continue
if not row[0]:
raise ValueError('Rule expects an LHS: {}'.format(row))
yield row
rhs = ''
if len(row) > 1:
rhs = row[1]
yield Rule(lhs=row[0], rhs=rhs)


def _match_lhs_in_lhs(rule_a: Rule, rule_b: Rule) -> bool:
Expand All @@ -115,8 +124,11 @@ def partition_unordered(rules: RuleSet) -> List[RuleSet]:

g = nx.DiGraph()
g.add_nodes_from(rules)
g.add_edges_from((p1, p2) for p1, p2 in it.product(rules, rules)
if p1 != p2 and _match_lhs_in_lhs(p1, p2))
g.add_edges_from(
(p1, p2)
for p1, p2 in it.product(rules, rules)
if p1 != p2 and _match_lhs_in_lhs(p1, p2)
)
partition = []
while g.number_of_nodes() > 0:
leaves = [node for node in g.nodes if g.out_degree(node) == 0]
Expand All @@ -138,8 +150,10 @@ def fst_from_rules(rules: RuleSet, sigma: pynini.Fst) -> pynini.Fst:
The Rewrite FST for the specified rule file.
"""

fsts = [pynini.optimize(pynini.string_map(rule_set))
for rule_set in partition_unordered(rules)]
fsts = [
pynini.optimize(pynini.string_map(rule_set))
for rule_set in partition_unordered(rules)
]
return ur.RewriteAndComposeFsts(fsts, sigma)


Expand Down Expand Up @@ -172,8 +186,9 @@ def _fst_from_cascading_rules(rules: RuleSet, sigma: pynini.Fst) -> pynini.Fst:
return ur.RewriteAndComposeFsts(fsts, sigma)


def fst_from_cascading_rule_file(rule_file: os.PathLike,
sigma: pynini.Fst) -> pynini.Fst:
def fst_from_cascading_rule_file(
rule_file: os.PathLike, sigma: pynini.Fst
) -> pynini.Fst:
"""Gets rewrite FST from a given rewrite rule file.

Args:
Expand Down
Loading