#!/usr/bin/perl -w

# Corrects dictionary subjects per Colowick lists.

use warnings 'FATAL', 'all';
# Make every warning fatal.

use strict;
# Require strict checking of variable references, etc.

use utf8;
# Make Perl interpret the script and standard files as UTF-8 rather than bytes.

open BADPOS, '<:utf8', 'possub-not.txt';
# Open the list of bad positives file for reading.

open BADNEG, '<:utf8', 'negsub-not.txt';
# Open the list of bad negatives file for reading.

open POSIN, '<:utf8', 'possubs.txt';
# Open the positive file for reading.

open NEGIN, '<:utf8', 'negsubs.txt';
# Open the negative file for reading.

open OUT, '>:utf8', 'goodsubs.txt';
# Create or truncate an output file and open it for writing.

my (%badpos, %badneg, %good, $it);

while (<BADPOS>) {
# For each bad positive subject:

	chomp;
	# Delete its trailing newline.

	s/^.+?: +//;
	# Delete its count prefix.

	$badpos{$_} = '';
	# Add it to the table of bad positive subjects.

}

while (<BADNEG>) {
# For each bad negative subject:

	chomp;
	# Delete its trailing newline.

	s/^.+?: +//;
	# Delete its count prefix.

	$badneg{$_} = '';
	# Add it to the table of bad negative subjects.

}

while (<POSIN>) {
# For each positive subject:

	$it = $_;
	# Identify a copy of it.

	chomp $it;
	# Delete the copy’s trailing newline.

	$it =~ s/^.+?: +//;
	# Delete the copy’s count prefix.

	unless (exists $badpos{$it}) {
	# If it is not bad (i.e. if it is in fact useful):

		$good{$it} = '';
		# Add it to the table of good subjects.

		print OUT (&Format ($_));
		# Output it, tab-delimited.

	}

}

while (<NEGIN>) {
# For each negative subject:

	$it = $_;
	# Identify a copy of it.

	chomp $it;
	# Delete the copy’s trailing newline.

	$it =~ s/^.+?: +//;
	# Delete the copy’s count prefix.

	if ((exists $badneg{$it}) && (! (exists $good{$it}))) {
	# If it is bad (i.e. useful) and it has not yet been output:

		print OUT (&Format ($_));
		# Output it, tab-delimited.

	}

}

close BADPOS;
# Close the bad positives file.

close BADNEG;
# Close the bad negatives file.

close POSIN;
# Close the positive input file.

close NEGIN;
# Close the negative input file.

close OUT;
# Close the output file.

sub Format {
# Reformat a subject line into 2 tab-delimited values.
# Arguments:
#	0: subject line.

	my @seg = ($_[0] =~ /^ *(\d+): (.+)$/);
	# Identify a list of the count and the subject.

	return (join "\t", @seg), "\n";
	# Return them, tab-delimited, and a newline.

}
