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

📄 meshsimilarity.java

📁 dragontoolkit用于机器学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    private double getFeatureSimilarity(String path1, String path2){
        double join, union;
        String[] sharedAncestors;
        sharedAncestors=getSharedAncestors(path1,path2);
        if(sharedAncestors==null) return 0;
        join = sharedAncestors.length;
        union = getUnionAncestors(path1,path2).length;
        return (join+1)/(union+1);
    }

    private double getKnappeSimilarity(String path1, String path2){
        double gen,speci,join;
        String[] ancestors1,ancestors2,sharedAncestors;

        if(path1.equals(path2)) return 1;
        ancestors1=getAncestors(path1);
        ancestors2=getAncestors(path2);
        sharedAncestors=getSharedAncestors(path1,path2);
        if(sharedAncestors ==null)return 0;
        join = sharedAncestors.length;
        gen=ancestors1.length;
        speci=ancestors2.length;
        return 0.5*(join/gen)+0.5*(join/speci);
    }

    private double getLeacockSimilarity(String path1, String path2){
        int minLen,pos;
        double value;
        String coParent,root;
        Token token;
        coParent = getCommonParent(path1,path2);
        if (coParent == null)
            return -1;
        minLen = getMinLen(path1,path2,coParent);
        root =getRoot(coParent);
        pos=maxTaxoLenList.binarySearch(root);
        token =(Token) maxTaxoLenList.get(pos);
        value = (double)(minLen+1)/2/token.getWeight();
        return -Math.log(value);
    }

    private double getJingSimilarity(String path1, String path2){
        double dist,coParentDepth;
        String coParent;

        coParent = getCommonParent(path1,path2);
        if(coParent==null) return -1;
        coParentDepth=getDepth(coParent);
        dist=1/coParentDepth;
        dist=dist*0.5;
        dist=dist*((getDepth(path1)-coParentDepth)/getMaxPathLen(path1)+(getDepth(path2)-coParentDepth)/getMaxPathLen(path2));
        return 1-dist;
    }

    private double getLinSimilarity(String path1, String path2){
        String coParent;
        if(path1.equals(path2))
            return 1;
        coParent = getCommonParent(path1,path2);
        if(coParent==null) return -1;
        return 2*meshNodeList.lookup(coParent).getWeight()/(meshNodeList.lookup(path1).getWeight()+meshNodeList.lookup(path2).getWeight());
    }

    private double getJiangSimilarity(String path1, String path2){
        String coParent;

        coParent = getCommonParent(path1, path2);
        if (coParent == null)
            return -1;
        return -meshNodeList.lookup(path1).getWeight() - meshNodeList.lookup(path2).getWeight() +
            2 * meshNodeList.lookup(coParent).getWeight();
    }

    private double getResinkSimilarity(String path1, String path2){
        double dist;
        String coParent;

        if(path1.equals(path2)){
            dist = -meshNodeList.lookup(path1).getWeight();
            if (dist == 0)
                dist = 0.11;
                return dist;
        }
        coParent = getCommonParent(path1,path2);
        if(coParent==null) return -1;
        dist= -meshNodeList.lookup(coParent).getWeight();
        if (dist == 0)
                dist = 0.11;
        return dist;
    }

    private double getMaoSimilarity(String path1, String path2){
        int c1,c2;
        double len;

        len=getMinLen(path1,path2);
        if (len == 0)
            return 1;
        else if (len == -1)
            return -1;
        c1 = getDescendantNum(path1);
        c2 = getDescendantNum(path2);
        return 0.9/len/Math.log(2+c1+c2);
    }

   public MeshNodeList getMeshNodeList(){
       return meshNodeList;
   }

   private SortedArray genMaxTaxoLenList() {
       int i, depth;
       double max;
       SortedArray maxTaxoLenList;
       Token token;
       MeshNode meshNode;
       String curRoot, oldRoot, maxPath;

       maxTaxoLenList = new SortedArray(107, new AlphabetaComparator());
       oldRoot = "A01";
       maxPath = "A01";
       max = 0;
       for (i = 0; i < meshNodeList.size(); i++) {
           meshNode = (MeshNode) meshNodeList.get(i);
           curRoot = getRoot(meshNode.getPath());
           if (!curRoot.equals(oldRoot)) {
               token = new Token(getRoot(maxPath));
               token.setWeight(max);
               maxTaxoLenList.add(token);
               max = 0;
               oldRoot = curRoot;
           }
           depth = getDepth(meshNode.getPath());
           if (max < depth) {
               max = depth;
               maxPath = meshNode.getPath();
           }
           oldRoot = curRoot;
       }
       maxTaxoLenList.add(new Token(getRoot(maxPath)));
       ((Token)maxTaxoLenList.get(maxTaxoLenList.size()-1)).setWeight(max);
       for(i=0;i<maxTaxoLenList.size();i++){
           token =((Token)maxTaxoLenList.get(i));
       }
       return maxTaxoLenList;
   }

    public void trainWeightByInformationContent(ArrayList trainings) {
        MeshNode node;
        String cur;
        double ic;
        int i;

        for(i=0;i<meshNodeList.size();i++){
            node = (MeshNode) meshNodeList.get(i);
            node.setFrequency(0);
        }

        for (i = 0; i < trainings.size(); i++) {
            node=(MeshNode)trainings.get(i);
            cur=node.getPath();
            meshNodeList.lookup(cur).addFrequency(node.getFrequency());
            while ( getParent(cur) != null){
                cur = getParent(cur);
                meshNodeList.lookup(cur).addFrequency(node.getFrequency());
            }
        }

        for(i=0;i<meshNodeList.size();i++){
            node = (MeshNode) meshNodeList.get(i);
            if(node.getFrequency()==0)
                node.setWeight(0);
            else{
                ic = (double) node.getFrequency() / meshNodeList.lookup(getRoot(node.getPath())).getFrequency();
                node.setWeight(Math.log(ic));
            }
        }
    }

    public void genSimilarityMatrix(String matrixFile, double threshold) {
        DoubleSuperSparseMatrix matrix;
        MeshNode meshNodeI,meshNodeJ;
        int i, j,start,end;
        double dist;

        start=0;
        end=0;
        matrix= new DoubleSuperSparseMatrix(matrixFile, false, false);
        for(i=0;i<meshNodeList.size();i++){
            meshNodeI = (MeshNode) meshNodeList.get(i);
            if(meshNodeI.getPath().indexOf(".")<0){
                start = i;
                end = i+meshNodeI.getDescendantNum();
                System.out.println(new Date()+" "+start+" "+end);
            }
            for(j=start;j<end;j++){
                meshNodeJ = (MeshNode)meshNodeList.get(j);
                dist = getSimilarity(meshNodeI.getPath(),meshNodeJ.getPath(),similarityMode);
                if(dist>threshold){
                    matrix.add(i,j,dist);
                    matrix.add(j,i,dist);
                }
            }
        }
        matrix.finalizeData();
    }

    private double getSimilarity(String path1, String path2, int mode){
        if (mode == MIN_LEN) {
            return getMinLen(path1,path2);
        }
        else if (mode == WU_PALMER) {
            return getWuPalmerSimilarity(path1,path2);
        }
        else if (mode == LI) {
            return getLiSimilarity(path1,path2,0.6,0.2);
        }
        else if (mode == LEACOCK) {
            return getLeacockSimilarity(path1,path2);
        }
        else if (mode == JING) {
            return getJingSimilarity(path1,path2);
        }
        else if (mode == LIN) {
            return getLinSimilarity(path1,path2);
        }
        else if (mode == JIANG) {
            return getJiangSimilarity(path1,path2);
        }
        else if (mode == RESINK) {
            return getResinkSimilarity(path1,path2);
        }
        else if (mode == MAO) {
            return getMaoSimilarity(path1,path2);
        }
        else if(mode==FEATURE){
            return getFeatureSimilarity(path1,path2);
        }
        else if(mode==KNAPPE){
            return getKnappeSimilarity(path1,path2);
        }
        return -1;
    }

    public double getSimilarity(String path1, String path2){
        return getSimilarity(path1,path2,similarityMode);
    }

    public double getSimilarity(Term a, Term b){
        return getSimilarity(a.getCUI(),b.getCUI(),similarityMode);
    }
}

⌨️ 快捷键说明

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