-
Notifications
You must be signed in to change notification settings - Fork 20
/
topic.h
executable file
·175 lines (123 loc) · 2.98 KB
/
topic.h
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
171
172
173
174
175
#ifndef TOPICH
#define TOPICH
#include <gsl/gsl_vector.h>
#include <math.h>
#include "utils.h"
#include "typedefs.h"
#include "hyperparameter.h"
#define MH_ETA_STDEV 0.005
#define MH_GAM_STDEV 0.005
/* === topic methods === */
/*
* update the count of a word in a topic
*
*/
void topic_update_word(topic* t, int w, double update);
/*
* update the document count in a topic
*
*/
void topic_update_doc_cnt(topic* t, double update);
/*
* write log probability in a file
*
*/
void topic_write_log_prob(topic* t, FILE* f);
/* === tree methods === */
/*
* allocate a new tree with a certain depth
*
*/
tree* tree_new(int depth,
int nwords,
gsl_vector* eta,
gsl_vector* gam,
double scaling_shape,
double scaling_scale);
/*
* add children to a node down to the depth of the tree;
* return the leaf of that path
*
*/
topic* tree_fill(topic* t);
/*
* add a child to a topic
*
*/
topic* topic_add_child(topic* t);
/*
* make a new topic
*
*/
topic* topic_new(int nwords, int level, topic* parent, tree* tr);
/*
* given a leaf with 0 instances, delete the leaf and all ancestors
* which also have 0 instances. (!!! use asserts here)
*
*/
void tree_prune(topic* t);
/*
* delete a node from the tree
*
*/
void delete_node(topic* t);
/*
* write a tree to file
*
*/
void tree_write_log_prob(tree* tree, FILE* f);
/*
* sample a document path from a tree
*
*/
void populate_prob_dfs(topic* node, doc* d, double* logsum, double* pprob, int root_level);
void tree_sample_doc_path(tree* tr, doc* d, short do_remove, int root_level);
/*
* sample a new path in the tree for a document
*
*/
void tree_sample_path_for_doc(tree* t, doc* d);
/*
* update the tree from an entire document
*
*/
void tree_update_from_doc(doc* d, double update, int root_level);
/*
* sample a leaf from the tree with populated probabilties
*
*/
topic* tree_sample_path(topic* node, double logsum);
topic* tree_sample_dfs(double r, topic* node, double* sum, double logsum);
void tree_add_doc_to_path(topic* node, doc* d, int root_level);
void tree_remove_doc_from_path(tree* tr, doc* d, int root_level);
/*
* write a tree to a file
*
*/
void write_tree(tree* tf, FILE* file);
void write_tree_levels(tree* tr, FILE* file);
void write_tree_level_dfs(topic* t, FILE* f);
void write_tree_topics_dfs(topic* t, FILE* f);
/*
* scores
*
*/
double gamma_score(topic* t);
double gamma_score_PY(topic* t, double gam_add);
double eta_score(topic* t);
double log_gamma_ratio(doc* d, topic* t, int level);
double log_gamma_ratio_new(doc* d, int level, double eta, int nterms);
void tree_mh_update_eta(tree* tr);
void dfs_sample_scaling(topic* t);
/*
* copying a tree
*
*/
void copy_topic(const topic* src, topic* dest);
void copy_tree_dfs(const topic* src, topic* dest);
tree * copy_tree(const tree* tr);
void free_tree(tree * tr);
void free_tree_dfs(topic * t);
int ntopics_in_tree(tree * tr);
int ntopics_in_tree_dfs(topic * t);
#endif