public Bigrams(String bigramARPAFileName){
  ugrams=new HashMap<String,Tuple2<Double,Double>>();
  bgrams=new HashMap<Tuple2<String,String>,Double>();
  BufferedReader br=null;
  try {
    br=new BufferedReader(new InputStreamReader(new FileInputStream(bigramARPAFileName),"UTF-8"));
    String line=null;
    boolean u=false;
    boolean b=false;
    while ((line=br.readLine()) != null) {
      line=line.trim();
      if (line.contains("1-grams:")) {
        u=true;
      }
      if (!line.isEmpty() && u) {
        String[] parts=line.split("\\s+");
        if (parts.length == 3) {
          ugrams.put(parts[1],new Tuple2<Double,Double>(Double.parseDouble(parts[0]),Double.parseDouble(parts[2])));
        }
 else         if (parts.length == 2) {
          ugrams.put(parts[1],new Tuple2<Double,Double>(Double.parseDouble(parts[0]),0d));
        }
      }
      if (line.contains("2-grams:")) {
        u=false;
        b=true;
      }
      if (!line.isEmpty() && b) {
        String[] parts=line.split("\\s+");
        if (parts.length == 3) {
          bgrams.put(new Tuple2<String,String>(parts[1],parts[2]),Double.parseDouble(parts[0]));
        }
      }
    }
    br.close();
  }
 catch (  IOException e) {
    e.printStackTrace();
  }
 finally {
    try {
      br.close();
    }
 catch (    IOException e) {
      e.printStackTrace();
    }
  }
}

public Bigrams ( String bigramARPAFileName ) { ugrams = new HashMap < String , Tuple2 < Double , Double > > ( ) ; bgrams = new HashMap < Tuple2 < String , String > , Double > ( ) ; BufferedReader br = null ; try { br = new BufferedReader ( new InputStreamReader ( new FileInputStream ( bigramARPAFileName ) , "UTF-8" ) ) ; String line = null ; boolean u = false ; boolean b = false ; while ( ( line = br . readLine ( ) ) != null ) { line = line . trim ( ) ; if ( line . contains ( "1-grams:" ) ) { u = true ; } if ( ! line . isEmpty ( ) && u ) { String [ ] parts = line . split ( "\\s+" ) ; if ( parts . length == 3 ) { ugrams . put ( parts [ 1 ] , new Tuple2 < Double , Double > ( Double . parseDouble ( parts [ 0 ] ) , Double . parseDouble ( parts [ 2 ] ) ) ) ; } else if ( parts . length == 2 ) { ugrams . put ( parts [ 1 ] , new Tuple2 < Double , Double > ( Double . parseDouble ( parts [ 0 ] ) , 0d ) ) ; } } if ( line . contains ( "2-grams:" ) ) { u = false ; b = true ; } if ( ! line . isEmpty ( ) && b ) { String [ ] parts = line . split ( "\\s+" ) ; if ( parts . length == 3 ) { bgrams . put ( new Tuple2 < String , String > ( parts [ 1 ] , parts [ 2 ] ) , Double . parseDouble ( parts [ 0 ] ) ) ; } } } br . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } finally { try { br . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } }
constructs a bigram language model from an arpa file .
