#!/usr/bin/perl

open(OUT, ">./love.txt"); binmode(OUT, ":utf8");
print OUT "{ \"type\": \"FeatureCollection\", \"features\": ["; $wrote = 0;

open(FILE, "./2018-mapping-the-geography-of-language-1gram-geographic-dataset.csv"); binmode(FILE, ":utf8");
while(<FILE>) {
    $_=~s/\s+$//;
    ($rank, $word, $gagg, $loc, $lat, $long, $cnt) = $_=~/^(\d+),([^,]+),([^,]+),["]*(.*?)["]*,([0-9\.\-]+),([0-9\.\-]+),(\d+)$/;
    if ($word eq 'love') { 
    	#print OUT "($loc)($lat)($long)($cnt)\n";
	
	if ($wrote++ > 1) { print OUT ",\n"; }
	print OUT "{ \"type\": \"Feature\", \"properties\": { \"name\": \"$loc\", \"count\": $cnt }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [$long, $lat] } }\n";
	if ($wrote==1) { print OUT ",\n"; }
	
	
    }
    if ($counter++ % 100000 == 0) { print "\tProcessed $counter...\n"; }
}
close(FILE);

print OUT "]};\n";
close(OUT);
