/* * Represents the similarity connection between two artists */ class Connection{ Artist from, to; float strength; Connection(Artist from, Artist to, float strength){ this.from = from; this.to = to; this.strength = strength; } void add(float strength){ this.strength = (this.strength + strength)/2; } } class Connections{ HashMap connectionMap; HashMap artistMap; // will contain linkedlists of connections Connections(){ this.connectionMap = new HashMap(); this.artistMap = new HashMap(); } void load(Artists artists){ try{ for(int a=0; a a2.id){ Artist temp = a1; a1 = a2; a2 = temp; } Integer key = code(a1, a2); Connection c = null; if(connectionMap.containsKey(key)){ c = (Connection) connectionMap.get(key); c.add(strength); } else{ c = new Connection(a1, a2, strength); connectionMap.put(key, c); // do map LinkedList a1Connections; if(artistMap.containsKey(a1)){ a1Connections = (LinkedList) artistMap.get(a1); }else{ a1Connections = new LinkedList(); artistMap.put(a1, a1Connections); } a1Connections.add(c); LinkedList a2Connections; if(artistMap.containsKey(a2)) a2Connections = (LinkedList) artistMap.get(a2); else{ a2Connections = new LinkedList(); artistMap.put(a2, a2Connections); } a1Connections.add(c); } return c; } Connection getConnection(Artist a1, Artist a2){ Integer key = code(a1, a2); return (Connection) connectionMap.get(key); } float getStrength(Artist a1, Artist a2){ Connection c = getConnection(a1, a2); if(c==null) return 0; return c.strength; } Iterator getConnections(Artist a){ if(!artistMap.containsKey(a)) return (new LinkedList()).iterator(); return ((LinkedList) artistMap.get(a)).iterator(); } Iterator iterator(){ return connectionMap.values().iterator(); } Integer code(Artist a1, Artist a2){ return new Integer(min(a1.id,a2.id)*45000 + max(a1.id,a2.id)); } byte[] similarArtistFile(String name){ try{ //byte[] asciiBytes = ( new String(name.getBytes("UTF-8"),"ASCII").replace('/','-')).replace(':','-').getBytes(); while(name.indexOf("&")!=-1){ int index = name.indexOf("&"); name = name.substring(0, index+1) + name.substring(index+5); } byte[] asciiBytes = name.replace('/','-').replace(':','-').replace('?','-').getBytes(); byte[] fileNameBytes = new byte[asciiBytes.length]; for(int i=0; i31) fileNameBytes[i] = asciiBytes[i]; else fileNameBytes[i] = '-'; } String fileName = (new String(fileNameBytes,"ASCII")).toLowerCase(); byte[] similar = loadBytes("data/similars/"+fileName+".dat"); if(similar == null){ String URLName = name; try{ URLName = URLEncoder.encode(join(split(name,'&'),"%26"), "UTF-8"); } catch(UnsupportedEncodingException e){ e.printStackTrace(); } println(name+" is pulling data from to save "+fileName); similar = loadBytes("http://ws.audioscrobbler.com/1.0/artist/"+URLName+"/similar.txt"); if(similar == null){ println(name+" has no similar artist file ."); similar = (new String("")).getBytes(); saveBytes("data/similars/"+fileName+".dat", similar); } else{ println("Saving this to "+fileName); saveBytes("data/similars/"+fileName+".dat", similar); } } return similar; } catch(UnsupportedEncodingException e){ e.printStackTrace(); } return null; } }