Java 8, 7054 6467
This is inspired by (but not copied from) the builtin java.lang.String.hashCode function, so feel free to disallow according to rule #2.
w -> { return w.chars().reduce(53, (acc, c) -> Math.abs(acc * 79 + c)) % 16777216; };
To score:
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class TweetableHash {
public static void main(String[] args) throws Exception {
List<String> words = Files.readAllLines(Paths.get("british-english-huge.txt"));
Function<String, Integer> hashFunc = w -> { return w.chars().reduce(53, (acc, c) -> Math.abs(acc * 79 + c)) % 16777216; };
Map<Integer, Integer> hashes = new HashMap<>();
for (String word : words) {
int hash = hashFunc.apply(word);
if (hash < 0 || hash >= 16777216) {
throw new Exception("hash too long for word: " + word + " hash: " + hash);
}
Integer numOccurences = hashes.get(hash);
if (numOccurences == null) {
numOccurences = 0;
}
numOccurences++;
hashes.put(hash, numOccurences);
}
int numCollisions = hashes.values().stream().filter(i -> i > 1).reduce(Integer::sum).get();
System.out.println("num collisions: " + numCollisions);
}
}