⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 corefcluster.java

📁 常用机器学习算法,java编写源代码,内含常用分类算法,包括说明文档
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * @param v2	 */	private void addNegativeWeights(WeightedGraph g, Vertex v1, Vertex v2) {		List adjacentToV1;		List adjacentToV2;		adjacentToV1 = g.getAdjacentVertices(v1);		adjacentToV2 = g.getAdjacentVertices(v2);		// Check that v1 is connected to all of v2's adjacent vertices		for (int i=0; i < adjacentToV2.size(); i++) {			Vertex v = (Vertex)adjacentToV2.get(i);			//System.out.println("v1: " + v1 + " v: " + v);			if (v == v1) {				continue;			}			if (!adjacentToV1.contains(v)) {				try {					System.out.println("Adding negative infinite edge: " + v1 + " to " + v);					g.addEdge(v, v1, NegativeInfinite);				} catch (Exception e) { e.printStackTrace(); }			}		}		// Check that v2 is connected to all of v1's adjacent vertices		for (int i=0; i < adjacentToV1.size(); i++) {			Vertex v = (Vertex)adjacentToV1.get(i);			//System.out.println("v2: " + v2 + " v " + v + " " + g.isConnected(v, v2));			if (v == v2) {				continue;			}			if (!adjacentToV2.contains(v)) {				try {					System.out.println("Adding negative infinite edge: " + v2 + " to " + v);					g.addEdge(v, v2, NegativeInfinite);				} catch (Exception e) { e.printStackTrace(); }			}		}	}	public void mergeVertices (WeightedGraph g, VertexImpl v1, VertexImpl v2) {		// Change: mhay 1/10/04		addNegativeWeights(g, v1, v2);		Object o1 = v1.getObject();		Object o2 = v2.getObject();		List l1;        //System.out.println("Merging o1 = " + toString(o1) + " and o2 = " + toString(o2));        if ((o1 instanceof List) && (o2 instanceof List)) {            /* Create a new vertex that merges the list of objects of the old vertices */            l1 = (List) o1;            l1.addAll((List) o2); // concatenate lists        } else if (o1 instanceof List) {            l1 = (List) o1;            l1.add(o2);        } else if (o2 instanceof List) {            l1 = (List) o2;            l1.add(o1);        } else {            l1 = new ArrayList();            l1.add(o1);            l1.add(o2);        }//		System.out.println("l1=" + l1);        VertexImpl newVertex = new VertexImpl(l1);        try {            g.add(newVertex);        } catch (Exception e) {            e.printStackTrace();        }        List edges1 = (List) g.getEdges(v1);        Iterator i1 = edges1.iterator();        HashMap hm = new HashMap();        while (i1.hasNext()) {            WeightedEdge e = (WeightedEdge) i1.next();            if ((e.getVertexA() == v1) && (e.getVertexB() != v2))                hm.put((Object) e.getVertexB(), new Double(e.getWeight()));            else if (e.getVertexA() != v2)                hm.put((Object) e.getVertexA(), new Double(e.getWeight()));        }        try {            g.remove(v1); // this also removes all edges incident with this vertex        } catch (Exception ex) {            ex.printStackTrace();        }//		System.out.println("Hashmap for vertex: " + v1 + " is: " + hm);		List edges2 = (List)g.getEdges(v2);		Iterator i2 = edges2.iterator();		Vertex cv = null;		if (edges2.size() > 0) {			while (i2.hasNext()) {				WeightedEdge e = (WeightedEdge)i2.next();				if (e.getVertexA() == v2)					cv = e.getVertexB();				else					cv = e.getVertexA();				double w2 = ((Double)hm.get(cv)).doubleValue();				double w1 = e.getWeight();				double weight = (w1 > w2) ? w1 : w2;   // max				//double weight = (w1 + w2)/2;   // avg				if (w1 == NegativeInfinite || w2 == NegativeInfinite) {					weight = NegativeInfinite; // precaution: avoid creeping away from Infinite				}				WeightedEdge ne = new WeightedEdgeImpl(newVertex, cv, weight);				try {					g.addEdge(ne);				} catch (Exception ex) { ex.printStackTrace(); }			}		} else { // in this case, no edges are left, just add new Vertex			try {				g.add(newVertex);			}	catch (Exception ex) { ex.printStackTrace(); }		}		try {			g.remove(v2);		}	catch (Exception ex) { ex.printStackTrace(); }//		System.out.println("After adding new edges: " + g);    }	public static void constructEdgesUsingTrainedClusterer (WeightedGraph graph,																													Instance instPair,																													HashMap alreadyAdded,																													MaxEnt classifier)	{		NodePair mentionPair = (NodePair)instPair.getSource();		Object node1 = mentionPair.getObject1();		Object node2 = mentionPair.getObject2();		VertexImpl v1 = (VertexImpl)alreadyAdded.get(node1);		VertexImpl v2 = (VertexImpl)alreadyAdded.get(node2);		if (v1 == null) {            System.err.println("Expected to have a vertex for node1" + node1.toString());			v1 = new VertexImpl(node1);			alreadyAdded.put(node1,v1);		}		if (v2 == null) {            System.err.println("Expected to have a vertex for node1" + node2.toString());			v2 = new VertexImpl(node2);			alreadyAdded.put(node2,v2);		}				// double  edgeVal = computeScore(classifier, instPair);		double  edgeVal = computeScore_NBest(classifier, instPair);		try {			if (node1 != null && node2 != null) {				graph.addEdge (v1, v2, edgeVal);			}		} catch (Exception e) {e.printStackTrace();}	}    /**     * This method assumes that the instance is a pair of Citation and that the     * Citation objects have an N-best list for the n-best segmentations. The     * edge value returned is simply the MAX score of any pair of citations.     *     * @param classifier     * @param origInstPair     * @return     */    private static double computeScore_NBest(MaxEnt classifier, Instance origInstPair) {        NodePair origNodePair = (NodePair)origInstPair.getSource();        Citation citation1 = (Citation)origNodePair.getObject1();        Citation citation2 = (Citation)origNodePair.getObject2();        Collection nBest1 = citation1.getNBest();        Collection nBest2 = citation2.getNBest();        String label;        if (origNodePair.getIdRel())            label = "yes";        else            label = "no";        Object name = origInstPair.getName();        Object source = origInstPair.getSource();        Pipe pipe = origInstPair.getPipe();        if (nBest1 == null || nBest2 == null) {            //System.err.println("Did not find n-best, using original");            //Instance instPair = new Instance (origNodePair, label, name, source, pipe);            double score = computeScore(classifier, origInstPair);						/*						            int i1 = citation1.getIndex();            int i2 = citation2.getIndex();            if (score < 0.0 && label == "yes") {                System.out.println(i1 + " " + i2 + " " + score + " " + label);                falseNegatives++;            } else if (score > 0.0 && label == "no") {                System.out.println(i1 + " " + i2 + " " + score + " " + label);                falsePositives++;								}*/            return score;        }        List scores = new ArrayList();        int i = 0, j = 0;        // make a new instance which is all pairs of two sets of n citations        for (Iterator iterator = nBest1.iterator(); iterator.hasNext();) {            j=0;            Citation nbest_citation1 = (Citation) iterator.next();            for (Iterator iterator2 = nBest2.iterator(); iterator2.hasNext();) {                Citation nbest_citation2 = (Citation) iterator2.next();                NodePair nodePair = new NodePair(nbest_citation1, nbest_citation2,                        origNodePair.getIdRel());                Instance instPair = new Instance (nodePair, label, name, null, pipe);//                System.out.println(i + ", " + j);//                System.out.println(nbest_citation1.rawstring);//                System.out.println(nbest_citation2.rawstring);                double score = computeScore(classifier, instPair);//                System.out.println(score);//                System.out.println(i + ", " + j + ": " + score);                scores.add(new Double(score));                j++;            }            i++;        }        return ((Double)Collections.max(scores)).doubleValue();    }    private static double computeScore(MaxEnt classifier, Instance instPair) {        // Include the feature weights according to each label        Classification classification = (Classification)classifier.classify(instPair);        Labeling labeling = classification.getLabeling();        /** NOTE:  THIS ASSUMES THERE ARE JUST TWO LABELS - IE CLASSIFIER IS         * BINARY */        double score = 0.0;        if (labeling.labelAtLocation(0).toString().equals("no")) {            score =  labeling.valueAtLocation(1)-labeling.valueAtLocation(0);        } else {            score =  labeling.valueAtLocation(0)-labeling.valueAtLocation(1);        }        return score;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -