It's the HAC algorithm that Im using to sort newspaper articles by news. You can adapt it to pretty much any type of text.