-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathofApp.cpp
126 lines (110 loc) · 4.58 KB
/
ofApp.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "ofApp.h"
#include <regex>
// This example shows what you can do with regular espressions, it does not
// cover the grammar of the regular expression but how they can be used.
// Here some links to learn more about regular expression
// http://www.informit.com/articles/article.aspx?p=2079020
// https://solarianprogrammer.com/2011/10/20/cpp-11-regex-tutorial-part-2/
// http://www.cplusplus.com/reference/regex/
//--------------------------------------------------------------
void ofApp::setup(){
text = "openFrameworks is developed in a collaborative way. We use git, a distributed versioning system, which means also that people can branch, experiment, and make suggestions. If you look at the network diagram on GitHub, it's looks like some alien diagram, full of weaving branches, code pulling apart and coming together. There's a huge community, all over the world, working on the core code: fixing bugs, submitting pull requests, and shaping the tool the way they want to see it. It's a world wide project, and it's common to wake up in the USA to an inbox full of pull requests and issues emails from coders in Asia and Europe. Over 70 people have contributed to the openFrameworks core directly, and hundreds of people have forked the code or contributed in other ways.";
// Regular expression can be used:
// To grep substrings and return them in a string
// [a-z]*ing\\s means all verbs ending with 'ing' followed by a whitespace
wordsWithS = grepStringInRegex(text, "[a-z]*ing\\s");
// To Count occurrences of a string in another string
countedOccurrences = countOccurencesInRegex(text, "[^\\s]+");
//To collect all the items that match certains criteria
// in this case, letter containing 'r'
matchesWithR = matchesInRegex(text, "[a-z]+r[a-z]+");
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(ofColor::red);
auto msg ="Number of words in the text";
ofDrawBitmapString(msg, 20, 10);
ofSetColor(ofColor::black);
ofDrawBitmapString(countedOccurrences, 20, 30);
ofSetColor(ofColor::red);
auto msg1 ="Words containing 'ing'";
ofDrawBitmapString(msg1, 20, 70);
ofSetColor(ofColor::black);
ofDrawBitmapString(wordsWithS, 20, 90);
ofSetColor(ofColor::red);
auto msg2 = "There are "+ofToString(matchesWithR.size())+" Words containing 'r'";
ofDrawBitmapString(msg2, 20, 130);
ofSetColor(ofColor::black);
int y = 150;
for(auto s:matchesWithR){
ofDrawBitmapString(s, 20, y);
y+=20;
}
// Regular expression can also be used parse a text file
file.open(ofToDataPath("HeadShouldersKneesAndToes.lrc"), ofFile::ReadWrite, false);
ofSetColor(ofColor::red);
auto msg3 ="Let's parse a lyric file for a karaoke";
ofDrawBitmapString(msg3, 400, 130);
ofSetColor(ofColor::black);
int posY = 150;
string line;
while(getline(file,line)){
if(line.empty() == false) {
string minutes = grepStringInRegex(line, "[0-9]+:[0-9]+");
string sentence = grepStringInRegex(line, "[^0-9_:\\[\\]]");
ofDrawBitmapString("Time:"+minutes, 400, posY);
ofDrawBitmapString("Sentence:"+sentence, 500, posY);
posY += 20;
}
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int _key){
if(isKeyInRegex(_key, "[0-9]")){
cout << "You have entered a number " << endl;
};
if(isKeyInRegex(_key, "[a-zA-Z]")){
cout << "You have entered a letter" << endl;
};
}
string ofApp::grepStringInRegex(string _str, string _reg){
std::smatch match;
std::regex regEx(_reg, std::regex_constants::icase);
stringstream buffer;
while (regex_search(_str,match,regEx)) {
for (auto x : match){
buffer << x;
}
_str = match.suffix().str();
}
return buffer.str();
}
int ofApp::countOccurencesInRegex(string _str, string _reg){
std::regex regEx(_reg, std::regex_constants::icase);
auto wordsBegin = std::sregex_iterator(_str.begin(), _str.end(), regEx);
auto wordsEnd = std::sregex_iterator();
return distance(wordsBegin, wordsEnd);
};
bool ofApp::isKeyInRegex(int keyPressed, string _reg){
string typedKey(1, keyPressed);
std::regex regEx(_reg, std::regex_constants::icase);
if (regex_match(typedKey, regEx)) {
return true;
} else {
return false;
}
}
vector<string> ofApp::matchesInRegex(string _str, string _reg){
std::regex regEx(_reg, std::regex_constants::icase);
vector<string> results;
auto wordsBegin = std::sregex_iterator(_str.begin(), _str.end(), regEx);
auto wordsEnd = std::sregex_iterator();
for(std::sregex_iterator i = wordsBegin; i != wordsEnd; ++i){
std::smatch m = *i;
results.push_back(m.str());
}
return results;
}