Skip to content

Commit

Permalink
Added check for the existence of a source rule file, its specificatio…
Browse files Browse the repository at this point in the history
…n by default if not provided, and a simple check that at least one rule was found in the source rule file, otherwise aborting script; revised initial usage example.
  • Loading branch information
aarppe committed Jun 7, 2023
1 parent 8107e03 commit 8593000
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion devtools/ruletest/compile-rewrite-rules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# compile-rewrite-rules.sh 1: XFSCRIPT (path to) 2: FSTTYPE

# Usage:
# echo /Users/arppe/giella/langs/crk/src/fst/phonology.xfscript foma |
# ./compile-rewrite-rules.sh phonology.xfscript foma

# Print empty line to get to the END statement in the GAWK script.

Expand All @@ -30,6 +30,7 @@ gawk -v XFSCRIPT=$1 -v FSTTYPE=$2 'BEGIN { xfscript=XFSCRIPT; fsttype=FSTTYPE;
{
# print "Aborting <- specify FST type for phonological rule(s) among the following: 1) foma; 2) hfst; or 3) hfstol";
print "Aborting <- specify FST type for phonological rule(s) among the following: 1) foma; or 2) hfst";
abort=1;
exit;
}
}
Expand All @@ -39,6 +40,21 @@ gawk -v XFSCRIPT=$1 -v FSTTYPE=$2 'BEGIN { xfscript=XFSCRIPT; fsttype=FSTTYPE;
# In the case of multiple REGEX statements, in effect only use the last one.
# This presumes a GiellaLT approach to defining morphophonology.
if(xfscript=="")
{
print "Setting source rule file as \"phonology.xfscript\" by default" > "/dev/stderr/";
xfscript="phonology.xfscript"
}
# Checking that XFSCRIPT file exists
"if [ -f \"" xfscript "\" ]\nthen\n echo 1\nelse\necho 0\nfi" | getline exit_status;
if(exit_status!=1)
{
print "Aborting - rule file \"" xfscript "\" not found" > "/dev/stderr/";
abort=1;
exit;
}
while((getline < xfscript)!=0)
{ if(index($0,"regex")!=0)
{ rx=1; regex=""; }
Expand All @@ -51,6 +67,15 @@ gawk -v XFSCRIPT=$1 -v FSTTYPE=$2 'BEGIN { xfscript=XFSCRIPT; fsttype=FSTTYPE;
sub("[ ]*\\].*;.*$","",regex);
n=split(regex,rule,"[ ]*\\.o\\.[ ]*");
if(n==0)
{
print "Aborting - no rewrite rules found/defined in \"" xfscript "\" as expected" > "/dev/stderr/";
abort=1;
exit;
}
else
print "Found n=" n " rewrite rules in \"" xfscript "\"" > "/dev/stderr/";
# Create a new XFSCRIPT command sequence to compile and save all individual rewrite rule FSTs.
# As first XFSCRIPT command, 1) read in (and compile) original XFSCRIPT file as is.
Expand All @@ -68,6 +93,7 @@ gawk -v XFSCRIPT=$1 -v FSTTYPE=$2 'BEGIN { xfscript=XFSCRIPT; fsttype=FSTTYPE;
# First create a subdirectory named according to FSTTYPE.
END {
if(!abort) {
if(fsttype=="foma")
{
system("mkdir foma");
Expand All @@ -90,6 +116,7 @@ END {
system("hfst-fst2fst -O -i " hfst " -o " hfstol ";");
}
}
}
}'

# END #

0 comments on commit 8593000

Please sign in to comment.