Exemplo n.º 1
0
  /**
   * Finds which word in uniqueWordsVec that each word from wordVecs is closest to, also saves the
   * index (friend) in uniqueWords that corresponds to this word
   *
   * @param wordVecs Word vectors of sentence
   * @param uniqueWordVecs Word vectors of all unique words
   * @param cs Cosine similarity object for calculations
   * @return Similarity vectors, distance to closest word, alonside that words index
   */
  public List<double[]> similarityVectors(
      List<double[]> wordVecs, List<double[]> uniqueWordVecs, CosSim cs) {
    double[] shortestDistances = new double[uniqueWordVecs.size()];
    double[] bestFriends = new double[uniqueWordVecs.size()]; // Index of all closest words
    int friend = 0;
    for (int i = 0; i < uniqueWordVecs.size(); i++) // For all unique words
    {
      double currentShortest = Double.NEGATIVE_INFINITY;
      for (int j = 0; j < wordVecs.size(); j++) // Finds closest word in wordVecs
      {
        double tmpDist = cs.CosSim(wordVecs.get(j), uniqueWordVecs.get(i));
        if (tmpDist > currentShortest) {
          currentShortest = tmpDist;
          friend = j;
        }
      }
      shortestDistances[i] = currentShortest;
      bestFriends[i] = friend;
    }

    List<double[]> results = new ArrayList<>();
    results.add(shortestDistances);
    results.add(bestFriends);
    return results;
  }
Exemplo n.º 2
0
 /**
  * Finds the word vectors for all words in a sentence.
  *
  * @param sent sentence
  * @return Word vectors for sentence
  */
 public List<double[]> CreateWordVector(String sent) {
   List<double[]> wordVecs = new ArrayList<double[]>();
   String[] splitSent = sent.split(" ");
   for (int i = 0; i < splitSent.length; i++) // For each word
   {
     double[] wordVector = allWordsVec.getVectorOfWord(splitSent[i]);
     if (wordVector[0] != -100) {
       wordVecs.add(wordVector);
     }
   }
   return wordVecs;
 }
Exemplo n.º 3
0
 public static List<Integer> mergeTwoLists(List<Integer> listOne, List<Integer> listTwo) {
   List<Integer> outputList = new ArrayList<Integer>();
   int i = 0;
   int j = 0;
   while (i < listOne.size() || j < listTwo.size()) {
     if (i == listOne.size()) {
       outputList.add(listTwo.get(j));
       j++;
     } else if (j == listTwo.size()) {
       outputList.add(listOne.get(i));
       i++;
     } else {
       if (listOne.get(i) <= listTwo.get(j)) {
         outputList.add(listOne.get(i));
         i++;
       } else {
         outputList.add(listTwo.get(j));
         j++;
       }
     }
   }
   return outputList;
 }
Exemplo n.º 4
0
  /**
   * Finds the weights of a word, both concerning the weight of the word itself, but also its
   * closest friend in the unique words. Note that if the word in the sentence exists in unique
   * words these will be the same The weights are inversely proportional to the frequency of the
   * word Frequencies of words are found in wordFreqs
   *
   * @param wordFreqs of word weights
   * @param sent sentence
   * @param unique all unique words in both sentences to be compared
   * @param sim Values of distances, and closest words to unique words for the sentence
   * @param sentJunk Sentence with nonsense words included
   * @return Word weights for all words in sentence/unique sentence
   */
  public List<double[]> WordWeights(
      List<WordFreq> wordFreqs, String sent, String unique, List<double[]> sim, String sentJunk) {
    String[] sentWordsJunk = sentJunk.split(" ");
    String[] sentWords = sent.split(" ");
    String[] uniqueWords = unique.split(" ");
    String friendWord = null;

    double[] weightsSent =
        new double[uniqueWords.length]; // Weights of closest words in sent to words in uniqueWords
    double[] weightsUnique = new double[uniqueWords.length]; // Weights of words in uniqueWords

    for (int j = 0; j < wordFreqs.size(); j++) {
      /* For each existing word in the listof words,
      check if it corresponds to the current word, then checks frequency value */
      for (int i = 0; i < uniqueWords.length; i++) {
        if ((wordFreqs.get(j).getWord()).equals(uniqueWords[i])) {
          weightsUnique[i] = 1 / wordFreqs.get(j).getFreq();
        }
      }
    }

    for (int i = 0; i < uniqueWords.length; i++) {
      int index = Arrays.asList(sentWords).indexOf(uniqueWords[i]);
      if (index >= 0) {
        weightsSent[i] = weightsUnique[i];
      } else { // if(sim.get(0)[i]>=threshold){
        friendWord = sentWordsJunk[(int) sim.get(1)[i]];
        index = Arrays.asList(uniqueWords).indexOf(friendWord);
        weightsSent[i] = weightsUnique[index]; // gets friend in sent
      }
    }

    List<double[]> results = new ArrayList<double[]>();
    results.add(weightsUnique);
    results.add(weightsSent);
    return results;
  }
Exemplo n.º 5
0
  public static int subscribeHandlerChange(
      String msgName, CHANGE_HANDLE_TYPE handlerChangeHandler) {
    /* Do it this way because multiple handlers can be subscribed
    for same message */
    List list = (List) handlerChangeHashTable.get(msgName);
    if (list == null) {
      list = new LinkedList();
      handlerChangeHashTable.put(msgName, list);
    }
    list.add(0, handlerChangeHandler);

    if (list.size() == 1) {
      return IPC_subscribeHandlerChange(msgName);
    } else {
      return IPC_OK;
    }
  }
