#include "ofApp.h"
bool ofApp::sortOnABC(const LyricWord &a, const LyricWord &b) {
return a.word < b.word;
}
bool ofApp::sortOnLength(const LyricWord &a, const LyricWord &b) {
return (int)a.word.size() > (int)b.word.size();
}
bool ofApp::sortOnOccurrences(const LyricWord &a, const LyricWord &b) {
return a.occurrences > b.occurrences;
}
bool ofApp::removeWordIf(LyricWord &wrd) {
bool bRemove = false;
static string ignoreWords[11] = {"the", "to", "of", "a", "and", "i", "it", "if", "is", "in", "be"};
if(wrd.word.empty()) bRemove = true;
for (int j=0; j<11; j++) {
if(wrd.word == ignoreWords[j]) {
bRemove = true;
break;
}
}
return bRemove;
}
void ofApp::setup() {
ofTrueTypeFont::setGlobalDpi(96);
font.load("sans-serif", 11);
sortTypeInfo = "no sort";
words.clear();
ofBuffer buffer = ofBufferFromFile("freshprince.txt");
string content = buffer.getText();
ofStringReplace(content, "\r", " ");
ofStringReplace(content, "\n", " ");
vector <string> splitString = ofSplitString(content, " ", true, true);
for (unsigned int i=0; i<splitString.size(); i++) {
LyricWord wrd;
wrd.occurrences = 1;
wrd.word = ofToLower( splitString[i] );
words.push_back(wrd);
}
for (unsigned int i=0; i<words.size(); i++) {
char ignoreList[12] = {',', '.', '(', ')', '?', '!', '-', ':', '"', '\'', '\n', '\t'};
for(int j=0; j<12; j++) {
string removeStr;
removeStr += ignoreList[j];
ofStringReplace(words[i].word, removeStr, "");
}
}
for (unsigned int i=0; i<words.size(); i++) {
int c = 1;
for (unsigned int j=0; j<words.size(); j++) {
if(words[i].word == words[j].word) c ++;
}
words[i].occurrences = c;
}
vector<LyricWord>tempWord;
for (unsigned int i=0; i<words.size(); i++) {
bool bAdd = true;
for(unsigned int j=0; j<tempWord.size(); j++) {
if(words[i].word == tempWord[j].word) bAdd = false;
}
if(bAdd) {
tempWord.push_back(words[i]);
}
}
words = tempWord;
ofRemove(words, ofApp::removeWordIf);
}
void ofApp::update() {
}
void ofApp::draw() {
ofSetColor(50);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
float radius = 350;
for(unsigned int i=0; i<words.size()/2; i++) {
float t = -HALF_PI + ofMap(i, 0, (words.size()/2), 0, TWO_PI);
float x = cos( t ) * radius;
float y = sin( t ) * radius;
float a = ofRadToDeg(atan2(y, x));
ofSetColor(0);
ofPushMatrix();
ofTranslate(x, y);
ofRotateZDeg(a);
float scl = 1;
glScalef(scl, scl, scl);
font.drawString(words[i].word, 0, 20);
ofPopMatrix();
}
ofSetColor(100);
font.drawString(sortTypeInfo, -(font.stringWidth(sortTypeInfo)/2), 0);
ofPopMatrix();
ofSetColor(10);
ofDrawBitmapString("\nPress 1 for no sort\nPress 2 for alphabetical\nPress 3 for word length\nPress 4 for word occurrence", 20, 20);
}
void ofApp::keyPressed (int key){
if(key == '1') {
sortTypeInfo = "no sort";
setup();
}
if(key == '2') {
sortTypeInfo = "sorting alphabetically";
ofSort(words, ofApp::sortOnABC);
}
if(key == '3') {
sortTypeInfo = "sorting on word length";
ofSort(words, ofApp::sortOnLength);
}
if(key == '4') {
sortTypeInfo = "sorting on word occurrences";
ofSort(words, ofApp::sortOnOccurrences);
}
}
void ofApp::keyReleased (int key){
}
void ofApp::mouseMoved(int x, int y ){
}
void ofApp::mouseDragged(int x, int y, int button){
}
void ofApp::mousePressed(int x, int y, int button){
}
void ofApp::mouseReleased(int x, int y, int button){
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments