/**
 * A program to search an export file for singular normal nouns
 * and print the extracted nouns to a destination file.
 * 
 * WS10/11 - SelfTest 10
 *
 * Author: Marie Hinrichs, minor modifications by Frank Richter
 */
import java.util.*;
import java.io.*;

public class SearchCorpus
{
    
    public static void main(String[] args)
    {
        
        if (args.length != 2)
        {
            System.out.println("Usage: java SearchCorpus <corpus> <destfile>");
            System.exit(0);
        }
        
        BufferedReader src = null;
        PrintWriter dest = null;
        File destFile = null;
        
        Scanner keyboard = new Scanner(System.in);
        String line, word, tag, morph;
        String[] lineSplit;
        
        try
        {
            // Open source file for reading
            src = FileUtils.openBufferedReader(args[0]);
            
            // if destination file exists and is writeable,
            // ask if it's ok to overwrite.  Exit if not.
            destFile = new File(args[1]);
            if (destFile.exists())
            {
                System.out.println("File " + args[1] + " exists. Overwrite? (y/n): ");
                String answer = keyboard.nextLine();
                
                if (!answer.equalsIgnoreCase("y"))
                {
                    System.exit(0);
                }
            }
            
            // open destination file for overwriting
            dest = FileUtils.openPrintWriter(destFile);
            
            // process the file
            while ((line = src.readLine()) != null)
            {                
                // process lines that don't start with %% or #
                if (! (line.startsWith("%%") || line.startsWith("#")))
                {
                    line = line.trim();
                    lineSplit = line.split("\\s+");
                    
                    // read the first 3 fields
                    word = lineSplit[0];
                    tag = lineSplit[1];
                    morph = lineSplit[2];
                    
                    // write word to destination file if it's a singular normal noun
                    if (tag.equals("NN") && (morph.charAt(1) == 's'))
                    {
                        dest.println(word);
                    }
                }
            }
            
            // close input and output files
            src.close();
            dest.close();
        }
        catch (FileNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);        
        }
    }
}
