<commit_before>
package com.cflint.plugins.core;

import java.util.regex.Pattern;

public class ValidName {
	public static final int MIN_VAR_LENGTH = 3;
	public static final int MAX_VAR_LENGTH = 20;
	public static final int MAX_VAR_WORDS = 4;

	public static final int MIN_METHOD_LENGTH = 3;
	public static final int MAX_METHOD_LENGTH = 25;
	public static final int MAX_METHOD_WORDS = 5;

	protected int minLength = MIN_VAR_LENGTH;
	protected int maxLength = MAX_VAR_LENGTH;
	protected int maxWords = MAX_VAR_WORDS;

	public ValidName() {
	}

	public ValidName(int minLength, int maxLength, int maxWords) {
		this.minLength = minLength;
		this.maxLength = maxLength;
		this.maxWords = maxWords;
	}

	public boolean isInvalid(String name) {
		return !validChars(name) || !(isSameCase(name) || isCamelCase(name) || usesUnderscores(name)) || endsInNumber(name);
	}

	public boolean validChars(String name) {
		 Pattern valid = Pattern.compile("[A-Za-z0-9_]+");
		 return valid.matcher(name).matches();
	}

	public boolean isUpperCase(String name) {
		return name.toUpperCase().equals(name);
	}

	public boolean isSameCase(String name) {
		return name.equals(name.toLowerCase()) || name.equals(name.toUpperCase());
	}

	public boolean isCamelCase(String name) {
		Pattern valid = Pattern.compile("[a-z0-9]+([A-Z][a-z0-9]+)*");
		return valid.matcher(name).matches();
	}

	public boolean usesUnderscores(String name) {
		Pattern valid = Pattern.compile("[a-z0-9]+[a-z0-9_]+");
		return valid.matcher(name).matches();
	}

	public boolean endsInNumber(String name) {
		char lastLetter = name.charAt(name.length() - 1);

		if (Character.isDigit(lastLetter)) {
			return true;
		}

		return false;
	}

	public boolean tooShort(String name) {
		return name.length() < minLength;
	}

	public boolean tooLong(String name) {
		return name.length() > maxLength;
	}

	public boolean tooWordy(String name) {
		String[] words = name.split("[A-Z_]");
		return words.length > maxWords;
	}

	public boolean isTemporary(String name) {
		String[] wordsToAvoid = {"temp", "tmp", "var", "func", "obj", "object", "bool", "struct", "string", "array"};
		String sentence = name.replaceAll("_", " ");
		sentence = sentence.replaceAll("(\\p{Ll})(\\p{Lu})","$1 $2");
		String[] words = sentence.split(" ");
		
		for (String badWord : wordsToAvoid) {
			for (String word : words) {
				if (word.toLowerCase().equals(badWord))
				{
					return true;
				}
			}
		}

		return false;
	}

	public boolean hasPrefixOrPostfix(String name) {
		String[] namesToAvoid = {"s", "st", "str", "o", "obj", "b", "q", "a", "arr", "this", "my"};
		String sentence = name.replaceAll("_", " ");
		sentence = sentence.replaceAll("(\\p{Ll})(\\p{Lu})","$1 $2");
		String[] words = sentence.split(" ");
		String firstWord = words[0];
		String lastWord = words[words.length-1];

		if (words.length > 1) {
			for (String badName : namesToAvoid) {<commit_msg>improve regular expressions and fix up word count<commit_after>
package com.cflint.plugins.core;

import java.util.regex.Pattern;

public class ValidName {
	public static final int MIN_VAR_LENGTH = 3;
	public static final int MAX_VAR_LENGTH = 20;
	public static final int MAX_VAR_WORDS = 4;

	public static final int MIN_METHOD_LENGTH = 3;
	public static final int MAX_METHOD_LENGTH = 25;
	public static final int MAX_METHOD_WORDS = 5;

	protected int minLength = MIN_VAR_LENGTH;
	protected int maxLength = MAX_VAR_LENGTH;
	protected int maxWords = MAX_VAR_WORDS;

	public ValidName() {
	}

	public ValidName(int minLength, int maxLength, int maxWords) {
		this.minLength = minLength;
		this.maxLength = maxLength;
		this.maxWords = maxWords;
	}

	public boolean isInvalid(String name) {
		return !validChars(name) || endsInNumber(name) || !(isSameCase(name) || isCamelCaseLower(name) || usesUnderscores(name));
	}

	public boolean validChars(String name) {
		 Pattern valid = Pattern.compile("^[A-Za-z0-9_]+$");
		 return valid.matcher(name).matches();
	}

	public boolean isUpperCase(String name) {
		return name.toUpperCase().equals(name);
	}

	public boolean isSameCase(String name) {
		return name.equals(name.toLowerCase()) || name.equals(name.toUpperCase());
	}

	public boolean isCamelCaseLower(String name) {
		// [A-Z0-9]{2,5} catch names like productID, phone4G, requestURL etc etc
		Pattern valid = Pattern.compile("^[a-z0-9]+([A-Z]{1,5}[a-z0-9]+)*([A-Z0-9]{2,5}){0,1}$");
		return valid.matcher(name).matches();
	}

	public boolean isCamelCaseUpper(String name) {
		Pattern valid = Pattern.compile("^([A-Z]{1,5}[a-z0-9]+)+([A-Z0-9]{2,5}){0,1}$");
		return valid.matcher(name).matches();
	}

	public boolean usesUnderscores(String name) {
		return name.indexOf('_') != -1;
	}

	public boolean endsInNumber(String name) {
		char lastLetter = name.charAt(name.length() - 1);

		if (Character.isDigit(lastLetter)) {
			return true;
		}

		return false;
	}

	public boolean tooShort(String name) {
		return name.length() < minLength;
	}

	public boolean tooLong(String name) {
		return name.length() > maxLength;
	}

	public boolean tooWordy(String name) {
		String[] words = name.split("[A-Z_]+");
		int count = 0;

		for (int i = 0; i < words.length; i++) {
			if (words[i] != null && !words[i].equals("")) {
				count++;
			}
		}

		return count > maxWords;
	}

	public boolean isTemporary(String name) {
		String[] wordsToAvoid = {"temp", "tmp", "var", "func", "obj", "object", "bool", "struct", "string", "array", "comp"};
		String sentence = name.replaceAll("_", " ");
		sentence = sentence.replaceAll("(\\p{Ll})(\\p{Lu})","$1 $2");
		String[] words = sentence.split(" ");
		
		for (String badWord : wordsToAvoid) {
			for (String word : words) {
				if (word.toLowerCase().equals(badWord))
				{
					return true;
				}
			}
		}

		return false;
	}

	public boolean hasPrefixOrPostfix(String name) {
		String[] namesToAvoid = {"s", "st", "str", "o", "obj", "b", "q", "a", "arr", "this", "my"};
		String sentence = name.replaceAll("_", " ");
		sentence = sentence.replaceAll("(\\p{Ll})(\\p{Lu})","$1 $2");
		String[] words = sentence.split(" ");
		String firstWord = words[0];
		String lastWord = words[words.length-1];

		if (words.length > 1) {
			for (String badName : namesToAvoid) {
{'fraction_non_alphanumeric': 0.16344294003868473, 'fraction_numerical': 0.011927788523533205, 'mean_word_length': 4.276360544217687, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3221822', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2130, 'n_words': 513}