Search in sources :

Example 46 with structures._Stn

use of structures._Stn in project IR_Base by Linda-sunshine.

the class LDAGibbs4AC_test method rankStn4ChildBySim.

// comment is a query, retrieve stn by topical similarity
protected HashMap<Integer, Double> rankStn4ChildBySim(_ParentDoc pDoc, _ChildDoc cDoc) {
    HashMap<Integer, Double> stnSimMap = new HashMap<Integer, Double>();
    for (_Stn stnObj : pDoc.getSentences()) {
        // double stnSim = computeSimilarity(cDoc.m_topics,
        // stnObj.m_topics);
        // stnSimMap.put(stnObj.getIndex()+1, stnSim);
        // 
        double stnKL = Utils.klDivergence(cDoc.m_topics, stnObj.m_topics);
        // double stnKL = Utils.KLsymmetric(cDoc.m_topics, stnObj.m_topics);
        // double stnKL = Utils.klDivergence(stnObj.m_topics,
        // cDoc.m_topics);
        stnSimMap.put(stnObj.getIndex() + 1, -stnKL);
    }
    return stnSimMap;
}
Also used : structures._Stn(structures._Stn) HashMap(java.util.HashMap)

Example 47 with structures._Stn

use of structures._Stn in project IR_Base by Linda-sunshine.

the class LDAGibbs4AC_test method printTopKChild4Stn.

