lda-ruby 0.4.0-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +61 -0
  3. data/Gemfile +9 -0
  4. data/README.md +157 -0
  5. data/VERSION.yml +5 -0
  6. data/docs/modernization-handoff.md +190 -0
  7. data/docs/porting-strategy.md +127 -0
  8. data/docs/precompiled-platform-policy.md +68 -0
  9. data/docs/release-runbook.md +157 -0
  10. data/ext/lda-ruby/cokus.c +145 -0
  11. data/ext/lda-ruby/cokus.h +27 -0
  12. data/ext/lda-ruby/extconf.rb +13 -0
  13. data/ext/lda-ruby/lda-alpha.c +96 -0
  14. data/ext/lda-ruby/lda-alpha.h +21 -0
  15. data/ext/lda-ruby/lda-data.c +67 -0
  16. data/ext/lda-ruby/lda-data.h +14 -0
  17. data/ext/lda-ruby/lda-inference.c +1023 -0
  18. data/ext/lda-ruby/lda-inference.h +63 -0
  19. data/ext/lda-ruby/lda-model.c +345 -0
  20. data/ext/lda-ruby/lda-model.h +31 -0
  21. data/ext/lda-ruby/lda.h +54 -0
  22. data/ext/lda-ruby/utils.c +111 -0
  23. data/ext/lda-ruby/utils.h +18 -0
  24. data/ext/lda-ruby-rust/Cargo.toml +12 -0
  25. data/ext/lda-ruby-rust/README.md +48 -0
  26. data/ext/lda-ruby-rust/extconf.rb +123 -0
  27. data/ext/lda-ruby-rust/src/lib.rs +456 -0
  28. data/lda-ruby.gemspec +78 -0
  29. data/lib/lda-ruby/backends/base.rb +129 -0
  30. data/lib/lda-ruby/backends/native.rb +158 -0
  31. data/lib/lda-ruby/backends/pure_ruby.rb +613 -0
  32. data/lib/lda-ruby/backends/rust.rb +226 -0
  33. data/lib/lda-ruby/backends.rb +58 -0
  34. data/lib/lda-ruby/config/stopwords.yml +571 -0
  35. data/lib/lda-ruby/corpus/corpus.rb +45 -0
  36. data/lib/lda-ruby/corpus/data_corpus.rb +22 -0
  37. data/lib/lda-ruby/corpus/directory_corpus.rb +25 -0
  38. data/lib/lda-ruby/corpus/text_corpus.rb +27 -0
  39. data/lib/lda-ruby/document/data_document.rb +30 -0
  40. data/lib/lda-ruby/document/document.rb +40 -0
  41. data/lib/lda-ruby/document/text_document.rb +39 -0
  42. data/lib/lda-ruby/lda.so +0 -0
  43. data/lib/lda-ruby/rust_build_policy.rb +21 -0
  44. data/lib/lda-ruby/version.rb +5 -0
  45. data/lib/lda-ruby/vocabulary.rb +46 -0
  46. data/lib/lda-ruby.rb +413 -0
  47. data/lib/lda_ruby_rust.so +0 -0
  48. data/license.txt +504 -0
  49. data/test/backend_compatibility_test.rb +146 -0
  50. data/test/backends_selection_test.rb +100 -0
  51. data/test/data/docs.dat +46 -0
  52. data/test/data/sample.rb +20 -0
  53. data/test/data/wiki-test-docs.yml +123 -0
  54. data/test/gemspec_test.rb +27 -0
  55. data/test/lda_ruby_test.rb +319 -0
  56. data/test/packaged_gem_smoke_test.rb +33 -0
  57. data/test/release_scripts_test.rb +54 -0
  58. data/test/rust_build_policy_test.rb +23 -0
  59. data/test/simple_pipeline_test.rb +22 -0
  60. data/test/simple_yaml.rb +17 -0
  61. data/test/test_helper.rb +10 -0
  62. metadata +111 -0
