#!/usr/local/bin/perl

open (READ, "../UNIHAN.TXT");
open (WRITE, ">NEWHAN.TXT");
$line=<READ>;
$oldfirstcol="U+4E00";
$df="kdf";
$ct="kct";
$bf="kbf";
$md="kmd";

while ($line ne ""){ 
	chop($line); 
	@column=split(/[\t]+/,$line);
	if($column[0] ne "#"){
		if ($column[0] eq $oldfirstcol){

			if ($column[1] =~ /kBigFive/){   
				$bf="kbf".$column[2];
			}
			if ($column[1] =~ /kCantonese/){
				$ct="kct".$column[2];
			}
			if ($column[1] =~ /kDefinition/){
				$df="kdf".$column[2];
			}
			if ($column[1] =~ /kMandarin/){
	
				$count1=0;
				$count2=0;
				@correctform1="";
				@correctform2="";

				@correctform1=split(/\s+/, $column[2]);
				while($correctform1[$count1] ne ""){
					if($correctform1[$count1] =~ /\D/){
						$correctform2[$count2]=$correctform1[$count1];
						$count2++;
					}
				$count1++;
				}

				$column[2]= join(" ",@correctform2);
				$md="kmd".$column[2];
			}
		}

		else{
			print WRITE "$bf\t$oldfirstcol\t$df\t$ct\t$md\n";
			$df="kdf";
			$ct="kct";
			$bf="kbf";
			$md="kmd";
			$oldfirstcol = $column[0];
		}
	}
	$line=<READ>;
}

close(READ);     
close(WRITE);  
chmod(0644, "NEWHAN.TXT");

