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 #include "logging/logger.h"
16 
17 namespace meta
18 {
19 namespace printing
20 {
21 
26 inline std::string add_commas(const std::string& number)
27 {
28  std::string ret{""};
29  size_t counter = 0;
30  for (auto it = number.rbegin(); it != number.rend(); ++it, ++counter)
31  {
32  if (counter != 0 && counter != number.size() && counter % 3 == 0)
33  ret = ',' + ret;
34  ret = *it + ret;
35  }
36 
37  return ret;
38 }
39 
44 inline std::string make_green(std::string str)
45 {
46  return "\033[32m" + str + "\033[0m";
47 }
48 
53 inline std::string make_red(std::string str)
54 {
55  return "\033[31m" + str + "\033[0m";
56 }
57 
62 inline std::string make_bold(std::string str)
63 {
64  return "\033[1m" + str + "\033[22m";
65 }
66 
72 inline std::string bytes_to_units(double num_bytes)
73 {
74  std::string units = "bytes";
75  for (auto& u : {"KB", "MB", "GB", "TB"})
76  {
77  if (num_bytes >= 1024)
78  {
79  num_bytes /= 1024;
80  units = u;
81  }
82  }
83 
84  num_bytes = static_cast<double>(static_cast<int>(num_bytes * 100)) / 100;
85  return std::to_string(num_bytes) + " " + units;
86 }
87 
94 inline void show_progress(size_t idx, size_t max, size_t freq,
95  const std::string& prefix = "")
96 {
97  if (idx % freq == 0)
98  LOG(progress) << prefix << static_cast<double>(idx) / max * 100
99  << "% \r" << ENDLG;
100 }
101 
106 inline void end_progress(const std::string& prefix)
107 {
108  LOG(progress) << prefix << "100% \n" << ENDLG;
109  LOG(info) << prefix << "100%" << ENDLG;
110 }
111 }
112 }
113 
114 #endif
void show_progress(size_t idx, size_t max, size_t freq, const std::string &prefix="")
Definition: printing.h:94
std::string make_bold(std::string str)
Definition: printing.h:62
std::string add_commas(const std::string &number)
Definition: printing.h:26
std::string bytes_to_units(double num_bytes)
Converts a number of bytes into a human-readable number.
Definition: printing.h:72
void freq(const std::string &file, const cpptoml::table &, uint16_t n)
Performs frequency analysis on a text file.
Definition: profile.cpp:276
std::string make_red(std::string str)
Definition: printing.h:53
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
void end_progress(const std::string &prefix)
Ends output from a call to show_progess by displaying 100% completion.
Definition: printing.h:106
std::string make_green(std::string str)
Definition: printing.h:44
Simple class for reporting progress of lengthy operations.
Definition: progress.h:27