#!/usr/bin/perl -w

# Extracts the subjects from the Open Library catalog.

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 IN, '<:utf8', 'ol_dump_2012-04-30.txt';
# Open the input file for reading.

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

my ($numpr, @seg, %seg, $seg);

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

	if (s/^.+?, "subjects": \["/"/) {
	# Delete the part before the subject list. If anything was deleted:

		if (s/"\].+$/"/) {
		# Delete the part after the subject list before the trailing newline. If anything was deleted:

			chomp;
			# Delete the trailing newline.

			if ((s/^"|"$//g) == 2) {
			# Delete the leading and trailing quotation marks. If both were deleted:

				@seg = (split /", "/);
				# Identify a list of the subjects.

				foreach $seg (@seg) {
				# For each of them:

					$seg =~ s/\.$//;
					# Delete any trailing period.

					$seg =~ s/ *-- *| +- +/ -- /g;
					# Collapse any multiple spaces around single- or double-hyphen-minus element
					# delimiters, and convert any space-enclosed single hyphen-minuses to space-
					# enclosed double hyphen-minuses.

					if (exists $seg{$seg}) {
					# If the subject has already been encountered:

						$seg{$seg}++;
						# Increment the count of its occurrences.

					}

					else {
					# Otherwise, i.e. if it has not been encountered yet:

						$seg{$seg} = 1;
						# Initialize the count of its occurrences.

					}

				}

			}

		}

	}

}

foreach $seg (sort (keys %seg)) {
# For each subject that has been encountered:

	$numpr = (sprintf '%8u', $seg{$seg});
	# Identify its count as a right-justified integer.

	print OUT "$numpr: $seg\n";
	# Output the subject, preceded by its count.

}

close IN;
# Close the input file.

close OUT;
# Close the output file.