Пример #1
0
  // constructor takes the name of the two input files
  public WordNet(String synsets, String hypernyms) {
    ifNullThrowException(synsets);
    ifNullThrowException(hypernyms);

    st = new LinearProbingHashST<String, ArrayList<Integer>>();
    In in = new In(synsets);

    int len = 0;
    int No;
    String[] words;
    ArrayList<String> tempKeys = new ArrayList<String>();
    while (in.hasNextLine()) {
      String[] a = in.readLine().split(",");
      tempKeys.add(a[1]);

      No = Integer.parseInt(a[0]);
      words = a[1].split(" ");
      for (String word : words) {
        if (st.contains(word)) {
          st.get(word).add(No);
        } else {
          ArrayList<Integer> list = new ArrayList<Integer>();
          list.add(No);
          st.put(word, list);
        }
      }
      len++;
    }

    keys = new String[len];
    tempKeys.toArray(keys);

    G = new Digraph(len);
    in = new In(hypernyms);

    while (in.hasNextLine()) {
      String temp = in.readLine();
      String[] a = temp.split(",");
      int v = Integer.parseInt(a[0]);
      for (int i = 1; i < a.length; i++) {
        int w = Integer.parseInt(a[i]);
        G.addEdge(v, w);
      }
    }

    ifNotDagThrowException(G);
    sap = new SAP(G);
  }
 public Digraph(In in) {
   this(in.readInt());
   int edge = in.readInt();
   try {
     for (int i = 0; i < edge; i++) {
       if (in.hasNextLine()) {
         int v = in.readInt();
         int w = in.readInt();
         addEdge(v, w);
       }
     }
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }