/** * 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); }
/** * 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; }
public int getWordidByWordstr(String wordstr) { ExceptionUtility.assertAsException(containsWordstr(wordstr)); return wordstrToWordidMap.get(wordstr); }
public String getWordstrByWordid(int wordid) { ExceptionUtility.assertAsException(containsWordid(wordid)); return wordidToWordstrMap.get(wordid); }