/** * Writes a list of strings to a file, one per line. */ public static void write(String fileName, Iterable<String> list) throws IOException { write(fileName, list, identityTransformer); }
/** * Writes a list of strings to a file, one per line. */ public static void write(String fileName, Iterable<String> list) throws IOException { write(fileName, list, identityTransformer); }
/** * This will save the rules out to file. * @param fname * @param rules * @throws IOException */ public static void saveRules(String fname, HashMap<String, HashMap<String, Integer>> rules) throws IOException { ArrayList<String> rulelines = new ArrayList<>(); ArrayList<String> sortedkeys = new ArrayList<>(rules.keySet()); Collections.sort(sortedkeys); for(String surface : sortedkeys){ HashMap<String, Integer> labelcounts = rules.get(surface); String outs = surface + "\t"; for(String label : labelcounts.keySet()){ outs += label + ":" + labelcounts.get(label) + "\t"; } rulelines.add(outs.trim()); } LineIO.write(fname, rulelines); }
@PostMapping(value = "/config") public String config(@ModelAttribute ConfigFile c, HttpSession hs) throws IOException { System.out.println("Writing to: config/" + c.getFname()); LineIO.write("config/" + c.getFname(), Collections.singletonList(c.toString())); return "redirect:/"; }
/** * Run this method to get all results for ranking. * @throws Exception */ private static void experiments() throws Exception { String[] arabic_names = {"Arabic", "Egyptian_Arabic", "Mazandarani", "Pashto", "Persian", "Western_Punjabi"}; String[] devanagari_names = {"Hindi", "Marathi", "Nepali", "Sanskrit"}; String[] cyrillic_names = {"Bashkir", "Bulgarian", "Chechen", "Kirghiz", "Macedonian", "Russian", "Ukrainian"}; List<String> cyrillicresults = new ArrayList<>(); NUMTRAIN = 381; NUMTEST = 482; for(String name : cyrillic_names){ logger.debug("Working on " + name); String trainfile = wikidata + String.format("wikidata.%s", name); String testfile = wikidata + String.format("wikidata.%s", "Chuvash"); cyrillicresults.add(name + TrainAndTest(trainfile, testfile)); } LineIO.write("cyrillicresults.txt", cyrillicresults); }
/** * Run this method to get all results for ranking. * @throws Exception */ private static void experiments() throws Exception { String[] arabic_names = {"Arabic", "Egyptian_Arabic", "Mazandarani", "Pashto", "Persian", "Western_Punjabi"}; String[] devanagari_names = {"Hindi", "Marathi", "Nepali", "Sanskrit"}; String[] cyrillic_names = {"Bashkir", "Bulgarian", "Chechen", "Kirghiz", "Macedonian", "Russian", "Ukrainian"}; List<String> cyrillicresults = new ArrayList<>(); NUMTRAIN = 381; NUMTEST = 482; for(String name : cyrillic_names){ logger.debug("Working on " + name); String trainfile = wikidata + String.format("wikidata.%s", name); String testfile = wikidata + String.format("wikidata.%s", "Chuvash"); cyrillicresults.add(name + TrainAndTest(trainfile, testfile)); } LineIO.write("cyrillicresults.txt", cyrillicresults); }
/** * This saves the user-generated pairs to file. */ public void save(String dataname, String username) throws IOException { List<String> outlines = newpairs.stream().map(p -> p.getFirst() + "\t" + p.getSecond()).collect(toList()); LineIO.write(getUserDictPath(dataname, username), outlines); }
/** * print output into a file in directory specified, with name based on annotationFile. * Should not create an empty file (i.e., if columnOutput is empty). * * @param nerOutputDir directory to write output file * @param annotationFile used as prefix for the name of the new file * @param columnOutput a list of strings to be printed to the output file * @throws IOException */ private static void printOut(String nerOutputDir, String annotationFile, List<String> columnOutput) throws IOException { String outFile = nerOutputDir + "/" + annotationFile + ".ner.column.txt" ; if ( !columnOutput.isEmpty() ) { if ( !IOUtils.exists( nerOutputDir ) ) IOUtils.mkdir( nerOutputDir ); LineIO.write(outFile, columnOutput); } }
@RequestMapping(value="/addsuffix", method= RequestMethod.GET) @ResponseBody public String addsuffix(@RequestParam(value="suffix") String suffix, @RequestParam(value="taid") String taid, HttpSession hs) { SessionData sd = new SessionData(hs); Properties prop = sd.datasets.get(sd.dataname); String folderpath = prop.getProperty("folderpath"); TreeMap<String, TextAnnotation> tas = sd.tas; TextAnnotation ta = tas.get(taid); logger.info(sd.suffixes.toString()); logger.info(suffix); // in case the user starts the string with a dash if(suffix.startsWith("-")){ suffix = suffix.substring(1); } // if it's not there, add it, and save it. if(!sd.suffixes.contains(suffix)) { sd.suffixes.add(suffix); // sort it sd.suffixes.sort((String s1, String s2)-> s2.length()-s1.length()); // write it out to file. Don't care if the file is clobbered... String folderparent = (new File(folderpath)).getParent(); File suffixfile = new File(folderparent, "suffixes-" + sd.username + ".txt"); try { LineIO.write(suffixfile.getAbsolutePath(), Collections.singletonList(StringUtils.join(" ", sd.suffixes))); } catch (IOException e) { logger.error("Could not save suffix file: " + suffixfile.getAbsolutePath()); } } return HtmlGenerator.getHTMLfromTA(ta, sd.dict, sd.showdefs, sd.showroman); }
/** * Serialize a TextAnnotation and then write to file. If forceOverwrite_ is set to false and * file already exists, this method throws an exception. * * @param ta The text annotation to be serialized * @param fileName Name of file to write to * @param forceOverwrite Whether or not to overwrite existing file. */ public static void serializeTextAnnotationToFile(TextAnnotation ta, String fileName, boolean forceOverwrite, boolean useJson) throws IOException { File outFile = new File(fileName); if (outFile.exists() && !forceOverwrite) throw new IOException("ERROR: " + NAME + ".serializeTextAnnotationToFile(): file '" + fileName + "' already exists."); if (!useJson) FileUtils.writeByteArrayToFile(outFile, serializeTextAnnotationToBytes(ta)); else { String jsonTaStr = SerializationHelper.serializeToJson(ta, true); if (!forceOverwrite) { if (IOUtils.exists(fileName)) throw new IOException("ERROR: file '" + fileName + "' already exists, and " + "forceOverwrite flag is set to 'false'."); } LineIO.write(fileName, Collections.singletonList(jsonTaStr)); } }
/** * Serialize a TextAnnotation and then write to file. If forceOverwrite_ is set to false and * file already exists, this method throws an exception. * * @param ta The text annotation to be serialized * @param fileName Name of file to write to * @param forceOverwrite Whether or not to overwrite existing file. */ public static void serializeTextAnnotationToFile(TextAnnotation ta, String fileName, boolean forceOverwrite, boolean useJson) throws IOException { File outFile = new File(fileName); if (outFile.exists() && !forceOverwrite) throw new IOException("ERROR: " + NAME + ".serializeTextAnnotationToFile(): file '" + fileName + "' already exists."); if (!useJson) FileUtils.writeByteArrayToFile(outFile, serializeTextAnnotationToBytes(ta)); else { String jsonTaStr = SerializationHelper.serializeToJson(ta, true); if (!forceOverwrite) { if (IOUtils.exists(fileName)) throw new IOException("ERROR: file '" + fileName + "' already exists, and " + "forceOverwrite flag is set to 'false'."); } LineIO.write(fileName, Collections.singletonList(jsonTaStr)); } }
/** * Use this to create the compact files used during inference. * NB: You need to have access to the Propbank and Nombank frame files */ public static void main(String[] args) throws IOException { List<String> outLines = new ArrayList<>(); FramesManager manager = new FramesManager(propFramesDir); // FramesManager manager = new FramesManager(nomFramesDir); for (String predicate : manager.getPredicates()) { FrameData frame = manager.getFrame(predicate); Set<String> senses = frame.getSenses(); String senseStr = ""; for (String sense : senses) { Set<String> argsForSense = frame.getArgsForSense(sense); if (argsForSense.isEmpty()) continue; senseStr += sense + "#"; for (String arg : argsForSense) senseStr += arg + ","; senseStr = senseStr.substring(0, senseStr.length()-1) + " "; } outLines.add(predicate + "\t" + senseStr.trim()); } Collections.sort(outLines); LineIO.write("src/main/resources/Verb.legal.arguments", outLines); // LineIO.write("src/main/resources/Nom.legal.arguments", outLines); } }
@CommandDescription(description = "<DESCRIPTION>\n" + "\tThis procedure is used to extract the weight vector from a saved model \n" + "<INPUT>\n" + "\tIt receives 2 different arguments. \n" + "\t1) model_file (a string), the file name of a trained model \n" + "\t2) output_file (a string), the file name that will be used to put the contain of the weight vector. \n" + "<OUTPUT>\n" + "\tThe weight vector will be output in the ${output_file}.") public static void outputWeightVector(String model_name, String output_file) throws IOException, ClassNotFoundException { JLISModelIOManager io = new JLISModelIOManager(); RerankerModel model = (RerankerModel) io.loadModel(model_name); ArrayList<String> out = new ArrayList<String>(); double[] w = model.wv.getInternalArray(); for (int i = 0; i < w.length; i++) out.add(i + ":" + w[i]); LineIO.write(output_file, out); System.out.println("Finish putting the weight vector at " + output_file); } }
/** * This writes the production probabilities out to file in human-readable format. * @param fname the name of the output file * @param threshold only write probs above this threshold * @throws IOException */ public void WriteProbs(String fname, double threshold) throws IOException { ArrayList<String> outlines = new ArrayList<>(); List<Production> keys = new ArrayList<>(probs.keySet()); Collections.sort(keys, new Comparator<Production>() { @Override public int compare(Production o1, Production o2) { return o1.getFirst().compareTo(o2.getFirst()); } }); for(Production t : keys){ if(probs.get(t) > threshold) { String tstr = t.getFirst() + "\t" + t.getSecond(); outlines.add(tstr + "\t" + probs.get(t)); } } LineIO.write(fname, outlines); }
/** * This writes the production probabilities out to file in human-readable format. * @param fname the name of the output file * @param threshold only write probs above this threshold * @throws IOException */ public void WriteProbs(String fname, double threshold) throws IOException { ArrayList<String> outlines = new ArrayList<>(); List<Production> keys = new ArrayList<>(probs.keySet()); Collections.sort(keys, new Comparator<Production>() { @Override public int compare(Production o1, Production o2) { return o1.getFirst().compareTo(o2.getFirst()); } }); for(Production t : keys){ if(probs.get(t) > threshold) { String tstr = t.getFirst() + "\t" + t.getSecond(); outlines.add(tstr + "\t" + probs.get(t)); } } LineIO.write(fname, outlines); }
@CommandDescription(description = "<DESCRIPTION>\n" + "\tThis procedure is used to extract the weight vector from a saved model \n" + "<INPUT>\n" + "\tIt receives 2 different arguments. \n" + "\t1) model_file (a string), the file name of a trained model \n" + "\t2) output_file (a string), the file name that will be used to put the contain of the weight vector. \n" + "<OUTPUT>\n" + "\tThe weight vector will be output in the ${output_file}.") public static void outputWeightVector(String model_name, String output_file) throws IOException, ClassNotFoundException { JLISModelIOManager io = new JLISModelIOManager(); MulticlassModel model = (MulticlassModel) io.loadModel(model_name); String[] reverse = model.getReverseMapping(); ArrayList<String> out = new ArrayList<String>(); int start = 0; double[] w = model.wv.getInternalArray(); for (int i = 0; i < reverse.length; i++) { out.add("Label:" + reverse[i]); for (int t = 0; t < model.n_base_feature_in_train; t++) { if (t == model.n_base_feature_in_train - 1) out.add(t + ":" + w[start + t] + " (bias)"); else out.add(t + ":" + w[start + t]); } start += model.n_base_feature_in_train; } LineIO.write(output_file, out); System.out.println("Finish putting the weight vector at " + output_file); }
try { logger.trace("Writing file out to '{}'...", outFile); LineIO.write(outFile, Collections.singletonList(jsonTa)); } catch (IOException e) { e.printStackTrace();
LineIO.write("output/out-disc.txt", outlines);
LineIO.write(output_name, out);
System.out.println("The prediction results have been output to [" + output_name + "]"); LineIO.write(output_name, out);