// Path: src/main/java/renaming/priors/JavaVariableNameTypeDistribution.java
// public class JavaVariableNameTypeDistribution implements
// 		IConditionalProbability<String, String> {
// 
// 	private static final Logger LOGGER = Logger
// 			.getLogger(JavaVariableNameTypeDistribution.class.getName());
// 
// 	public static JavaVariableNameTypeDistribution buildFromFiles(
// 			final Collection<File> files) {
// 		final JavaVariableNameTypeDistribution tp = new JavaVariableNameTypeDistribution();
// 		for (final File f : files) {
// 			try {
// 				final JavaASTExtractor ex = new JavaASTExtractor(false);
// 				final CompilationUnit cu = ex.getAST(f);
// 				final JavaApproximateTypeInferencer typeInf = new JavaApproximateTypeInferencer(
// 						cu);
// 				typeInf.infer();
// 				final Map<String, String> varTypes = typeInf.getVariableTypes();
// 				for (final Entry<String, String> variable : varTypes.entrySet()) {
// 					tp.typePrior.addElement(variable.getKey(),
// 							variable.getValue());
// 				}
// 			} catch (final IOException e) {
// 				LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
// 			}
// 		}
// 
// 		return tp;
// 	}
// 
// 	public static void main(final String args[]) throws SerializationException {
// 		if (args.length != 2) {
// 			System.err.println("Usage <folderWithTypes> <SerialzedTypePrior>");
// 			return;
// 		}
// 
// 		final Collection<File> trainingFiles = FileUtils.listFiles(new File(
// 				args[0]), new RegexFileFilter(".*\\.java$"),
// 				DirectoryFileFilter.DIRECTORY);
// 
// 		final JavaVariableNameTypeDistribution tp = JavaVariableNameTypeDistribution
// 				.buildFromFiles(trainingFiles);
// 		Serializer.getSerializer().serialize(tp, args[1]);
// 	}
// 
// 	DiscreteElementwiseConditionalDistribution<String, String> typePrior = new DiscreteElementwiseConditionalDistribution<String, String>();
// 
// 	/**
// 	 * 
// 	 */
// 	private JavaVariableNameTypeDistribution() {
// 	}
// 
// 	@Override
// 	public Optional<String> getMaximumLikelihoodElement(final String type) {
// 		return typePrior.getMaximumLikelihoodElement(type);
// 	}
// 
// 	@Override
// 	public double getMLProbability(final String element, final String type) {
// 		if (type == null) {
// 			return 1;
// 		}
// 		return typePrior.getMLProbability(element, type);
// 	}
// 
// }


import java.io.File;
import java.util.Collection;
import java.util.logging.Logger;
import renaming.priors.JavaVariableNameTypeDistribution;
import codemining.languagetools.ITokenizer;
import codemining.languagetools.Scope;
import com.google.common.math.DoubleMath;
/**
 * 
 */
package renaming.renamers;




/**
 * Use type information for renaming stuff.
 * 
 * @author Miltos Allamanis <m.allamanis@ed.ac.uk>
 * 
 */
public class LocalTypedIdentifierRenamings extends BaseIdentifierRenamings {

	JavaVariableNameTypeDistribution tp;

	private static final Logger LOGGER = Logger
			.getLogger(LocalTypedIdentifierRenamings.class.getName());

	/**
	 * 
	 */
	public LocalTypedIdentifierRenamings(final ITokenizer tokenizer) {
		super(tokenizer);
	}

	@Override
	public double addScopePriors(final String identifierName, final Scope scope) {
		final double prob = tp.getMLProbability(identifierName, scope.type);
		if (prob > 0) {
			return -DoubleMath.log2(prob);
		} else if (!this.isTrueUNK(identifierName)) {
			return 100;
		} else {
			return 0;
		}
	}

