import javax.xml.parsers.*; import org.w3c.dom.*; import java.util.regex.*; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.Collections; import java.util.Comparator; import java.io.*; public class TP3_1 { public static void main (String[] args) { try { BufferedReader in = new BufferedReader(new FileReader(args[0])); for (String line = in.readLine(); line != null && line.length() > 0; line = in.readLine()) { String url = line.split("\\s+", 2)[1]; FeedData data; try { data = new FeedData(url); } catch (Exception e) { continue; } List> words = data.getSortedWordCounts(); System.out.println(url); for (Map.Entry entry : words) System.out.println(entry.getValue() + " " + entry.getKey()); } } catch (IOException e) { System.exit(1); } } } class FeedData { private final static Pattern pattern = Pattern.compile("[A-Za-z]+"); private final static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); private DocumentBuilder builder = null; protected Map occurrences = new HashMap(); protected int getOccurrences(String word) { Integer value = occurrences.get(word); if (null == value) return 0; return value; } protected void countWords (NodeList nodes) { for (int i = 0; i < nodes.getLength(); i++) { String text = nodes.item(i).getTextContent(); Matcher matches = pattern.matcher(text); while (matches.find()) { String word = matches.group().toLowerCase(); occurrences.put(word, 1+getOccurrences(word)); } } } protected void computeOccurrences (String url) throws org.xml.sax.SAXException, IOException { Document doc = builder.parse(url); countWords(doc.getElementsByTagName("title")); countWords(doc.getElementsByTagName("description")); } public FeedData(String url) throws ParserConfigurationException, org.xml.sax.SAXException, IOException { builder = factory.newDocumentBuilder(); computeOccurrences(url); } public Set> getWordCounts () { return occurrences.entrySet(); } public List> getSortedWordCounts () { List> entries = new ArrayList>(this.getWordCounts()); Collections.sort(entries, new ValueComparator(true)); return entries; } public Set getWords () { return occurrences.keySet(); } } class ValueComparator> implements Comparator> { protected boolean reversed_p = false; public ValueComparator () {} public ValueComparator (boolean reversed_p) { this.reversed_p = reversed_p; } public int compare (Map.Entry a, Map.Entry b) { return (reversed_p ? -1 : 1) * a.getValue().compareTo(b.getValue()); } }