ModErn Text Analysis
META Enumerates Textual Applications
printing.h
Go to the documentation of this file.
1 
10 #ifndef META_PRINTING_H_
11 #define META_PRINTING_H_
12 
13 #include <iostream>
14 #include <string>
15 
16 #include "meta/config.h"
17 #include "meta/logging/logger.h"
18 
19 namespace meta
20 {
21 namespace printing
22 {
23 
28 inline std::string add_commas(const std::string& number)
29 {
30  std::string ret{""};
31  size_t counter = 0;
32  for (auto it = number.rbegin(); it != number.rend(); ++it, ++counter)
33  {
34  if (counter != 0 && counter != number.size() && counter % 3 == 0)
35  ret = ',' + ret;
36  ret = *it + ret;
37  }
38 
39  return ret;
40 }
41 
46 inline std::string make_green(std::string str)
47 {
48  return "\033[32m" + str + "\033[0m";
49 }
50 
55 inline std::string make_red(std::string str)
56 {
57  return "\033[31m" + str + "\033[0m";
58 }
59 
64 inline std::string make_bold(std::string str)
65 {
66  return "\033[1m" + str + "\033[22m";
67 }
68 
74 inline std::string bytes_to_units(double num_bytes)
75 {
76  std::string units = "bytes";
77  for (auto& u : {"KB", "MB", "GB", "TB"})
78  {
79  if (num_bytes >= 1024)
80  {
81  num_bytes /= 1024;
82  units = u;
83  }
84  }
85 
86  num_bytes = static_cast<double>(static_cast<int>(num_bytes * 100)) / 100;
87  return std::to_string(num_bytes) + " " + units;
88 }
89 
96 inline void show_progress(size_t idx, size_t max, size_t freq,
97  const std::string& prefix = "")
98 {
99  if (idx % freq == 0)
100  LOG(progress) << prefix << static_cast<double>(idx) / max * 100
101  << "% \r" << ENDLG;
102 }
103 
108 inline void end_progress(const std::string& prefix)
109 {
110  LOG(progress) << prefix << "100% \n" << ENDLG;
111  LOG(info) << prefix << "100%" << ENDLG;
112 }
113 }
114 }
115 
116 #endif
void show_progress(size_t idx, size_t max, size_t freq, const std::string &prefix="")
Definition: printing.h:96
std::string make_bold(std::string str)
Definition: printing.h:64
std::string add_commas(const std::string &number)
Definition: printing.h:28
std::string bytes_to_units(double num_bytes)
Converts a number of bytes into a human-readable number.
Definition: printing.h:74
std::string make_red(std::string str)
Definition: printing.h:55
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
void end_progress(const std::string &prefix)
Ends output from a call to show_progess by displaying 100% completion.
Definition: printing.h:108
std::string make_green(std::string str)
Definition: printing.h:46
Simple class for reporting progress of lengthy operations.
Definition: progress.h:34
void freq(const std::string &file, cpptoml::table &config, int64_t n)
Performs frequency analysis on a text file.
Definition: profile.cpp:278