public static double bleu(List<List<String>> candidates, List<List<String>> references, int n){
		if (candidates.size() == 0 || references.size() == 0){
			throw new IllegalStateException();
		}
		double score = 0.0;
		// c is the total length of the candidates' corpus, 
		// r is the sum of best matched length from references to each candidate
		int c = 0, r = 0;
		for (List<String> candidate : candidates){
			int candidateLength = candidate.size();	
			c += candidateLength;
			int closestRefLength = references.get(0).size();
			int distance = Math.abs( closestRefLength - candidateLength);
			if (references.size() > 1){ //we usually have only one reference translation
				for (List<String> reference : references){
					int newDistance = Math.abs(reference.size() - candidateLength);
					if (distance > newDistance){
						distance = newDistance;
						closestRefLength = reference.size();
					}
				}
			}
			r += closestRefLength;
		}
		double brevityPenalty = 0.0;
		if (c > r){
			brevityPenalty = 1.0;
		} else {
			brevityPenalty = Math.exp(1-r/c);
		}
		for (int i = 1; i <= n; i++){
			score += Math.log(score(candidates, references, i));
		}
		return brevityPenalty * Math.exp(score/n);
	}