2015-01-02 19:12:01 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
#
|
|
|
|
# Setup
|
|
|
|
#
|
|
|
|
|
|
|
|
# Directives
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use utf8;
|
|
|
|
|
|
|
|
binmode(STDOUT, ":utf8");
|
|
|
|
binmode(STDERR, ":utf8");
|
|
|
|
|
|
|
|
# Modules
|
|
|
|
use File::Basename;
|
2017-05-10 00:05:40 +00:00
|
|
|
use Getopt::Long;
|
|
|
|
|
|
|
|
# Variables
|
|
|
|
my $EXT = ".markdown";
|
|
|
|
my $REPO_DIR = dirname($0) . "/..";
|
|
|
|
my $BUILD_DIR = "$REPO_DIR/build";
|
|
|
|
|
|
|
|
&GetOptions(
|
|
|
|
"output|o=s" => \$BUILD_DIR,
|
|
|
|
"ext|e=s" => \$EXT,
|
|
|
|
);
|
2015-01-02 19:12:01 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Paths
|
|
|
|
#
|
|
|
|
|
|
|
|
# These scripts are designed to work within the Git repository and
|
|
|
|
# makes assumptions of all the relative paths and outputs.
|
2017-05-10 00:05:40 +00:00
|
|
|
my $DICT_MARKDOWN = "$BUILD_DIR/index.markdown";
|
2017-05-09 22:44:45 +00:00
|
|
|
my $DICT_DIR = "$REPO_DIR/src/dictionary";
|
2018-03-10 03:30:04 +00:00
|
|
|
|
2015-01-02 19:12:01 +00:00
|
|
|
# Make sure the build directory exists.
|
|
|
|
unless (-d $BUILD_DIR)
|
|
|
|
{
|
|
|
|
print STDERR "Creating build directory\n";
|
2017-05-17 04:34:31 +00:00
|
|
|
system("mkdir", "-p", $BUILD_DIR);
|
2015-01-02 19:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Processing
|
|
|
|
#
|
|
|
|
|
|
|
|
# Create the initial Markdown file.
|
2017-05-10 00:05:40 +00:00
|
|
|
my ($DICT, $SYB);
|
|
|
|
print STDERR "Writing to $DICT_MARKDOWN\n";
|
|
|
|
open $DICT, ">:encoding(UTF-8)", $DICT_MARKDOWN
|
2015-01-02 19:12:01 +00:00
|
|
|
or die "Cannot write dictionary file ($!)";
|
|
|
|
|
|
|
|
# Write out the front matter.
|
2017-05-10 00:05:40 +00:00
|
|
|
print $DICT join(
|
2015-01-02 19:12:01 +00:00
|
|
|
"\n",
|
|
|
|
"---",
|
|
|
|
"title: Miwāfu Dictionary",
|
2017-05-10 00:05:40 +00:00
|
|
|
"breadcrumbTitle: Dictionary",
|
2015-01-02 19:12:01 +00:00
|
|
|
"---"), "\n\n";
|
|
|
|
|
|
|
|
# Loop through the directories in the dictionary.
|
|
|
|
for my $s (sort(glob("$DICT_DIR/*")))
|
|
|
|
{
|
|
|
|
# Figure out the basename.
|
|
|
|
my $bs = basename($s);
|
|
|
|
|
2017-05-10 00:05:40 +00:00
|
|
|
# Open up the syllable file.
|
|
|
|
open $SYB, ">:encoding(UTF-8)", "$BUILD_DIR/$bs.markdown"
|
|
|
|
or die "Cannot write $bs dictionary file ($!)";
|
|
|
|
|
2015-01-02 19:12:01 +00:00
|
|
|
# Determine if we have any entries in here.
|
|
|
|
my @w = sort(glob("$s/*.markdown"));
|
|
|
|
next unless @w;
|
|
|
|
my $w = scalar(@w);
|
|
|
|
|
|
|
|
# Write out the entry.
|
|
|
|
print STDERR "Processing: $bs ($w entries)\n";
|
2017-05-10 00:05:40 +00:00
|
|
|
print $DICT "# $bs\n\n";
|
2018-03-10 03:30:04 +00:00
|
|
|
|
2017-05-10 01:37:15 +00:00
|
|
|
print $SYB "---\ntitle: Miwāfu Dictionary - $bs\nbreadcrumbTitle: $bs\n---\n\n# $bs\n\n";
|
2015-01-02 19:12:01 +00:00
|
|
|
|
|
|
|
# Go through each of these entries.
|
|
|
|
for $w (@w)
|
|
|
|
{
|
2017-05-10 00:05:40 +00:00
|
|
|
process_word($w, $bs);
|
2015-01-02 19:12:01 +00:00
|
|
|
}
|
2017-05-10 00:05:40 +00:00
|
|
|
|
|
|
|
# Close the file.
|
|
|
|
close $SYB;
|
2015-01-02 19:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Finish up the dictionary.
|
2017-05-10 00:05:40 +00:00
|
|
|
close $DICT;
|
2015-01-02 19:12:01 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Finished
|
|
|
|
#
|
|
|
|
|
|
|
|
print STDERR "Done\n";
|
|
|
|
|
|
|
|
#
|
|
|
|
# Subroutines
|
|
|
|
#
|
|
|
|
|
|
|
|
sub process_word
|
|
|
|
{
|
|
|
|
# Pull out the entries from the file.
|
2017-05-10 00:05:40 +00:00
|
|
|
my ($file, $bs) = @_;
|
2015-01-02 19:12:01 +00:00
|
|
|
|
|
|
|
# Read in this file and process the entries.
|
|
|
|
open WORD, "<:encoding(UTF-8)", $file;
|
|
|
|
|
|
|
|
# The format of the file matches Wikionary's format which is not
|
|
|
|
# how most dictionaries are created.
|
|
|
|
my $pos = undef;
|
|
|
|
my $word = undef;
|
|
|
|
my %defs = ();
|
|
|
|
|
|
|
|
while (<WORD>)
|
|
|
|
{
|
|
|
|
# Clean up the line.
|
|
|
|
chomp;
|
|
|
|
next if /^\s*$/;
|
|
|
|
next if /^=/;
|
|
|
|
next if /^-/;
|
|
|
|
|
|
|
|
# Figure out the parts of speech.
|
|
|
|
if (m@(Noun|Verb|Marker|Pronoun|Adjective|Adverb)@)
|
|
|
|
{
|
|
|
|
$pos = lc($1);
|
|
|
|
$pos = "adv" if $pos eq "adverb";
|
|
|
|
$pos = "adj" if $pos eq "adjective";
|
|
|
|
$pos = "pro" if $pos eq "pronoun";
|
|
|
|
$pos = "mark" if $pos eq "marker";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m@^Related$@)
|
|
|
|
{
|
|
|
|
$pos = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If we haven't hit a POS, then skip it.
|
|
|
|
next unless defined $pos;
|
|
|
|
|
|
|
|
# If we have a number in the beginning, then it's a definition.
|
|
|
|
if (m@^(\d+)\. (.*?)$@)
|
|
|
|
{
|
|
|
|
# Make sure we have an entry here.
|
|
|
|
push @{$defs{$word}{$pos}}, "**$1** $2";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Anything else is a word to add.
|
|
|
|
my %def_pos = ();
|
|
|
|
my @def_list = ();
|
|
|
|
|
2017-05-10 01:37:15 +00:00
|
|
|
s@^\#+\s+@@;
|
2015-01-02 19:12:01 +00:00
|
|
|
$word = $_;
|
|
|
|
$defs{$word} = \%def_pos unless defined $defs{$word};
|
|
|
|
$defs{$word}{$pos} = \@def_list unless defined $defs{$word}{$pos};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Finish up the file.
|
|
|
|
close WORD;
|
|
|
|
|
|
|
|
# Write out the Markdown line.
|
|
|
|
foreach $word (sort(keys(%defs)))
|
|
|
|
{
|
|
|
|
# Start by formatting the word.
|
2017-05-10 00:05:40 +00:00
|
|
|
my $slug = $word;
|
2018-03-10 03:30:04 +00:00
|
|
|
$slug =~ s/[áàā]/a/g;
|
|
|
|
$slug =~ s/[éèē]/e/g;
|
|
|
|
$slug =~ s/[íìī]/i/g;
|
|
|
|
$slug =~ s/[óòō]/o/g;
|
|
|
|
$slug =~ s/[úùū]/u/g;
|
|
|
|
|
2017-05-10 00:05:40 +00:00
|
|
|
print $DICT "[$word]($bs/$slug$EXT):";
|
|
|
|
print $SYB "[$word]($slug$EXT):";
|
|
|
|
|
|
|
|
my $buffer = "";
|
2015-01-02 19:12:01 +00:00
|
|
|
|
|
|
|
# Add in the parts of speech.
|
|
|
|
for $pos (qw(noun verb adj adv pro mark))
|
|
|
|
{
|
|
|
|
# If we don't have one, skip it.
|
|
|
|
next unless exists $defs{$word}{$pos};
|
|
|
|
|
|
|
|
# Add in the POS.
|
|
|
|
$buffer .= " *$pos* ";
|
|
|
|
|
|
|
|
# Go through the definitions.
|
|
|
|
$buffer .= join(" ", @{$defs{$word}{$pos}});
|
|
|
|
}
|
|
|
|
|
2017-05-10 00:05:40 +00:00
|
|
|
print $DICT "$buffer\n\n";
|
|
|
|
print $SYB "$buffer\n\n";
|
2015-01-02 19:12:01 +00:00
|
|
|
}
|
|
|
|
}
|