-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmark.cpp
107 lines (94 loc) · 3.46 KB
/
mark.cpp
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
#include "../include/color.hpp"
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <set>
#include <sstream>
#include <string>
using namespace std;
const set<string> TO_WATCH = {"google", "facebook", "microsoft", "twitter"};
void segment(const string & s, decltype(s.begin()) & a, decltype(s.end()) & b)
{
auto is_punct = [](char c) -> bool
{
auto punct = set<char>{',', '.', ':', ';', '?', '!',
'\'', '\"', '(', ')'};
return punct.find(c) != punct.end();
};
a = find_if_not(s.cbegin(), s.cend(), is_punct);
b = find_if_not(s.crbegin(), s.crend(), is_punct).base();
// if entire s is punct, this punct is deemed a post punct
if (a == s.cend())
a = s.cbegin();
}
void separate(const string & s, string & pre, string & word, string & post)
{
auto a = s.cbegin(), b = s.cend();
segment(s, a, b);
pre = string(s.cbegin(), a);
word = string(a, b);
post = string(b, s.cend());
}
bool is_keyword(string s, const set<string> & names = TO_WATCH)
{
transform(s.begin(), s.end(), s.begin(), ::tolower);
return names.find(s) != names.end();
}
bool is_number(string s)
{
if (s.empty())
return false;
auto a = s.cbegin(), b = s.cend();
if (s.front() == '$')
a = s.cbegin() + 1;
if (s.back() == '%')
b = s.cend() - 1;
s = string(a, b);
return !s.empty() && s.find_first_not_of(",.1234567890") == string::npos;
}
#if __cplusplus < 201402L
auto mark(const string & str, string color) -> decltype(dye::vanilla(""))
#else
auto mark(const string & str, string color)
#endif
{
istringstream iss(str);
auto marked = dye::vanilla("");
for (string line; getline(iss, line); marked += "\n") {
istringstream lineiss(line);
for (string text; lineiss >> text; marked += " ") {
string pre, word, post;
// split a text into 3 parts: word in middle, and punctuations around it
separate(text, pre, word, post);
marked += pre;
if (is_keyword(word))
marked += dye::colorize(word, color).invert();
else if (is_number(word))
marked += dye::colorize(word, color);
else
marked += word;
marked += post;
}
}
return marked;
}
int main()
{
auto tech_news = "Silicon Valley giants including Google, Facebook and "
"Twitter are headed to Capitol Hill. Here's what you "
"can expect from their hearings.";
cout << mark(tech_news, "light_red") << endl;
auto stock_news = "Shares in Twitter, Snap and Facebook all declined "
"significantly, dragging the Nasdaq down more than 1% "
"to below the 8,000 level.\n\nTwitter fell 6% to end the "
"session at $32.17. \n\nFacebook saw its shares fall more "
"than 2% to finish at $167.18.\n\nGoogle parent, "
"Alphabet, came in for a drubbing during the hearing "
"because it declined to accept the committes's "
"invitation to testify. The company's stock slipped "
"nearly 1% to close at 1186.48.\n\nMicrosoft fell almost "
"3% on the day, to $108.49.";
cout << mark(stock_news, "yellow") << endl;
return 0;
}