summaryrefslogtreecommitdiff
path: root/gi/pyp-topics/src/train.cc
blob: a8fd994c7fc8ad309897c9a84e2452df877d5a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// STL
#include <iostream>
#include <fstream>

// Boost
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/scoped_ptr.hpp>

// Local
#include "pyp-topics.hh"
#include "corpus.hh"
#include "contexts_corpus.hh"
#include "gzstream.hh"
#include "mt19937ar.h"

static const char *REVISION = "$Revision: 0.1 $";

// Namespaces
using namespace boost;
using namespace boost::program_options;
using namespace std;

int main(int argc, char **argv)
{
  std::cout << "Pitman Yor topic models: Copyright 2010 Phil Blunsom\n";
  std::cout << REVISION << '\n' << std::endl;

  ////////////////////////////////////////////////////////////////////////////////////////////
  // Command line processing
  variables_map vm; 

  // Command line processing
  options_description cmdline_specific("Command line specific options");
  cmdline_specific.add_options()
    ("help,h", "print help message")
    ("config,c", value<string>(), "config file specifying additional command line options")
    ;
  options_description generic("Allowed options");
  generic.add_options()
    ("documents,d", value<string>(), "file containing the documents")
    ("contexts", value<string>(), "file containing the documents in phrase contexts format")
    ("topics,t", value<int>()->default_value(50), "number of topics")
    ("document-topics-out,o", value<string>(), "file to write the document topics to")
    ("topic-words-out,w", value<string>(), "file to write the topic word distribution to")
    ("samples,s", value<int>()->default_value(10), "number of sampling passes through the data")
    ("test-corpus", value<string>(), "file containing the test data")
    ("backoff-paths", value<string>(), "file containing the term backoff paths")
    ("backoff-type", value<string>(), "backoff type: none|simple")
    ;
  options_description config_options, cmdline_options;
  config_options.add(generic);
  cmdline_options.add(generic).add(cmdline_specific);

  store(parse_command_line(argc, argv, cmdline_options), vm); 
  if (vm.count("config") > 0) {
    ifstream config(vm["config"].as<string>().c_str());
    store(parse_config_file(config, cmdline_options), vm); 
  }
  notify(vm);
  ////////////////////////////////////////////////////////////////////////////////////////////
  if (vm.count("contexts") > 0 && vm.count("documents") > 0) {
    cerr << "Only one of --documents or --contexts must be specified." << std::endl; 
    return 1; 
  }

  if (vm.count("documents") == 0 && vm.count("contexts") == 0) {
    cerr << "Please specify a file containing the documents." << endl;
    cout << cmdline_options << "\n"; 
    return 1;
  }

  if (vm.count("help")) { 
    cout << cmdline_options << "\n"; 
    return 1; 
  }

  // seed the random number generator
  //mt_init_genrand(time(0));

  PYPTopics model(vm["topics"].as<int>());

  // read the data
  boost::shared_ptr<Corpus> corpus;
  if (vm.count("documents") == 0 && vm.count("contexts") == 0) {
    corpus.reset(new Corpus);
    corpus->read(vm["documents"].as<string>());

    // read the backoff dictionary
    if (vm.count("backoff-paths"))
      model.set_backoff(vm["backoff-paths"].as<string>());

  }
  else {
    BackoffGenerator* backoff_gen=0;
    if (vm.count("backoff-type")) {
      if (vm["backoff-type"].as<std::string>() == "none") {
        backoff_gen = 0;
      }
      else if (vm["backoff-type"].as<std::string>() == "simple") {
        backoff_gen = new SimpleBackoffGenerator();
      }
      else {
        std::cerr << "Backoff type (--backoff-type) must be one of none|simple." << std::endl;
        return(1);
      }
    }

    boost::shared_ptr<ContextsCorpus> contexts_corpus(new ContextsCorpus);
    contexts_corpus->read_contexts(vm["contexts"].as<string>(), backoff_gen);
    corpus = contexts_corpus;
    model.set_backoff(contexts_corpus->backoff_index());

    if (backoff_gen) 
      delete backoff_gen;
  }

  // train the sampler
  model.sample(*corpus, vm["samples"].as<int>());

  if (vm.count("document-topics-out")) {
    ogzstream documents_out(vm["document-topics-out"].as<string>().c_str());
    //model.print_document_topics(documents_out);

    int document_id=0;
    for (Corpus::const_iterator corpusIt=corpus->begin(); 
         corpusIt != corpus->end(); ++corpusIt, ++document_id) {
      std::vector<int> unique_terms;
      for (Document::const_iterator docIt=corpusIt->begin();
           docIt != corpusIt->end(); ++docIt) {
        if (unique_terms.empty() || *docIt != unique_terms.back())
          unique_terms.push_back(*docIt);
      }
      documents_out << unique_terms.size();
      for (std::vector<int>::const_iterator termIt=unique_terms.begin();
           termIt != unique_terms.end(); ++termIt)
        documents_out << " " << *termIt << ":" << model.max(document_id, *termIt);
      documents_out << std::endl;
    }
    documents_out.close();
  }

  if (vm.count("topic-words-out")) {
    ogzstream topics_out(vm["topic-words-out"].as<string>().c_str());
    model.print_topic_terms(topics_out);
    topics_out.close();
  }

  if (vm.count("test-corpus")) {
    TestCorpus test_corpus;
    test_corpus.read(vm["test-corpus"].as<string>());
    ogzstream topics_out((vm["test-corpus"].as<string>() + ".topics.gz").c_str());

    for (TestCorpus::const_iterator corpusIt=test_corpus.begin(); 
         corpusIt != test_corpus.end(); ++corpusIt) {
      int index=0;
      for (DocumentTerms::const_iterator instanceIt=corpusIt->begin();
           instanceIt != corpusIt->end(); ++instanceIt, ++index) {
        int topic = model.max(instanceIt->doc, instanceIt->term);
        if (index != 0) topics_out << " ";
        topics_out << topic;
      }
      topics_out << std::endl;
    }
    topics_out.close();
  }
  std::cout << std::endl;

  return 0;
}