Search in sources :

Example 1 with structures._Word

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

the class DCMLDA4AC_test method printChildTopicAssignment.

protected void printChildTopicAssignment(_Doc d, File topicFolder) {
    String topicAssignmentFile = d.getName() + ".txt";
    try {
        PrintWriter pw = new PrintWriter(new File(topicFolder, topicAssignmentFile));
        for (_Word w : d.getWords()) {
            int wid = w.getIndex();
            int tid = w.getTopic();
            String featureName = m_corpus.getFeature(wid);
            pw.print(featureName + ":" + tid + "\t");
        }
        pw.flush();
        pw.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) structures._Word(structures._Word) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with structures._Word

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

the class LDAGibbs4AC method initialize_probability.

protected void initialize_probability(Collection<_Doc> collection) {
    createSpace();
    for (int i = 0; i < number_of_topics; i++) {
        Arrays.fill(topic_term_probabilty[i], 0);
        Arrays.fill(word_topic_sstat[i], d_beta);
    }
    Arrays.fill(m_sstat, d_beta * vocabulary_size);
    for (_Doc d : collection) {
        if (d instanceof _ParentDoc) {
            for (_Stn stnObj : d.getSentences()) {
                stnObj.setTopicsVct(number_of_topics);
            }
            d.setTopics4Gibbs(number_of_topics, d_alpha);
        } else if (d instanceof _ChildDoc) {
            ((_ChildDoc) d).setTopics4Gibbs_LDA(number_of_topics, d_alpha);
        }
        for (_Word w : d.getWords()) {
            word_topic_sstat[w.getTopic()][w.getIndex()]++;
            m_sstat[w.getTopic()]++;
        }
    }
    imposePrior();
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) structures._Word(structures._Word)

Example 3 with structures._Word

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

the class corrLDA_Gibbs method initialize_probability.

@Override
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) {
            for (_Stn stnObj : d.getSentences()) {
                stnObj.setTopicsVct(number_of_topics);
            }
            d.setTopics4Gibbs(number_of_topics, 0);
        } else if (d instanceof _ChildDoc) {
            ((_ChildDoc) d).setTopics4Gibbs_LDA(number_of_topics, 0);
        }
        for (_Word w : d.getWords()) {
            word_topic_sstat[w.getTopic()][w.getIndex()]++;
            m_sstat[w.getTopic()]++;
        }
    }
    imposePrior();
    m_statisticsNormalized = false;
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) structures._Word(structures._Word)

Example 4 with structures._Word

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

the class corrLDA_Gibbs method sampleInChildDoc.

protected void sampleInChildDoc(_Doc d) {
    _ChildDoc cDoc = (_ChildDoc) d;
    int wid, tid;
    double normalizedProb = 0;
    for (_Word w : cDoc.getWords()) {
        wid = w.getIndex();
        tid = w.getTopic();
        cDoc.m_sstat[tid]--;
        if (m_collectCorpusStats) {
            word_topic_sstat[tid][wid]--;
            m_sstat[tid]--;
        }
        normalizedProb = 0;
        for (tid = 0; tid < number_of_topics; tid++) {
            double pWordTopic = childWordByTopicProb(tid, wid);
            double pTopicDoc = childTopicInDocProb(tid, cDoc);
            m_topicProbCache[tid] = pWordTopic * pTopicDoc;
            normalizedProb += m_topicProbCache[tid];
        }
        normalizedProb *= m_rand.nextDouble();
        for (tid = 0; tid < number_of_topics; tid++) {
            normalizedProb -= m_topicProbCache[tid];
            if (normalizedProb < 0)
                break;
        }
        if (tid == number_of_topics)
            tid--;
        w.setTopic(tid);
        cDoc.m_sstat[tid]++;
        if (m_collectCorpusStats) {
            word_topic_sstat[tid][wid]++;
            m_sstat[tid]++;
        }
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Word(structures._Word)

Example 5 with structures._Word

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

the class languageModelBaseLine method generateReferenceModelWithXVal.

protected void generateReferenceModelWithXVal() {
    m_allWordFrequencyWithXVal = 0;
    for (_Doc d : m_corpus.getCollection()) {
        if (d instanceof _ParentDoc) {
            for (_SparseFeature fv : d.getSparse()) {
                int wid = fv.getIndex();
                double val = fv.getValue();
                m_allWordFrequencyWithXVal += val;
                if (m_wordSstat.containsKey(wid)) {
                    double oldVal = m_wordSstat.get(wid);
                    m_wordSstat.put(wid, oldVal + val);
                } else {
                    m_wordSstat.put(wid, val);
                }
            }
        } else {
            double docLenWithXVal = 0;
            for (_Word w : d.getWords()) {
                // double xProportion = w.getXProb();
                int wid = w.getIndex();
                double val = 0;
                if (((_ChildDoc) d).m_wordXStat.containsKey(wid)) {
                    val = ((_ChildDoc) d).m_wordXStat.get(wid);
                }
                docLenWithXVal += val;
                m_allWordFrequencyWithXVal += val;
                if (m_wordSstat.containsKey(wid)) {
                    double oldVal = m_wordSstat.get(wid);
                    m_wordSstat.put(wid, oldVal + val);
                } else {
                    m_wordSstat.put(wid, val);
                }
            }
            ((_ChildDoc) d).setChildDocLenWithXVal(docLenWithXVal);
        }
    }
    for (int wid : m_wordSstat.keySet()) {
        double val = m_wordSstat.get(wid);
        double prob = val / m_allWordFrequencyWithXVal;
        m_wordSstat.put(wid, prob);
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) structures._SparseFeature(structures._SparseFeature) structures._Word(structures._Word)

Aggregations

structures._Word (structures._Word)69 structures._ChildDoc (structures._ChildDoc)18 File (java.io.File)16 FileNotFoundException (java.io.FileNotFoundException)15 PrintWriter (java.io.PrintWriter)15 structures._ParentDoc (structures._ParentDoc)14 structures._Doc (structures._Doc)12 structures._Stn (structures._Stn)11 structures._ParentDoc4DCM (structures._ParentDoc4DCM)10 structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)9 HashMap (java.util.HashMap)5 structures._Doc4DCMLDA (structures._Doc4DCMLDA)4 structures._Doc4SparseDCMLDA (structures._Doc4SparseDCMLDA)4 structures._SparseFeature (structures._SparseFeature)3 Feature (Classifier.supervised.liblinear.Feature)1 FeatureNode (Classifier.supervised.liblinear.FeatureNode)1 Model (Classifier.supervised.liblinear.Model)1 Parameter (Classifier.supervised.liblinear.Parameter)1 Problem (Classifier.supervised.liblinear.Problem)1 SolverType (Classifier.supervised.liblinear.SolverType)1