Search in sources :

Example 1 with structures._ChildDoc4BaseWithPhi

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

the class ParentChildAnalyzer method loadChildDoc.

public void loadChildDoc(String fileName) {
    if (fileName == null || fileName.isEmpty())
        return;
    JSONObject json = LoadJSON(fileName);
    String content = Utils.getJSONValue(json, "content");
    String name = Utils.getJSONValue(json, "name");
    String parent = Utils.getJSONValue(json, "parent");
    String title = Utils.getJSONValue(json, "title");
    // 
    // _ChildDoc4BaseWithPhi d = new _ChildDoc4BaseWithPhi(m_corpus.getSize(),
    // name, "", content, 0);
    // _ChildDoc4BaseWithPhi_Hard d = new _ChildDoc4BaseWithPhi_Hard(m_corpus.getSize(), name, "", content, 0) ;
    // _ChildDoc4ChildPhi d = new _ChildDoc4ChildPhi(m_corpus.getSize(),
    // name,
    // "", content, 0);
    // _ChildDoc4TwoPhi d = new _ChildDoc4TwoPhi(m_corpus.getSize(), name, "", content, 0);
    // _ChildDoc4ThreePhi d = new _ChildDoc4ThreePhi(m_corpus.getSize(), name,
    // "", content, 0);
    // _ChildDoc4OneTopicProportion d = new _ChildDoc4OneTopicProportion(m_corpus.getSize(), name, "", content, 0);
    _ChildDoc d = new _ChildDoc(m_corpus.getSize(), name, "", content, 0);
    if (parentHashMap.containsKey(parent)) {
        if (AnalyzeDoc(d)) {
            // this is a valid child document
            // if (parentHashMap.containsKey(parent)) {
            _ParentDoc pDoc = parentHashMap.get(parent);
            d.setParentDoc(pDoc);
            pDoc.addChildDoc(d);
        } else {
        // System.err.format("filtering comments %s!\n", parent);
        }
    } else {
    // System.err.format("[Warning]Missing parent document %s!\n", parent);
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) JSONObject(json.JSONObject) structures._ParentDoc(structures._ParentDoc)

Example 2 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_C method collectChildStats.

@Override
protected void collectChildStats(_Doc d) {
    _ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
    _ParentDoc pDoc = cDoc.m_parentDoc;
    double pDocTopicSum = Utils.sumOfArray(pDoc.m_sstat);
    for (int k = 0; k < this.number_of_topics; k++) cDoc.m_xTopics[0][k] += cDoc.m_xTopicSstat[0][k] + d_alpha + cDoc.getMu() * pDoc.m_sstat[k] / pDocTopicSum;
    for (int x = 0; x < m_gamma.length; x++) cDoc.m_xProportion[x] += m_gamma[x] + cDoc.m_xSstat[x];
    for (int w = 0; w < vocabulary_size; w++) cDoc.m_xTopics[1][w] += cDoc.m_xTopicSstat[1][w];
    for (_Word w : d.getWords()) {
        w.collectXStats();
    }
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._ParentDoc(structures._ParentDoc) structures._Word(structures._Word)

Example 3 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_C method initialize_probability.

protected void initialize_probability(Collection<_Doc> collection) {
    createSpace();
    for (int i = 0; i < number_of_topics; i++) Arrays.fill(word_topic_sstat[i], d_beta);
    Arrays.fill(m_sstat, d_beta * vocabulary_size);
    for (_Doc d : collection) {
        if (d instanceof _ParentDoc) {
            d.setTopics4Gibbs(number_of_topics, 0);
            for (_Stn stnObj : d.getSentences()) stnObj.setTopicsVct(number_of_topics);
        } else if (d instanceof _ChildDoc4BaseWithPhi) {
            ((_ChildDoc4BaseWithPhi) d).createXSpace(number_of_topics, m_gamma.length, vocabulary_size, d_beta);
            ((_ChildDoc4BaseWithPhi) d).setTopics4Gibbs(number_of_topics, 0);
            computeMu4Doc((_ChildDoc) d);
        }
        if (d instanceof _ParentDoc) {
            for (_Word w : d.getWords()) {
                word_topic_sstat[w.getTopic()][w.getIndex()]++;
                m_sstat[w.getTopic()]++;
            }
        } else if (d instanceof _ChildDoc4BaseWithPhi) {
            for (_Word w : d.getWords()) {
                int xid = w.getX();
                int tid = w.getTopic();
                int wid = w.getIndex();
                // update global
                if (xid == 0) {
                    word_topic_sstat[tid][wid]++;
                    m_sstat[tid]++;
                }
            }
        }
    }
    imposePrior();
    m_statisticsNormalized = false;
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) structures._Word(structures._Word)

Example 4 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_C method calculate_log_likelihood4Child.

@Override
protected double calculate_log_likelihood4Child(_Doc d) {
    _ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
    double docLogLikelihood = 0.0;
    double gammaLen = Utils.sumOfArray(m_gamma);
    double cDocXSum = Utils.sumOfArray(cDoc.m_xSstat);
    // prepare compute the normalizers
    _SparseFeature[] fv = cDoc.getSparse();
    for (int i = 0; i < fv.length; i++) {
        int wid = fv[i].getIndex();
        double value = fv[i].getValue();
        double wordLogLikelihood = 0;
        for (int k = 0; k < number_of_topics; k++) {
            double wordPerTopicLikelihood = childWordByTopicProb(k, wid) * childTopicInDocProb(k, cDoc) * childXInDocProb(0, cDoc) / (cDocXSum + gammaLen);
            wordLogLikelihood += wordPerTopicLikelihood;
        }
        double wordPerTopicLikelihood = childLocalWordByTopicProb(wid, cDoc) * childXInDocProb(1, cDoc) / (cDocXSum + gammaLen);
        wordLogLikelihood += wordPerTopicLikelihood;
        if (Math.abs(wordLogLikelihood) < 1e-10) {
            System.out.println("wordLoglikelihood\t" + wordLogLikelihood);
            wordLogLikelihood += 1e-10;
        }
        wordLogLikelihood = Math.log(wordLogLikelihood);
        docLogLikelihood += value * wordLogLikelihood;
    }
    return docLogLikelihood;
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._SparseFeature(structures._SparseFeature)

Example 5 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_C method sampleInChildDoc.

protected void sampleInChildDoc(_Doc d) {
    _ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
    int wid, tid, xid;
    double normalizedProb;
    for (_Word w : cDoc.getWords()) {
        wid = w.getIndex();
        tid = w.getTopic();
        xid = w.getX();
        if (xid == 0) {
            cDoc.m_xTopicSstat[xid][tid]--;
            cDoc.m_xSstat[xid]--;
            cDoc.m_wordXStat.put(wid, cDoc.m_wordXStat.get(wid) - 1);
            if (m_collectCorpusStats) {
                word_topic_sstat[tid][wid]--;
                m_sstat[tid]--;
            }
        } else if (xid == 1) {
            cDoc.m_xTopicSstat[xid][wid]--;
            cDoc.m_xSstat[xid]--;
            cDoc.m_childWordSstat--;
        }
        normalizedProb = 0;
        double pLambdaZero = childXInDocProb(0, cDoc);
        double pLambdaOne = childXInDocProb(1, cDoc);
        for (tid = 0; tid < number_of_topics; tid++) {
            double pWordTopic = childWordByTopicProb(tid, wid);
            double pTopic = childTopicInDocProb(tid, cDoc);
            m_topicProbCache[tid] = pWordTopic * pTopic * pLambdaZero;
            normalizedProb += m_topicProbCache[tid];
        }
        double pWordTopic = childLocalWordByTopicProb(wid, cDoc);
        m_topicProbCache[tid] = pWordTopic * pLambdaOne;
        normalizedProb += m_topicProbCache[tid];
        normalizedProb *= m_rand.nextDouble();
        for (tid = 0; tid < m_topicProbCache.length; tid++) {
            normalizedProb -= m_topicProbCache[tid];
            if (normalizedProb <= 0)
                break;
        }
        if (tid == m_topicProbCache.length)
            tid--;
        if (tid < number_of_topics) {
            xid = 0;
            w.setX(xid);
            w.setTopic(tid);
            cDoc.m_xTopicSstat[xid][tid]++;
            cDoc.m_xSstat[xid]++;
            if (cDoc.m_wordXStat.containsKey(wid)) {
                cDoc.m_wordXStat.put(wid, cDoc.m_wordXStat.get(wid) + 1);
            } else {
                cDoc.m_wordXStat.put(wid, 1);
            }
            if (m_collectCorpusStats) {
                word_topic_sstat[tid][wid]++;
                m_sstat[tid]++;
            }
        } else if (tid == (number_of_topics)) {
            xid = 1;
            w.setX(xid);
            w.setTopic(tid);
            cDoc.m_xTopicSstat[xid][wid]++;
            cDoc.m_xSstat[xid]++;
            cDoc.m_childWordSstat++;
        }
    }
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._Word(structures._Word)

Aggregations

structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)15 structures._Word (structures._Word)10 structures._ParentDoc (structures._ParentDoc)9 structures._ChildDoc (structures._ChildDoc)7 structures._Doc (structures._Doc)4 structures._Stn (structures._Stn)4 File (java.io.File)2 structures._SparseFeature (structures._SparseFeature)2 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 JSONObject (json.JSONObject)1