Java Code Examples for codemining.languagetools.ParseType#COMPILATION_UNIT

The following examples show how to use codemining.languagetools.ParseType#COMPILATION_UNIT . 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: PerturbationEvaluator.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PerturbationEvaluator(final File directory,
		final ITokenizer tokenizer, final IScopeExtractor scopeExtractor,
		final String renamerClass) {
	allFiles = FileUtils.listFiles(directory, tokenizer.getFileFilter(),
			DirectoryFileFilter.DIRECTORY);
	this.tokenizer = tokenizer;
	this.scopeExtractor = scopeExtractor;
	this.renamerClass = renamerClass;
	varRenamer = new ScopedIdentifierRenaming(scopeExtractor,
			ParseType.COMPILATION_UNIT);
}
 
Example 2
Source File: HumanEvaluationOutput.java    From naturalize with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void getOutput() {
	for (long i = 0; i < nExamples; i++) {
		try {
			final int idx = RandomUtils.nextInt(allFiles.size());
			final File testFile = allFiles.get(idx);

			final Collection<File> trainFiles = Sets.newTreeSet(allFiles);
			checkArgument(trainFiles.remove(testFile));

			final AbstractIdentifierRenamings renamer = getRenamer(
					renamerClass, renamerConstructorParam);
			renamer.buildRenamingModel(trainFiles);

			// Get scopes
			final Multimap<Scope, String> scopes = scopeExtractor
					.getFromFile(testFile);
			if (scopes.isEmpty()) {
				i--;
				continue;
			}
			final List<Entry<Scope, String>> allIdentifiers = Lists
					.newArrayList(scopes.entries());
			Collections.shuffle(allIdentifiers);
			final Entry<Scope, String> selected = allIdentifiers
					.get(RandomUtils.nextInt(allIdentifiers.size()));

			// Get results
			final SortedSet<Renaming> renamings = renamer.getRenamings(
					selected.getKey(), selected.getValue());

			if (renamings.isEmpty()
					|| renamings.first().name.equals(selected.getValue())) {
				i--;
				continue;
			}

			// Print possible terms/snippet and store
			boolean includedGround = false;
			final List<String> alternatives = Lists.newArrayList();
			int counter = 0;
			for (final Renaming renaming : renamings) {
				if ((counter <= 3) || (counter <= 4 && includedGround)) {
					counter++;
				} else {
					break;
				}
				final String nextName = renaming.name;
				if (nextName.equals(selected.getValue())) {
					includedGround = true;
				}
				alternatives.add(nextName);
			}
			if (!includedGround) {
				alternatives.add(selected.getValue());
			}
			if (alternatives.size() != 5) {
				i--;
				continue;
			}

			// Now output and add to cheatsheet
			Collections.shuffle(alternatives);
			System.out.println("============Renaming " + nextQuestionId
					+ "==============");
			final ScopedIdentifierRenaming sir = new ScopedIdentifierRenaming(
					scopeExtractor, ParseType.COMPILATION_UNIT);
			final String renamed = sir.getFormattedRenamedCode(
					selected.getKey().code, selected.getValue(),
					"GUESS_IT", FileUtils.readFileToString(testFile));
			System.out.println(renamed);
			System.out
					.println("===================Alternatives==================");
			for (final String alternative : alternatives) {
				System.out.println(alternative);
			}
			System.out.println("=====================================");
			final RenamingsData rd = new RenamingsData();
			rd.renamings = renamings;
			rd.ground = selected.getValue();
			cheatsheet.put(nextQuestionId, rd);
			nextQuestionId++;
		} catch (Throwable e) {
			LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
		}
	}
	printCheatsheet();
}