@@ -0,0 +1,157 @@
1
+ # Release Runbook (Phase 5A + 5B)
2
+
3
+ This runbook defines the maintainer workflow for shipping `lda-ruby` source and precompiled platform gem releases.
4
+
5
+ Authoritative platform/support policy is maintained in `docs/precompiled-platform-policy.md`.
6
+
7
+ ## Scope
8
+
9
+ - Release artifact types:
10
+ - source gem: `pkg/lda-ruby-<version>.gem`
11
+ - precompiled gems (current targets are defined in `docs/precompiled-platform-policy.md`)
12
+ - Release trigger: git tag (`vX.Y.Z`) with matching version files
13
+ - Publish targets:
14
+ - RubyGems (`gem push`)
15
+ - GitHub Releases (gem + checksum attachment)
16
+
17
+ ## Prerequisites
18
+
19
+ 1. Access:
20
+ - push/tag rights on `master`
21
+ - access to GitHub Actions environments for release approvals
22
+ - RubyGems owner access for `lda-ruby`
23
+ 2. Local tooling:
24
+ - Ruby 3.2+ with Bundler
25
+ - Rust toolchain (`cargo`) for local precompiled-gem build checks
26
+ - `libclang` available to Rust bindgen
27
+ - Docker (recommended for reproducible checks)
28
+ 3. Repository state:
29
+ - release commit merged to `master`
30
+ - clean working tree
31
+ - version files in sync
32
+
33
+ ## Required Secrets and Environments
34
+
35
+ GitHub repository secret:
36
+
37
+ - `RUBYGEMS_API_KEY`: API key with push rights for `lda-ruby`.
38
+
39
+ GitHub Actions environment:
40
+
41
+ - `release`: protect this environment with required reviewer approval.
42
+ - Both publish jobs in `.github/workflows/release.yml` are bound to `release`.
43
+
44
+ ## Release Preparation
45
+
46
+ 1. Prepare and update release files:
47
+
48
+ ```bash
49
+ ./bin/release-prepare 0.4.0
50
+ ```
51
+
52
+ 2. Review changes:
53
+ - `VERSION.yml`
54
+ - `lib/lda-ruby/version.rb`
55
+ - `CHANGELOG.md`
56
+
57
+ 3. Validate full release checks locally:
58
+
59
+ ```bash
60
+ SKIP_DOCKER=1 ./bin/release-preflight
61
+ ./bin/test-packaged-gem-manifest
62
+ ```
63
+
64
+ 4. Validate local precompiled gem flow for your current host platform:
65
+
66
+ ```bash
67
+ ./bin/release-precompiled-artifacts --tag v0.4.0 --skip-preflight
68
+ ```
69
+
70
+ Note: `release-precompiled-artifacts` only supports building for the current host platform (no cross-compilation).
71
+
72
+ 5. Commit and merge to `master`.
73
+
74
+ ## Dry-Run Path (No Publish)
75
+
76
+ Use `workflow_dispatch` with `publish=false`.
77
+
78
+ Behavior:
79
+
80
+ - runs release validation and artifact build
81
+ - uploads source + precompiled `pkg/lda-ruby-*.gem` and checksum files as workflow artifacts
82
+ - does not push to RubyGems
83
+ - does not create a GitHub release
84
+
85
+ Latest verified dry-run reference:
86
+
87
+ - date: 2026-02-25
88
+ - workflow run: `https://github.com/ealdent/lda-ruby/actions/runs/22382692416`
89
+ - dispatch parameters: `release_tag=v0.4.0`, `publish=false`
90
+ - result: success across `validate`, `build_artifacts`, and full `build_precompiled_artifacts` matrix
91
+
92
+ Optional local dry-run equivalent:
93
+
94
+ ```bash
95
+ ./bin/release-artifacts --tag v0.4.0
96
+ ./bin/release-precompiled-artifacts --tag v0.4.0 --skip-preflight
97
+ ```
98
+
99
+ ## Publish Path (Tag-Driven)
100
+
101
+ 1. Ensure the release commit is on `master`.
102
+ 2. Create and push the release tag:
103
+
104
+ ```bash
105
+ git checkout master
106
+ git pull --ff-only
107
+ git tag -a v0.4.0 -m "Release v0.4.0"
108
+ git push origin v0.4.0
109
+ ```
110
+
111
+ 3. Monitor `.github/workflows/release.yml`:
112
+ - `validate`
113
+ - `build_artifacts`
114
+ - `build_precompiled_artifacts` (linux + macOS matrix)
115
+ - environment-gated `publish_rubygems`
116
+ - environment-gated `publish_github_release`
117
+ 4. Approve the protected `release` environment when prompted.
118
+ 5. Confirm published outputs:
119
+ - RubyGems shows `lda-ruby` `0.4.0` source gem and platform gems
120
+ - GitHub release `v0.4.0` exists with all gem and `.sha256` attachments
121
+
122
+ ## Rollback and Recovery
123
+
124
+ If publish fails before RubyGems push:
125
+
126
+ 1. Fix issue on `master`.
127
+ 2. Delete and recreate the tag only if the broken tag did not produce public artifacts:
128
+ - `git tag -d vX.Y.Z`
129
+ - `git push origin :refs/tags/vX.Y.Z`
130
+ 3. Re-tag and re-run release.
131
+
132
+ If RubyGems push succeeds but GitHub release fails:
133
+
134
+ 1. Re-run only the GitHub release path by re-running the workflow job after fix.
135
+ 2. Do not re-push gem for the same version.
136
+
137
+ If an incorrect gem is published:
138
+
139
+ 1. Yank from RubyGems:
140
+
141
+ ```bash
142
+ gem yank lda-ruby -v X.Y.Z
143
+ ```
144
+
145
+ 2. Publish a corrective version (for example `X.Y.(Z+1)`), do not re-use yanked version numbers.
146
+ 3. Update `CHANGELOG.md` and release notes to document the correction.
147
+
148
+ ## Troubleshooting
149
+
150
+ - `Could not find 'bundler'`: install the Bundler version pinned in `Gemfile.lock`.
151
+ - `cargo not found` in rust-enabled checks: ensure Rust toolchain is installed or run in Docker.
152
+ - `libclang` not found while building precompiled gems: install LLVM/libclang and set `LIBCLANG_PATH` if needed.
153
+ - Linux `Install Rust bindgen dependencies` can take several minutes on fresh runners due apt package index and package installs.
154
+ - macOS Rust link errors (`symbol(s) not found` for Ruby APIs): ensure build path preserves `-C link-arg=-Wl,-undefined,dynamic_lookup` in `RUSTFLAGS`.
155
+ - Tag/version mismatch: run `./bin/check-version-sync --tag vX.Y.Z`.
156
+ - Artifact mismatch during release: rebuild with `./bin/release-artifacts --tag vX.Y.Z`.
157
+ - Precompiled artifact mismatch: rebuild with `./bin/release-precompiled-artifacts --tag vX.Y.Z --skip-preflight`.
@@ -0,0 +1,145 @@
1
+ // This is the ``Mersenne Twister'' random number generator MT19937, which
2
+ // generates pseudorandom integers uniformly distributed in 0..(2^32 - 1)
3
+ // starting from any odd seed in 0..(2^32 - 1). This version is a recode
4
+ // by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by
5
+ // Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in
6
+ // July-August 1997).
7
+ //
8
+ // Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha
9
+ // running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to
10
+ // generate 300 million random numbers; after recoding: 24.0 sec. for the same
11
+ // (i.e., 46.5% of original time), so speed is now about 12.5 million random
12
+ // number generations per second on this machine.
13
+ //
14
+ // According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html>
15
+ // (and paraphrasing a bit in places), the Mersenne Twister is ``designed
16
+ // with consideration of the flaws of various existing generators,'' has
17
+ // a period of 2^19937 - 1, gives a sequence that is 623-dimensionally
18
+ // equidistributed, and ``has passed many stringent tests, including the
19
+ // die-hard test of G. Marsaglia and the load test of P. Hellekalek and
20
+ // S. Wegenkittl.'' It is efficient in memory usage (typically using 2506
21
+ // to 5012 bytes of static data, depending on data type sizes, and the code
22
+ // is quite short as well). It generates random numbers in batches of 624
23
+ // at a time, so the caching and pipelining of modern systems is exploited.
24
+ // It is also divide- and mod-free.
25
+ //
26
+ // This library is free software; you can redistribute it and/or modify it
27
+ // under the terms of the GNU Library General Public License as published by
28
+ // the Free Software Foundation (either version 2 of the License or, at your
29
+ // option, any later version). This library is distributed in the hope that
30
+ // it will be useful, but WITHOUT ANY WARRANTY, without even the implied
31
+ // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
32
+ // the GNU Library General Public License for more details. You should have
33
+ // received a copy of the GNU Library General Public License along with this
34
+ // library; if not, write to the Free Software Foundation, Inc., 59 Temple
35
+ // Place, Suite 330, Boston, MA 02111-1307, USA.
36
+ //
37
+ // The code as Shawn received it included the following notice:
38
+ //
39
+ // Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When
40
+ // you use this, send an e-mail to <matumoto@math.keio.ac.jp> with
41
+ // an appropriate reference to your work.
42
+ //
43
+ // It would be nice to CC: <Cokus@math.washington.edu> when you write.
44
+ //
45
+
46
+ #include "cokus.h"
47
+
48
+ static uint32 state[N+1]; // state vector + 1 extra to not violate ANSI C
49
+ static uint32 *next; // next random value is computed from here
50
+ static int left = -1; // can *next++ this many times before reloading
51
+
52
+ void seedMT(uint32 seed)
53
+ {
54
+ //
55
+ // We initialize state[0..(N-1)] via the generator
56
+ //
57
+ // x_new = (69069 * x_old) mod 2^32
58
+ //
59
+ // from Line 15 of Table 1, p. 106, Sec. 3.3.4 of Knuth's
60
+ // _The Art of Computer Programming_, Volume 2, 3rd ed.
61
+ //
62
+ // Notes (SJC): I do not know what the initial state requirements
63
+ // of the Mersenne Twister are, but it seems this seeding generator
64
+ // could be better. It achieves the maximum period for its modulus
65
+ // (2^30) iff x_initial is odd (p. 20-21, Sec. 3.2.1.2, Knuth); if
66
+ // x_initial can be even, you have sequences like 0, 0, 0, ...;
67
+ // 2^31, 2^31, 2^31, ...; 2^30, 2^30, 2^30, ...; 2^29, 2^29 + 2^31,
68
+ // 2^29, 2^29 + 2^31, ..., etc. so I force seed to be odd below.
69
+ //
70
+ // Even if x_initial is odd, if x_initial is 1 mod 4 then
71
+ //
72
+ // the lowest bit of x is always 1,
73
+ // the next-to-lowest bit of x is always 0,
74
+ // the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
75
+ // the 3rd-from-lowest bit of x 4-cycles ... 0 1 1 0 0 1 1 0 ... ,
76
+ // the 4th-from-lowest bit of x has the 8-cycle ... 0 0 0 1 1 1 1 0 ... ,
77
+ // ...
78
+ //
79
+ // and if x_initial is 3 mod 4 then
80
+ //
81
+ // the lowest bit of x is always 1,
82
+ // the next-to-lowest bit of x is always 1,
83
+ // the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
84
+ // the 3rd-from-lowest bit of x 4-cycles ... 0 0 1 1 0 0 1 1 ... ,
85
+ // the 4th-from-lowest bit of x has the 8-cycle ... 0 0 1 1 1 1 0 0 ... ,
86
+ // ...
87
+ //
88
+ // The generator's potency (min. s>=0 with (69069-1)^s = 0 mod 2^32) is
89
+ // 16, which seems to be alright by p. 25, Sec. 3.2.1.3 of Knuth. It
90
+ // also does well in the dimension 2..5 spectral tests, but it could be
91
+ // better in dimension 6 (Line 15, Table 1, p. 106, Sec. 3.3.4, Knuth).
92
+ //
93
+ // Note that the random number user does not see the values generated
94
+ // here directly since reloadMT() will always munge them first, so maybe
95
+ // none of all of this matters. In fact, the seed values made here could
96
+ // even be extra-special desirable if the Mersenne Twister theory says
97
+ // so-- that's why the only change I made is to restrict to odd seeds.
98
+ //
99
+
100
+ register uint32 x = (seed | 1U) & 0xFFFFFFFFU, *s = state;
101
+ register int j;
102
+
103
+ for(left=0, *s++=x, j=N; --j;
104
+ *s++ = (x*=69069U) & 0xFFFFFFFFU);
105
+ }
106
+
107
+
108
+ uint32 reloadMT(void)
109
+ {
110
+ register uint32 *p0=state, *p2=state+2, *pM=state+M, s0, s1;
111
+ register int j;
112
+
113
+ if(left < -1)
114
+ seedMT(4357U);
115
+
116
+ left=N-1, next=state+1;
117
+
118
+ for(s0=state[0], s1=state[1], j=N-M+1; --j; s0=s1, s1=*p2++)
119
+ *p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
120
+
121
+ for(pM=state, j=M; --j; s0=s1, s1=*p2++)
122
+ *p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
123
+
124
+ s1=state[0], *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
125
+ s1 ^= (s1 >> 11);
126
+ s1 ^= (s1 << 7) & 0x9D2C5680U;
127
+ s1 ^= (s1 << 15) & 0xEFC60000U;
128
+ return(s1 ^ (s1 >> 18));
129
+ }
130
+
131
+ uint32 randomMT(void)
132
+ {
133
+ uint32 y;
134
+
135
+ if(--left < 0)
136
+ return(reloadMT());
137
+
138
+ y = *next++;
139
+ y ^= (y >> 11);
140
+ y ^= (y << 7) & 0x9D2C5680U;
141
+ y ^= (y << 15) & 0xEFC60000U;
142
+ y ^= (y >> 18);
143
+ return(y);
144
+ }
145
+
@@ -0,0 +1,27 @@
1
+ #ifndef COKUS_H
2
+ #define COKUS_H
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+
7
+ //
8
+ // uint32 must be an unsigned integer type capable of holding at least 32
9
+ // bits; exactly 32 should be fastest, but 64 is better on an Alpha with
10
+ // GCC at -O3 optimization so try your options and see what's best for you
11
+ //
12
+
13
+ typedef unsigned long uint32;
14
+
15
+ #define N (624) // length of state vector
16
+ #define M (397) // a period parameter
17
+ #define K (0x9908B0DFU) // a magic constant
18
+ #define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u
19
+ #define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u
20
+ #define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u
21
+ #define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v
22
+
23
+ void seedMT(uint32 seed);
24
+ uint32 reloadMT(void);
25
+ uint32 randomMT(void);
26
+
27
+ #endif
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ extension_name = "lda-ruby/lda"
6
+ dir_config(extension_name)
7
+
8
+ $defs << "-DUSE_RUBY"
9
+ append_cflags("-Wall")
10
+ append_cflags("-Wextra")
11
+ append_cflags("-Wno-unused-parameter")
12
+
13
+ create_makefile(extension_name)
@@ -0,0 +1,96 @@
1
+ // (C) Copyright 2004, David M. Blei (blei [at] cs [dot] cmu [dot] edu)
2
+
3
+ // This file is part of LDA-C.
4
+
5
+ // LDA-C is free software; you can redistribute it and/or modify it under
6
+ // the terms of the GNU General Public License as published by the Free
7
+ // Software Foundation; either version 2 of the License, or (at your
8
+ // option) any later version.
9
+
10
+ // LDA-C is distributed in the hope that it will be useful, but WITHOUT
11
+ // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
+ // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
+ // for more details.
14
+
15
+ // You should have received a copy of the GNU General Public License
16
+ // along with this program; if not, write to the Free Software
17
+ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
+ // USA
19
+
20
+ #include "lda-alpha.h"
21
+
22
+ /*
23
+ * objective function and its derivatives
24
+ *
25
+ */
26
+
27
+ double alhood(double a, double ss, int D, int K)
28
+ { return(D * (lgamma(K * a) - K * lgamma(a)) + (a - 1) * ss); }
29
+
30
+ double d_alhood(double a, double ss, int D, int K)
31
+ { return(D * (K * digamma(K * a) - K * digamma(a)) + ss); }
32
+
33
+ double d2_alhood(double a, int D, int K)
34
+ { return(D * (K * K * trigamma(K * a) - K * trigamma(a))); }
35
+
36
+
37
+ /*
38
+ * newtons method
39
+ *
40
+ */
41
+
42
+ double opt_alpha(double ss, int D, int K)
43
+ {
44
+ double a, log_a, init_a = 100;
45
+ double f, df, d2f;
46
+ int iter = 0;
47
+
48
+ log_a = log(init_a);
49
+ do
50
+ {
51
+ iter++;
52
+ a = exp(log_a);
53
+ if (isnan(a))
54
+ {
55
+ init_a = init_a * 10;
56
+ printf("warning : alpha is nan; new init = %5.5f\n", init_a);
57
+ a = init_a;
58
+ log_a = log(a);
59
+ }
60
+ f = alhood(a, ss, D, K);
61
+ df = d_alhood(a, ss, D, K);
62
+ d2f = d2_alhood(a, D, K);
63
+ log_a = log_a - df/(d2f * a + df);
64
+ printf("alpha maximization : %5.5f %5.5f\n", f, df);
65
+ }
66
+ while ((fabs(df) > NEWTON_THRESH) && (iter < MAX_ALPHA_ITER));
67
+ return(exp(log_a));
68
+ }
69
+
70
+ double quiet_opt_alpha(double ss, int D, int K)
71
+ {
72
+ double a, log_a, init_a = 100;
73
+ double f, df, d2f;
74
+ int iter = 0;
75
+
76
+ log_a = log(init_a);
77
+ do
78
+ {
79
+ iter++;
80
+ a = exp(log_a);
81
+ if (isnan(a))
82
+ {
83
+ init_a = init_a * 10;
84
+ //printf("warning : alpha is nan; new init = %5.5f\n", init_a);
85
+ a = init_a;
86
+ log_a = log(a);
87
+ }
88
+ f = alhood(a, ss, D, K);
89
+ df = d_alhood(a, ss, D, K);
90
+ d2f = d2_alhood(a, D, K);
91
+ log_a = log_a - df/(d2f * a + df);
92
+ //printf("alpha maximization : %5.5f %5.5f\n", f, df);
93
+ }
94
+ while ((fabs(df) > NEWTON_THRESH) && (iter < MAX_ALPHA_ITER));
95
+ return(exp(log_a));
96
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef LDA_ALPHA_H
2
+ #define LDA_ALPHA_H
3
+
4
+ #include <stdlib.h>
5
+ #include <math.h>
6
+ #include <float.h>
7
+
8
+ #include "lda.h"
9
+ #include "utils.h"
10
+
11
+ #define NEWTON_THRESH 1e-5
12
+ #define MAX_ALPHA_ITER 1000
13
+
14
+ double alhood(double a, double ss, int D, int K);
15
+ double d_alhood(double a, double ss, int D, int K);
16
+ double d2_alhood(double a, int D, int K);
17
+ double opt_alpha(double ss, int D, int K);
18
+ double quiet_opt_alpha(double ss, int D, int K);
19
+ //void maximize_alpha(double** gamma, lda_model* model, int num_docs);
20
+
21
+ #endif
@@ -0,0 +1,67 @@
1
+ // (C) Copyright 2004, David M. Blei (blei [at] cs [dot] cmu [dot] edu)
2
+
3
+ // This file is part of LDA-C.
4
+
5
+ // LDA-C is free software; you can redistribute it and/or modify it under
6
+ // the terms of the GNU General Public License as published by the Free
7
+ // Software Foundation; either version 2 of the License, or (at your
8
+ // option) any later version.
9
+
10
+ // LDA-C is distributed in the hope that it will be useful, but WITHOUT
11
+ // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
+ // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
+ // for more details.
14
+
15
+ // You should have received a copy of the GNU General Public License
16
+ // along with this program; if not, write to the Free Software
17
+ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
+ // USA
19
+
20
+ #include "lda-data.h"
21
+
22
+ corpus* read_data(char* data_filename)
23
+ {
24
+ FILE *fileptr;
25
+ int length, count, word, n, nd, nw;
26
+ corpus* c;
27
+
28
+ printf("reading data from %s\n", data_filename);
29
+ c = malloc(sizeof(corpus));
30
+ c->docs = 0;
31
+ c->num_terms = 0;
32
+ c->num_docs = 0;
33
+ fileptr = fopen(data_filename, "r");
34
+ nd = 0; nw = 0;
35
+ while ((fscanf(fileptr, "%10d", &length) != EOF))
36
+ {
37
+ c->docs = (document*) realloc(c->docs, sizeof(document)*(nd+1));
38
+ c->docs[nd].length = length;
39
+ c->docs[nd].total = 0;
40
+ c->docs[nd].words = malloc(sizeof(int)*length);
41
+ c->docs[nd].counts = malloc(sizeof(int)*length);
42
+ for (n = 0; n < length; n++)
43
+ {
44
+ fscanf(fileptr, "%10d:%10d", &word, &count);
45
+ word = word - OFFSET;
46
+ c->docs[nd].words[n] = word;
47
+ c->docs[nd].counts[n] = count;
48
+ c->docs[nd].total += count;
49
+ if (word >= nw) { nw = word + 1; }
50
+ }
51
+ nd++;
52
+ }
53
+ fclose(fileptr);
54
+ c->num_docs = nd;
55
+ c->num_terms = nw;
56
+ printf("number of docs : %d\n", nd);
57
+ printf("number of terms : %d\n", nw);
58
+ return(c);
59
+ }
60
+
61
+ int max_corpus_length(corpus* c)
62
+ {
63
+ int n, max = 0;
64
+ for (n = 0; n < c->num_docs; n++)
65
+ if (c->docs[n].length > max) max = c->docs[n].length;
66
+ return(max);
67
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef LDA_DATA_H
2
+ #define LDA_DATA_H
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+
7
+ #include "lda.h"
8
+
9
+ #define OFFSET 0; // offset for reading data
10
+
11
+ corpus* read_data(char* data_filename);
12
+ int max_corpus_length(corpus* c);
13
+
14
+ #endif