	@Override
	public void buildRenamingModel(final Collection<File> trainingFiles) {
		super.buildRenamingModel(trainingFiles);
// Path: src/main/java/renaming/priors/JavaVariableNameTypeDistribution.java
// public class JavaVariableNameTypeDistribution implements
// 		IConditionalProbability<String, String> {
// 
// 	private static final Logger LOGGER = Logger
// 			.getLogger(JavaVariableNameTypeDistribution.class.getName());
// 
// 	public static JavaVariableNameTypeDistribution buildFromFiles(
// 			final Collection<File> files) {
// 		final JavaVariableNameTypeDistribution tp = new JavaVariableNameTypeDistribution();
// 		for (final File f : files) {
// 			try {
// 				final JavaASTExtractor ex = new JavaASTExtractor(false);
// 				final CompilationUnit cu = ex.getAST(f);
// 				final JavaApproximateTypeInferencer typeInf = new JavaApproximateTypeInferencer(
// 						cu);
// 				typeInf.infer();
// 				final Map<String, String> varTypes = typeInf.getVariableTypes();
// 				for (final Entry<String, String> variable : varTypes.entrySet()) {
// 					tp.typePrior.addElement(variable.getKey(),
// 							variable.getValue());
// 				}
// 			} catch (final IOException e) {
// 				LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
// 			}
// 		}
// 
// 		return tp;
// 	}
// 
// 	public static void main(final String args[]) throws SerializationException {
// 		if (args.length != 2) {
// 			System.err.println("Usage <folderWithTypes> <SerialzedTypePrior>");
// 			return;
// 		}
// 
// 		final Collection<File> trainingFiles = FileUtils.listFiles(new File(
// 				args[0]), new RegexFileFilter(".*\\.java$"),
// 				DirectoryFileFilter.DIRECTORY);
// 
// 		final JavaVariableNameTypeDistribution tp = JavaVariableNameTypeDistribution
// 				.buildFromFiles(trainingFiles);
// 		Serializer.getSerializer().serialize(tp, args[1]);
// 	}
// 
// 	DiscreteElementwiseConditionalDistribution<String, String> typePrior = new DiscreteElementwiseConditionalDistribution<String, String>();
// 
// 	/**
// 	 * 
// 	 */
// 	private JavaVariableNameTypeDistribution() {
// 	}
// 
// 	@Override
// 	public Optional<String> getMaximumLikelihoodElement(final String type) {
// 		return typePrior.getMaximumLikelihoodElement(type);
// 	}
// 
// 	@Override
// 	public double getMLProbability(final String element, final String type) {
// 		if (type == null) {
// 			return 1;
// 		}
// 		return typePrior.getMLProbability(element, type);
// 	}
// 
// }


// Path: src/main/java/renaming/renamers/LocalTypedIdentifierRenamings.java
import java.io.File;
import java.util.Collection;
import java.util.logging.Logger;
import renaming.priors.JavaVariableNameTypeDistribution;
import codemining.languagetools.ITokenizer;
import codemining.languagetools.Scope;
import com.google.common.math.DoubleMath;

/**
 * 
 */
package renaming.renamers;




/**
 * Use type information for renaming stuff.
 * 
 * @author Miltos Allamanis <m.allamanis@ed.ac.uk>
 * 
 */
public class LocalTypedIdentifierRenamings extends BaseIdentifierRenamings {

	JavaVariableNameTypeDistribution tp;

	private static final Logger LOGGER = Logger
			.getLogger(LocalTypedIdentifierRenamings.class.getName());

	/**
	 * 
	 */
	public LocalTypedIdentifierRenamings(final ITokenizer tokenizer) {
		super(tokenizer);
	}

	@Override
	public double addScopePriors(final String identifierName, final Scope scope) {
		final double prob = tp.getMLProbability(identifierName, scope.type);
		if (prob > 0) {
			return -DoubleMath.log2(prob);
		} else if (!this.isTrueUNK(identifierName)) {
			return 100;
		} else {
			return 0;
		}
	}

	@Override
	public void buildRenamingModel(final Collection<File> trainingFiles) {
		super.buildRenamingModel(trainingFiles);
		tp = JavaVariableNameTypeDistribution.buildFromFiles(trainingFiles);