Java Code Examples for org.antlr.v4.runtime.misc.Utils#writeFile()

The following examples show how to use org.antlr.v4.runtime.misc.Utils#writeFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LeaveOneOutValidator.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
		LangDescriptor[] languages = new LangDescriptor[] {
			JAVA_DESCR,
			JAVA8_DESCR,
			JAVA_GUAVA_DESCR,
			JAVA8_GUAVA_DESCR,
			ANTLR4_DESCR,
			SQLITE_CLEAN_DESCR,
			TSQL_CLEAN_DESCR,
			SQLITE_NOISY_DESCR,
			TSQL_NOISY_DESCR,
//			QUORUM_DESCR,
		};
		List<String> corpusDirs = map(languages, l -> l.corpusDir);
		String[] dirs = corpusDirs.toArray(new String[languages.length]);
		String python = testAllLanguages(languages, dirs, "leave_one_out.pdf");
		String fileName = "python/src/leave_one_out.py";
		Utils.writeFile(fileName, python);
		System.out.println("wrote python code to "+fileName);
	}
 
Example 2
Source File: Tool.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void format(LangDescriptor language,
                          String testFileName,
                          String outputFileName)
	throws Exception
{
	// load all files up front
	List<String> allFiles = getFilenames(new File(language.corpusDir), language.fileRegex);
	List<InputDocument> documents = load(allFiles, language);
	// if in corpus, don't include in corpus
	final String path = new File(testFileName).getAbsolutePath();
	List<InputDocument> others = filter(documents, d -> !d.fileName.equals(path));
	InputDocument testDoc = parse(testFileName, language);
	Corpus corpus = new Corpus(others, language);
	corpus.train();

	Formatter formatter = new Formatter(corpus, language.indentSize, Formatter.DEFAULT_K,
	                                    FEATURES_INJECT_WS, FEATURES_HPOS);
	String output = formatter.format(testDoc, false);

	if ( outputFileName!=null ) {
		Utils.writeFile(outputFileName, output);
	}
	else {
		System.out.print(output);
	}
}
 
Example 3
Source File: TestK.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void writePython(LangDescriptor[] languages, List<Integer> ks, Float[][] medians) throws IOException {
	StringBuilder data = new StringBuilder();
	StringBuilder plot = new StringBuilder();
	for (int i = 0; i<languages.length; i++) {
		LangDescriptor language = languages[i];
		List<Float> filteredMedians = BuffUtils.filter(Arrays.asList(medians[i]), m -> m!=null);
		data.append(language.name+'='+filteredMedians+'\n');
		plot.append(String.format("ax.plot(ks, %s, label=\"%s\", marker='%s', color='%s')\n",
		                          language.name, language.name,
		                          nameToGraphMarker.get(language.name),
								  nameToGraphColor.get(language.name)));
	}

	String python =
		"#\n"+
		"# AUTO-GENERATED FILE. DO NOT EDIT\n" +
		"# CodeBuff %s '%s'\n" +
		"#\n"+
		"import numpy as np\n"+
		"import matplotlib.pyplot as plt\n\n" +
		"%s\n" +
		"ks = %s\n"+
		"fig = plt.figure()\n"+
		"ax = plt.subplot(111)\n"+
		"%s"+
		"ax.tick_params(axis='both', which='major', labelsize=18)\n" +
		"ax.set_xlabel(\"$k$ nearest neighbors\", fontsize=20)\n"+
		"ax.set_ylabel(\"Median error rate\", fontsize=20)\n" +
		"#ax.set_title(\"k Nearest Neighbors vs\\nLeave-one-out Validation Error Rate\")\n"+
		"plt.legend(fontsize=18)\n\n" +
		"fig.savefig('images/vary_k.pdf', format='pdf')\n"+
		"plt.show()\n";
	String code = String.format(python, Tool.version, new Date(), data, ks, plot);

	String fileName = "python/src/vary_k.py";
	Utils.writeFile(fileName, code);
	System.out.println("wrote python code to "+fileName);
}
 
