@@ -59,15 +59,16 @@ std::ostream &operator<<(std::ostream &in,
5959 return in << static_cast <int >(context_weight);
6060}
6161
62+ // Arguments specified via command line options. See ParseArgs for documentation.
6263struct Arguments {
6364 unsigned int num_threads = 1 ;
64- unsigned int windowSize = 5 ;
65- bool noSymmetric = false ;
65+ unsigned int window_size = 5 ;
66+ bool no_symmetric = false ;
6667 bool subsample = false ;
6768 ContextWeight context_weight;
6869};
6970
70- auto parseArgs (int argc, char **argv) {
71+ auto ParseArgs (int argc, char **argv) {
7172 // Performance optimizations for writing to stdout
7273 std::ios::sync_with_stdio (false );
7374
@@ -79,9 +80,9 @@ auto parseArgs(int argc, char **argv) {
7980 app.add_option (" -o,--output" , output,
8081 " Output file name. Co-occurence matrix is saved as "
8182 " scipy.sparse compatible CSR matrix in a numpy .npz archive" );
82- app.add_option (" -w,--window-size" , args.windowSize ,
83+ app.add_option (" -w,--window-size" , args.window_size ,
8384 " Window size in which to count co-occurences." );
84- app.add_flag (" --no-symmetric" , args.noSymmetric ,
85+ app.add_flag (" --no-symmetric" , args.no_symmetric ,
8586 " If not specified, a symmetric context window is used." );
8687 app.add_flag (" --subsample" , args.subsample ,
8788 " Apply subsampling during co-occurence matrix construction as "
@@ -178,7 +179,7 @@ class CircularBuffer : public ranges::view_facade<CircularBuffer<T>> {
178179};
179180
180181// * Input
181- auto readVocab () {
182+ auto ReadVocab () {
182183 std::string word;
183184 std::string count;
184185 int rank{0 };
@@ -194,11 +195,11 @@ auto readVocab() {
194195std::mutex paths_m;
195196std::mutex matrices_m;
196197
197- void readMatrix (std::queue<fs::path> &paths, queue<Matrix> &matrices,
198+ void ReadMatrix (std::queue<fs::path> &paths, queue<Matrix> &matrices,
198199 const Vocab &vocab, const Arguments &args, uint32_t seed) {
199200 assert (seed > 0 );
200201 std::string line;
201- CircularBuffer<uint32_t > history (args.windowSize );
202+ CircularBuffer<uint32_t > history (args.window_size );
202203 std::unique_ptr<Matrix> m = std::make_unique<Matrix>();
203204
204205 // Prepare subsampling
@@ -255,7 +256,7 @@ void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
255256 uint64_t key; // We merge 32 bit row and col indices to a single 64
256257 // bit key
257258 // For symmetric contexts, only store one direction.
258- if (!args.noSymmetric ) {
259+ if (!args.no_symmetric ) {
259260 if (word_rank <= context_word_rank) {
260261 key = (uint64_t )word_rank << 32 | context_word_rank;
261262 } else {
@@ -268,7 +269,7 @@ void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
268269 if (args.context_weight == ContextWeight::Harmonic) {
269270 (*m)[key] += 1 .0f / static_cast <count_type>(distance);
270271 } else if (args.context_weight == ContextWeight::DistanceOverSize) {
271- (*m)[key] += (args.windowSize - distance - 1 ) / args.windowSize ;
272+ (*m)[key] += (args.window_size - distance - 1 ) / args.window_size ;
272273 } else {
273274 (*m)[key]++;
274275 }
@@ -286,7 +287,7 @@ void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
286287 }
287288}
288289
289- std::unique_ptr<Matrix> combineMatrices (queue<Matrix> &matrices,
290+ std::unique_ptr<Matrix> CombineMatrices (queue<Matrix> &matrices,
290291 int num_threads) {
291292 std::unique_ptr<Matrix> m1 = matrices.pop ();
292293 for (int i = 1 ; i < num_threads; i++) {
@@ -305,24 +306,24 @@ std::unique_ptr<Matrix> combineMatrices(queue<Matrix> &matrices,
305306 return m1;
306307}
307308
308- auto computeCooccurrenceMatrix (Vocab &vocab, std::queue<fs::path> &paths,
309+ auto ComputeCooccurrenceMatrix (Vocab &vocab, std::queue<fs::path> &paths,
309310 const Arguments &args) {
310311 std::vector<std::thread> threads;
311312 queue<Matrix> matrices;
312313 for (unsigned int i = 0 ; i < args.num_threads ; i++) {
313314 threads.push_back (std::thread ([&paths, &matrices, &vocab, &args, i]() {
314- readMatrix (std::ref (paths), std::ref (matrices), std::ref (vocab),
315+ ReadMatrix (std::ref (paths), std::ref (matrices), std::ref (vocab),
315316 std::ref (args), i + 1 );
316317 }));
317318 }
318- std::unique_ptr<Matrix> m = combineMatrices (matrices, args.num_threads );
319+ std::unique_ptr<Matrix> m = CombineMatrices (matrices, args.num_threads );
319320 for (unsigned int i = 0 ; i < args.num_threads ; i++) {
320321 threads[i].join ();
321322 }
322323 return m;
323324}
324325
325- auto toCOO (const Vocab &vocab, std::unique_ptr<Matrix> m) {
326+ auto ToCOO (const Vocab &vocab, std::unique_ptr<Matrix> m) {
326327 size_t num_tokens = vocab.size ();
327328 size_t nnz = m->size ();
328329 std::cout << " Got " << nnz
@@ -343,7 +344,7 @@ auto toCOO(const Vocab &vocab, std::unique_ptr<Matrix> m) {
343344}
344345
345346// * Output
346- void writeNumpy (const std::string output, const std::vector<uint32_t > &row,
347+ void WriteNumpy (const std::string output, const std::vector<uint32_t > &row,
347348 const std::vector<uint32_t > &col,
348349 const std::vector<count_type> &data, const bool symmetric,
349350 const uint32_t num_tokens) {
@@ -359,10 +360,10 @@ void writeNumpy(const std::string output, const std::vector<uint32_t> &row,
359360
360361// * Main
361362int main (int argc, char **argv) {
362- auto [paths, output, args] = parseArgs (argc, argv);
363- auto vocab = readVocab ();
364- auto cooccurenceMatrix = computeCooccurrenceMatrix (vocab, paths, args);
365- auto [row, col, data] = toCOO (vocab, std::move (cooccurenceMatrix));
366- writeNumpy (output, row, col, data, !args.noSymmetric , vocab.size ());
363+ auto [paths, output, args] = ParseArgs (argc, argv);
364+ auto vocab = ReadVocab ();
365+ auto cooccurenceMatrix = ComputeCooccurrenceMatrix (vocab, paths, args);
366+ auto [row, col, data] = ToCOO (vocab, std::move (cooccurenceMatrix));
367+ WriteNumpy (output, row, col, data, !args.no_symmetric , vocab.size ());
367368 return 0 ;
368369}
0 commit comments