Beispiel #1
0
 /**
  * Add a (wordid, wordstr) into the vocabulary. If the wordstr already exists in the vocabulary,
  * then output errors.
  */
 public void addWordstrWithWordid(int wordid, String wordstr) {
   ExceptionUtility.assertAsException(
       !containsWordid(wordid), "The word id already exists in the vocabulary!");
   ExceptionUtility.assertAsException(
       !containsWordstr(wordstr), "The word string already exists in the vocabulary!");
   wordidToWordstrMap.put(wordid, wordstr);
   wordstrToWordidMap.put(wordstr, wordid);
 }
Beispiel #2
0
  /**
   * Read the vocabulary from the file.
   *
   * @param filePath
   * @return
   */
  public static Vocabulary getVocabularyFromFile(String filePath) {
    Vocabulary vocab = new Vocabulary();

    ArrayList<String> lines = FileReaderAndWriter.readFileAllLines(filePath);
    for (String line : lines) {
      String[] splits = line.trim().split(":");
      ExceptionUtility.assertAsException(splits.length == 2);
      int wordid = Integer.parseInt(splits[0]);
      String wordstr = splits[1];
      vocab.addWordstrWithWordid(wordid, wordstr);
    }

    return vocab;
  }
Beispiel #3
0
 public int getWordidByWordstr(String wordstr) {
   ExceptionUtility.assertAsException(containsWordstr(wordstr));
   return wordstrToWordidMap.get(wordstr);
 }
Beispiel #4
0
 public String getWordstrByWordid(int wordid) {
   ExceptionUtility.assertAsException(containsWordid(wordid));
   return wordidToWordstrMap.get(wordid);
 }