#!/usr/bin/perl -w

# Simplifies the presentation of the top-priority OL Dump publications.

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.

use Unicode::Normalize 'NFC';
# Import a function to normalize Unicode strings.

open IN, '<:utf8', 'booktop.txt';
# Open the input file for reading.

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

my ($i, $key, $lccls, @out, %out, @seg);

my $seq = 0;
# Initialize the sequence ID.

print OUT "NON-DONATIONS\n";
# Output the file header.

while (<IN>) {
# For each line of the input file:

	chomp;
	# Delete its trailing newline.

	@seg = (split /\t/);
	# Identify its segments.

	@out = ();
	# Reinitialize the output list.

	push @out, (($seg[4] =~ /"lc_classifications": \["([^"]+)"\]/), '')[0];
	# Identify its LC classification.

	push @out, (($seg[4] =~ /"title": "([^"]+)"/), '')[0];
	# Identify its title.

	push @out, (($seg[4] =~ /"by_statement": "([^"]+)"/), '')[0];
	# Identify its authorship.

	push @out, (($seg[4] =~ /"physical_format": "([^"]+)"/), '')[0];
	# Identify its physical format.

	push @out, (($seg[4] =~ /"publish_date": "([^"]+)"/), '')[0];
	# Identify its publication date.

	push @out, (($seg[4] =~ /"isbn_10": "([^"]+)"/), '')[0];
	# Identify its ISBN-10.

	push @out, (($seg[4] =~ /"isbn_13": "([^"]+)"/), '')[0];
	# Identify its ISBN-13.

	push @out, (($seg[4] =~ /"lccn": \["(\d+)"\]/), '')[0];
	# Identify its LCCN.

	push @out, (($seg[1] =~ m#/books/(.+)$#), '')[0];
	# Identify its OL ID.

	$key = ("$out[0] (" . $seq++ . ")");
	# Identify a unique key for it.

	$out{$key} = (&Norm (
		"\nlccls: $out[0]\ntitle: $out[1]\nauthr: $out[2]\nformt: $out[3]\npubyr: $out[4]\n"
		. "isb10: $out[5]\nisb13: $out[6]\nlccat: $out[7]\nolcat: $out[8]\n"
	));
	# Add it to a table of output lines.

}

$seq = 0;
# Reinitialize the sequence ID.

foreach $lccls (sort (keys %out)) {
# For each output line, in LC classification order:

	print OUT ("\nseqid: " . $seq++ . $out{$lccls});
	# Output it to the cleaned top-priority file.

}

close IN;
# Close the input file.

close OUT;
# Close the output file.

sub Norm {
# Normalize the specified string.
# Arguments:
#	0: string.

	my $ret = $_[0];
	# Identify the string.

	$ret =~ s/\\u(.{4})/chr (hex ($1))/eg;
	# Convert all of its Unicode hexadecimal encodings to characters.

	$ret = (NFC ($ret));
	# Apply NFC normalization to it.

	return $ret;
	# Return the converted string.

}