Example 4
Source File: AllJavaLeaveOneOutValidation.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	LangDescriptor[] languages = new LangDescriptor[] {
		JAVA_DESCR,
		JAVA8_DESCR,
		JAVA_GUAVA_DESCR,
	};
	List<String> corpusDirs = map(languages, l -> l.corpusDir);
	String[] dirs = corpusDirs.toArray(new String[languages.length]);
	String python = testAllLanguages(languages, dirs, "all_java_leave_one_out.pdf");
	String fileName = "python/src/all_java_leave_one_out.py";
	Utils.writeFile(fileName, python);
	System.out.println("wrote python code to "+fileName);
}
 
Example 5
Source File: AllSQLLeaveOneOutValidation.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	LangDescriptor[] languages = new LangDescriptor[] {
		SQLITE_NOISY_DESCR,
		SQLITE_CLEAN_DESCR,
		TSQL_NOISY_DESCR,
		TSQL_CLEAN_DESCR,
	};
	List<String> corpusDirs = map(languages, l -> l.corpusDir);
	String[] dirs = corpusDirs.toArray(new String[languages.length]);
	String python = testAllLanguages(languages, dirs, "all_sql_leave_one_out.pdf");
	String fileName = "python/src/all_sql_leave_one_out.py";
	Utils.writeFile(fileName, python);
	System.out.println("wrote python code to "+fileName);
}
 
Example 6
Source File: SubsetValidator.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
		LangDescriptor[] languages = new LangDescriptor[] {
//			QUORUM_DESCR,
			ANTLR4_DESCR,
			JAVA_DESCR,
			JAVA8_DESCR,
			JAVA_GUAVA_DESCR,
			JAVA8_GUAVA_DESCR,
			SQLITE_CLEAN_DESCR,
			TSQL_CLEAN_DESCR,
		};

		int maxNumFiles = 30;
		int trials = 50;
		Map<String,float[]> results = new HashMap<>();
		for (LangDescriptor language : languages) {
			float[] medians = getMedianErrorRates(language, maxNumFiles, trials);
			results.put(language.name, medians);
		}
		String python =
			"#\n"+
			"# AUTO-GENERATED FILE. DO NOT EDIT\n" +
			"# CodeBuff <version> '<date>'\n" +
			"#\n"+
			"import numpy as np\n"+
			"import matplotlib.pyplot as plt\n\n" +
			"fig = plt.figure()\n"+
			"ax = plt.subplot(111)\n"+
			"N = <maxNumFiles>\n" +
			"sizes = range(1,N+1)\n" +
			"<results:{r |\n" +
			"<r> = [<rest(results.(r)); separator={,}>]\n"+
			"ax.plot(range(1,len(<r>)+1), <r>, label=\"<r>\", marker='<markers.(r)>', color='<colors.(r)>')\n" +
			"}>\n" +
			"ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5)\n" +
			"ax.set_xlabel(\"Number of training files in sample corpus subset\", fontsize=14)\n"+
			"ax.set_ylabel(\"Median Error rate for <trials> trials\", fontsize=14)\n" +
			"ax.set_title(\"Effect of Corpus size on Median Leave-one-out Validation Error Rate\")\n"+
			"plt.legend()\n" +
			"plt.tight_layout()\n" +
			"fig.savefig('images/subset_validator.pdf', format='pdf')\n"+
			"plt.show()\n";
		ST pythonST = new ST(python);
		pythonST.add("results", results);
		pythonST.add("markers", LeaveOneOutValidator.nameToGraphMarker);
		pythonST.add("colors", LeaveOneOutValidator.nameToGraphColor);
		pythonST.add("version", version);
		pythonST.add("date", new Date());
		pythonST.add("trials", trials);
		pythonST.add("maxNumFiles", maxNumFiles);
		List<String> corpusDirs = map(languages, l -> l.corpusDir);
		String[] dirs = corpusDirs.toArray(new String[languages.length]);
		String fileName = "python/src/subset_validator.py";
		Utils.writeFile(fileName, pythonST.render());
		System.out.println("wrote python code to "+fileName);
	}