/** * Maps the POS tags from strings to <code>POS</code> class * * @param str String representation of a POS * @return the POS-class mapped pos */ public static POS convertStringToPos(String str) { if (str.equals("Noun")) return POS.NOUN; else if (str.equals("Adverb")) return POS.ADVERB; else if (str.equals("Adjective")) return POS.ADJECTIVE; else if (str.equals("Verb")) return POS.VERB; else MyError.exit("Invalid POS!"); return POS.ANY; }
public static String replaceCorresponding(String text, String toFind, String toReplace) { MyError.assertTrue(toFind.length() == toReplace.length()); for (int i = 0; i < toFind.length(); i++) { text = text.replace(toFind.charAt(i), toReplace.charAt(i)); } return text; }
/** * Loads a whole file into a string * * @param filename path to the file * @return string containing the file content */ public static String getFileContent(String filename) { String out = null; byte[] encoded = null; try { encoded = Files.readAllBytes(Paths.get(filename)); } catch (IOException e) { MyError.exit(e.getMessage()); } try { out = new String(encoded, "UTF8"); } catch (UnsupportedEncodingException e) { MyError.exit(e.getMessage()); } return out; }
/** * Produces the abbreviated form of a pos string * * @param pos The full pos string * @return The abbreviated single character pos */ public static String convertPosToSingleCharString(String pos) { pos = pos.toLowerCase(); if (pos.equals("noun")) return "n"; else if (pos.equals("adverb")) return "r"; else if (pos.equals("adjective")) return "a"; else if (pos.equals("verb")) return "v"; else MyError.exit("Invalid POS `" + pos + "`!"); return ""; }
public static BufferedWriter openFileForWriting(String path, String encoding) { BufferedWriter handler = null; try { handler = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding)); } catch (IOException e) { MyError.exit("Couldn't open '" + path + "' for writing!"); } return handler; }
public static void putFileContent(String path, String payload) { BufferedWriter outFile = Common.openFileForWriting(path); try { outFile.write(payload); outFile.close(); } catch (IOException e) { MyError.exit("Couldn't write to file '" + path + "'!"); } }
public static LexicalType getCategoryFromInteger(int catNum) { switch (catNum) { case 0: return LexicalType.ANY; case 1: return LexicalType.SENSE; } MyError.exit("Invalid LexicalType!"); return LexicalType.ANY; }
/** * Determines the POS string code based on an integer code used only when parsing WordNet data * files * * @param posCode the POS code * @return the POS */ public static String convertIntegerToSingleCharStringPos(int posCode) { switch (posCode) { case 1: return "n"; case 2: return "v"; case 3: return "a"; case 4: return "r"; case 5: return "s"; } MyError.exit("Invalid Part-Of-Speech!"); return ""; }
/** * Converts POS tags to single character strings * * @param pos The pos tag * @return The single character representation of pos */ public static String convertPosToSingleCharString(POS pos) { switch (pos) { case NOUN: return "n"; case ADJECTIVE: return "a"; case ADVERB: return "r"; case VERB: return "v"; case SETELLITE_ADJECTIVE: return "a"; } MyError.exit("Invalid POS `" + pos + "`!"); return ""; }
/** * @param posNum * @return POS tag */ public static POS convertIntegerToPos(int posNum) { switch (posNum) { case 0: return POS.ANY; case 1: return POS.NOUN; case 2: return POS.VERB; case 3: return POS.ADJECTIVE; case 4: return POS.ADVERB; case 5: return POS.SETELLITE_ADJECTIVE; } MyError.exit("Invalid Part-Of-Speech!"); return POS.ANY; }
/** * Determines the POS tag of a sense based on special characters ('#' delimiter) in it. * * @param sense The word to be processed * @return The part-of-speech tag */ public static POS convertSingleCharStringToPos(String sense) { int index = sense.indexOf("#"); if (index == sense.length() - 1) return POS.ANY; switch (sense.charAt(index + 1)) { case 'n': return POS.NOUN; // noun case 'v': return POS.VERB; // verb case 'a': return POS.ADJECTIVE; // adjective case 'r': return POS.ADVERB; // adverb case 's': return POS.SETELLITE_ADJECTIVE; // Satellite adjective case 'u': return POS.ANY; // Unknown default: MyError.exit("Invalid POS character!"); } return POS.ANY; }