Exemplo n.º 6
0
 public void add(Point point) {
   points.add(point);
 }
Exemplo n.º 7
0
 //execute SQL statement, SELECT * FROM mailing
 //rs = ...
 while (rs.next())
 {
     strRS = rs.getString(table1C1);
     strRS = strRS.substring(strRS.lastIndexOf("@")+1);
     emailList.add(strRS);
 }
Exemplo n.º 8
0
  public static void main(String[] args) {
    File file = new File("./shapesInput.txt");
    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
      fin = new FileInputStream(file);
      while ((ch = fin.read()) != -1) {
        strContent.append((char) ch);
      }
      fin.close();
    } catch (FileNotFoundException e) {
      System.out.println("File" + file.getAbsolutePath() + " could not be found on filesystem");
    } catch (IOException ioe) {
      System.out.println("Exception while reading the file" + ioe);
    }
    String fileOutput = strContent.toString();
    String[] commands = fileOutput.split(";");
    List<Shape> shapes = new ArrayList<Shape>();
    List<Double> areas = new ArrayList<Double>();
    List<Double> areasD = new ArrayList<Double>();
    for (int i = 0; i <= commands.length - 1; i++) {
      String codeLine = commands[i];
      String shapeType = codeLine.substring(17, 20);
      switch (shapeType) {
        case "Cir":
          String rad = codeLine.substring(23, codeLine.length());
          rad = rad.replace("(", "");
          rad = rad.replace(")", "");
          int radius;
          radius = Integer.parseInt(rad);
          shapes.add(new Circle(radius));
          break;
        case "Rec":
          String len = codeLine.substring(26, codeLine.length());
          len = len.replace("(", "");
          len = len.replace(")", "");
          String[] params = len.split(",");
          String h = params[0];
          String l = params[1];
          int height = Integer.parseInt(h);
          int length = Integer.parseInt(l);
          shapes.add(new Rectangle(height, length));
          break;
        case "Rho":
          String stuff = codeLine.substring(24, codeLine.length());
          stuff = stuff.replace("(", "");
          stuff = stuff.replace(")", "");
          String[] parame = stuff.split(",");
          String first = parame[0];
          String second = parame[1];
          int fir = Integer.parseInt(first);
          int sec = Integer.parseInt(second);
          shapes.add(new Rhombus(fir, sec));
          break;
        case "Tra":
          String total = codeLine.substring(26, codeLine.length());
          total = total.replace("(", "");
          total = total.replace(")", "");
          String[] seperated = total.split(",");
          String onep = seperated[0];
          String twop = seperated[1];
          String threep = seperated[2];
          int op = Integer.parseInt(onep);
          int tp = Integer.parseInt(twop);
          int thp = Integer.parseInt(threep);
          shapes.add(new Trapezoid(op, tp, thp));
          break;
        case "Tri":
          String measures = codeLine.substring(25, codeLine.length());
          measures = measures.replace("(", "");
          measures = measures.replace(")", "");
          String[] listp = measures.split(",");
          String aaa = listp[0];
          String bbb = listp[1];
          int aa = Integer.parseInt(aaa);
          int bb = Integer.parseInt(bbb);
          shapes.add(new Triangle(aa, bb));
          break;
      }
    }
    for (int q = 0; q <= shapes.size(); q++) {
      for (int i = 0; i <= shapes.size() - 2; i++) {
        if (shapes.get(i).getArea() > shapes.get(i + 1).getArea()) {
          Shape temp;
          temp = shapes.get(i);
          shapes.set(i, shapes.get(i + 1));
          shapes.set(i + 1, temp);
        } else {

        }
      }
    }
    for (Shape s : shapes) {
      System.out.println("\nCalculating " + s.getShapeName() + " area:");
      System.out.println("Area = " + s.getArea());
      System.out.println("Printing " + s.getShapeName() + " description:");
      s.printDescription();
    }
  }
Exemplo n.º 9
0
  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String line = null;
    while ((line = reader.readLine()) != null) {
      StringTokenizer token = new StringTokenizer(line);
      int n = Integer.parseInt(token.nextToken());
      int m = Integer.parseInt(token.nextToken());

      if (n == 0 && m == 0) {
        break;
      }

      for (int i = 0; i < n; i++) {
        parentRank[i][0] = i;
        parentRank[i][1] = 0;
      }

      edgeList.clear();

      for (int i = 0; i < m; i++) {
        token = new StringTokenizer(reader.readLine());
        int x = Integer.parseInt(token.nextToken());
        int y = Integer.parseInt(token.nextToken());
        int w = Integer.parseInt(token.nextToken());

        edgeList.add(new Edge(x, y, w));
      }

      List<Integer> maxWeights = new ArrayList<Integer>();

      Collections.sort(edgeList);

      int maxWeight = 0;

      for (int i = 0; i < edgeList.size(); i++) {
        Edge edge = edgeList.get(i);
        int x = find(edge.a);
        int y = find(edge.b);

        if (x != y) {
          maxWeight = Math.max(maxWeight, edge.weight);
          union(x, y);
        } else {
          maxWeights.add(Math.max(maxWeight, edge.weight));
        }
      }

      if (maxWeights.size() > 0) {
        for (int i = 0; i < maxWeights.size(); i++) {
          if (i + 1 != maxWeights.size()) {
            System.out.print(maxWeights.get(i) + " ");
          } else {
            System.out.print(maxWeights.get(i));
          }
        }

        System.out.println();
      } else {
        System.out.println("forest");
      }
    }
  }