/* LanguageTool, a natural language style checker 
 * Copyright (C) 2014 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.dev;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Simple command line tool to look up occurrence counts in an ngram index.
 * @since 2.7
 */
class NGramStats {

  private void lookup(File dir, String phrase) throws IOException {
    try (HomophoneOccurrenceDumper lm = new HomophoneOccurrenceDumper(dir)) {
      String[] tokens = phrase.split(" ");
      if (tokens.length > 3) {
        throw new RuntimeException("Phrases of length " + tokens.length + " are not yet supported: '" + phrase + "'");
      } else {
        long count = lm.getCount(Arrays.asList(tokens));
        System.out.println(phrase + ": " + count);
      }
    }
  }

  public static void main(String[] args) throws IOException {
    if (args.length != 1 && args.length != 2) {
      System.out.println("Usage: " + NGramStats.class.getSimpleName() + " <dir> <phrase>");
      System.out.println("  'dir' is a directory with '1grams' etc sub directories with a Lucene index of ngrams");
      System.out.println("  'phrase' is a 1 to 3 word case-sensitive phrase, e.g. \"the tall boy\" (include the quotes)");
      System.exit(1);
    }
    String dir = args[0];
    String phrase = args[1];
    NGramStats stats = new NGramStats();
    stats.lookup(new File(dir), phrase);
  }

}

@@ -934,16 +934,44 @@
 eption;%0A
+import java.nio.file.Files;%0A
 import j
@@ -982,16 +982,16 @@
 util.*;%0A
-
 %0A/**%0A * 
@@ -1127,31 +1127,53 @@
 kup(
-File dir, String
+HomophoneOccurrenceDumper lm, File
  phrase
+File
 ) th
@@ -1199,84 +1199,225 @@
     
-try (HomophoneOccurrenceDumper lm = new HomophoneOccurrenceDumper(dir)) %7B%0A  
+List%3CString%3E lines = Files.readAllLines(phraseFile.toPath());%0A    for (String line : lines) %7B%0A      lookup(lm, line);%0A    %7D%0A  %7D%0A%0A  private void lookup(HomophoneOccurrenceDumper lm, String phrase) throws IOException %7B%0A
     
@@ -1457,18 +1457,16 @@
 %22);%0A    
-  
 if (toke
@@ -1478,26 +1478,24 @@
 ngth %3E 3) %7B%0A
-  
       throw 
@@ -1603,18 +1603,16 @@
 %22);%0A    
-  
 %7D else %7B
@@ -1618,18 +1618,16 @@
 %7B%0A      
- 
- 
 long cou
@@ -1669,26 +1669,24 @@
 ns));%0A      
-  
 System.out.p
@@ -1696,39 +1696,30 @@
 tln(
-phrase
+count
  + %22
-:
  %22 + 
-count);%0A      %7D
+phrase);
 %0A   
@@ -2163,16 +2163,26 @@
  quotes)
+ or a file
 %22);%0A    
@@ -2248,16 +2248,22 @@
 g phrase
+OrFile
  = args%5B
@@ -2315,44 +2315,259 @@
     
-stats.lookup(new File(dir), phrase);
+File file = new File(phraseOrFile);%0A    try (HomophoneOccurrenceDumper lm = new HomophoneOccurrenceDumper(new File(dir))) %7B%0A      if (file.exists()) %7B%0A        stats.lookup(lm, file);%0A      %7D else %7B%0A        stats.lookup(lm, phraseOrFile);%0A      %7D%0A    %7D
 %0A  %7D

support a file (one word per line) as input