#!/usr/bin/perl -w

use strict;
use utf8;
use POSIX;
use locale;
setlocale(LC_ALL, "de_DE.UTF-8");
use open ':encoding(utf8)';
binmode STDOUT, ":utf8";

my $infile = $ARGV[0];
open IN, "<$infile";                            # Text einlesen my @html = <IN>; close IN; my $html = join '\n', @html; my $hyphenator = new HTML::Hyphenate( style => 'german', default_lang => 'de_de'); my $hyphenated .= $hyphenator->hyphenated($html) #utf8::decode $html; #my $src = $text;            # Kopie für den Fortfinder #my %liste = liste(); ## Das soll beliebte Trennfehler verhindern.  #map{
my @tmp = <IN>;
close IN;

my $t = join "\n",  @tmp;

my @wl  = erstelle_wortliste();
my @src = oeffne_greplist();

map {
    my $pattern = $_;
    my @matches = grep(/^$pattern;/i, @src);
    my $n = @matches;               # Anzahl der Treffer,  idealerweise 1
    if ($n == 0){                   # Neue Worte
        print "NEU: ", $pattern, "\n" if $n == 0;   # Ausgabe nach STDOUT
        open NEU, ">>/home/gp/workspace/silben/neue_worte";
        print NEU $pattern, "\n" if $n == 0;
        close NEU;
    }

    elsif ($n != 0){                # Bekannte Worte
        my $match   = $matches[0];
        $match =~ s/^[^;]+;(?|-\d-;|)([^;]+).*/$1/;
        $match =~ s/[\.\|=·]+/-/g;
        $match =~ s/\{(.+?)\/.+?\}/$1/g;
        print $match;
    }
} @wl;

sub erstelle_wortliste {
    my $tmp = $t;

    $tmp =~ s/<[^<>]+>//g;
    $tmp =~ s/[-–_0-9©»«,\.! \(\):;„“‚‘#<>\/'"]+/ /g;
    $tmp =~ s/ [IVXLCMD]+ / /g;

    my %tmp;

    foreach ($tmp =~ /\S+/g){
        $tmp{$_} = $_ if length($_) > 3;
    }
    return (sort keys(%tmp));
}

sub oeffne_greplist {
    my @lists   = ("/home/gp/workspace/wortliste/wortliste", 
        "/home/gp/workspace/wortliste/pre-1901.wl", 
        "/home/gp/workspace/silben/wortliste");

    open IN, "</home/gp/workspace/wortliste/wortliste";    # allg. Liste
    my @tmp = <IN>;
    close IN;

    open IN, "</home/gp/workspace/wortliste/pre-1901.wl";  # Wortliste pre-1901
    push (@tmp, $_) while <IN>;
    close IN;
 
    open IN, "</home/gp/workspace/silben/wortliste";        # lokale Wortliste
    push (@tmp, $_) while <IN>;
    close IN;
 
    return @tmp;
}