protected void printTopKChild4Stn(int topK, _ParentDoc pDoc, File topKChildFolder) {
    File topKChild4PDocFolder = new File(topKChildFolder, pDoc.getName());
    if (!topKChild4PDocFolder.exists()) {
        // System.out.println("creating top K stn directory\t"+topKChild4PDocFolder);
        topKChild4PDocFolder.mkdir();
    }
    for (_Stn stnObj : pDoc.getSentences()) {
        HashMap<String, Double> likelihoodMap = rankChild4StnByLikelihood(stnObj, pDoc);
        String topChild4StnFile = (stnObj.getIndex() + 1) + ".txt";
        try {
            int i = 0;
            PrintWriter pw = new PrintWriter(new File(topKChild4PDocFolder, topChild4StnFile));
            for (Map.Entry<String, Double> e : sortHashMap4String(likelihoodMap, true)) {
                if (i == topK)
                    break;
                pw.print(e.getKey());
                pw.print("\t" + e.getValue());
                pw.println();
                i++;
            }
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : structures._Stn(structures._Stn) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 48 with structures._Stn

use of structures._Stn in project IR_Base by Linda-sunshine.

the class LDAGibbs4AC_test method printTopKChild4StnWithHybrid.

protected void printTopKChild4StnWithHybrid(String filePrefix, int topK) {
    String topKChild4StnFile = filePrefix + "topChild4Stn_hybrid.txt";
    try {
        PrintWriter pw = new PrintWriter(new File(topKChild4StnFile));
        m_LM.generateReferenceModel();
        for (_Doc d : m_trainSet) {
            if (d instanceof _ParentDoc) {
                _ParentDoc pDoc = (_ParentDoc) d;
                pw.println(pDoc.getName() + "\t" + pDoc.getSenetenceSize());
                for (_Stn stnObj : pDoc.getSentences()) {
                    // HashMap<String, Double> likelihoodMap =
                    // rankChild4StnByLikelihood(stnObj, pDoc);
                    HashMap<String, Double> likelihoodMap = rankChild4StnByHybrid(stnObj, pDoc);
                    // HashMap<String, Double> likelihoodMap =
                    // rankChild4StnByLanguageModel(stnObj, pDoc);
                    int i = 0;
                    pw.print((stnObj.getIndex() + 1) + "\t");
                    for (Map.Entry<String, Double> e : sortHashMap4String(likelihoodMap, true)) {
                        // if(i==topK)
                        // break;
                        pw.print(e.getKey());
                        pw.print(":" + e.getValue());
                        pw.print("\t");
                        i++;
                    }
                    pw.println();
                }
            }
        }
        pw.flush();
        pw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : structures._Stn(structures._Stn) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 49 with structures._Stn

use of structures._Stn in project IR_Base by Linda-sunshine.

the class LDAGibbs4AC_test method printParameter.

protected void printParameter(String parentParameterFile, String childParameterFile, ArrayList<_Doc> docList) {
    System.out.println("printing parameter");
    try {
        System.out.println(parentParameterFile);
        System.out.println(childParameterFile);
        PrintWriter parentParaOut = new PrintWriter(new File(parentParameterFile));
        PrintWriter childParaOut = new PrintWriter(new File(childParameterFile));
        for (_Doc d : docList) {
            if (d instanceof _ParentDoc) {
                parentParaOut.print(d.getName() + "\t");
                parentParaOut.print("topicProportion\t");
                for (int k = 0; k < number_of_topics; k++) {
                    parentParaOut.print(d.m_topics[k] + "\t");
                }
                for (_Stn stnObj : d.getSentences()) {
                    parentParaOut.print("sentence" + (stnObj.getIndex() + 1) + "\t");
                    for (int k = 0; k < number_of_topics; k++) {
                        parentParaOut.print(stnObj.m_topics[k] + "\t");
                    }
                }
                parentParaOut.println();
                for (_ChildDoc cDoc : ((_ParentDoc) d).m_childDocs) {
                    childParaOut.print(cDoc.getName() + "\t");
                    childParaOut.print("topicProportion\t");
                    for (int k = 0; k < number_of_topics; k++) {
                        childParaOut.print(cDoc.m_topics[k] + "\t");
                    }
                    childParaOut.println();
                }
            }
        }
        parentParaOut.flush();
        parentParaOut.close();
        childParaOut.flush();
        childParaOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 50 with structures._Stn

use of structures._Stn in project IR_Base by Linda-sunshine.

the class LDAGibbs4AC_test method rankChild4StnByLikelihood.

// stn is a query, retrieve comment by likelihood
protected HashMap<String, Double> rankChild4StnByLikelihood(_Stn stnObj, _ParentDoc pDoc) {
    HashMap<String, Double> childLikelihoodMap = new HashMap<String, Double>();
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        int cDocLen = cDoc.getTotalDocLength();
        double stnLogLikelihood = 0;
        for (_Word w : stnObj.getWords()) {
            double wordLikelihood = 0;
            int wid = w.getIndex();
            for (int k = 0; k < number_of_topics; k++) {
                wordLikelihood += (word_topic_sstat[k][wid] / m_sstat[k]) * (topicInDocProb(k, cDoc) / (d_alpha * number_of_topics + cDocLen));
            // wordLikelihood +=
            // topic_term_probabilty[k][wid]*cDoc.m_topics[k];
            }
            stnLogLikelihood += Math.log(wordLikelihood);
        }
        childLikelihoodMap.put(cDoc.getName(), stnLogLikelihood);
    }
    return childLikelihoodMap;
}
Also used : structures._ChildDoc(structures._ChildDoc) HashMap(java.util.HashMap) structures._Word(structures._Word)

Aggregations

structures._Stn (structures._Stn)46 structures._ChildDoc (structures._ChildDoc)33 structures._ParentDoc (structures._ParentDoc)27 structures._Doc (structures._Doc)22 HashMap (java.util.HashMap)19 File (java.io.File)17 PrintWriter (java.io.PrintWriter)17 structures._Word (structures._Word)16 FileNotFoundException (java.io.FileNotFoundException)15 structures._SparseFeature (structures._SparseFeature)12 structures._ParentDoc4DCM (structures._ParentDoc4DCM)6 Map (java.util.Map)5 structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 TokenizeResult (structures.TokenizeResult)2 TreeMap (java.util.TreeMap)1 MyPriorityQueue (structures.MyPriorityQueue)1 structures._ChildDoc4BaseWithPhi_Hard (structures._ChildDoc4BaseWithPhi_Hard)1