void TextRoom::getFileStatus() { // Calculate deadline QString showdeadline; int daysremaining; QString todaytext = QDate::currentDate().toString("yyyyMMdd"); today = QDate::fromString(todaytext, "yyyyMMdd"); daysremaining = today.daysTo(deadline); QString daysto; daysto.setNum(daysremaining); if (daysremaining <= 0) { showdeadline = ""; } else { showdeadline = daysto + " Days remaining. "; } // Show deadline + clock QDateTime now = QDateTime::currentDateTime(); QString clock = now.toString( timeFormat ); deadlineLabel->setText(showdeadline + clock); //Compute word count QString text, target, selectedText, pageText, characterText; text = textEdit->document()->toPlainText(); if (wordCountChanged) words = getWordCount(text); if (textEdit->textCursor().hasSelection()) { const int wordsSelected = getWordCount(textEdit->textCursor().selection().toPlainText()); if (wordsSelected > 0) selectedText = selectedText.setNum(wordsSelected) + "/"; } // Compute page count if (isPageCount) { pageCount = (int)((words/pageCountFormula)+1); pageText = tr(" Pages: %1").arg(pageCount); } // Compute character count if (isCharacterCount) { if (wordCountChanged) characterCount = textEdit->document()->characterCount() - parasold; characterText = tr(" Characters: %1").arg(characterCount); } if (wordcount > 0) { int percent = (int)(words*100/wordcount); target = tr(" Target: %1 (%2%)").arg(wordcount).arg(percent); } statsLabel->setText(tr("Words: %1%2%3%4%5").arg(selectedText).arg(words).arg(target).arg(characterText).arg(pageText)); wordCountChanged = false; }
int CPinyinTrie::lengthAt(unsigned int idx) const { if (idx < getWordCount() - 1) { return (m_words[idx + 1] - m_words[idx]) - 1; } else if (idx == getWordCount() - 1) { return (((TWCHAR*)(m_mem + m_Size)) - m_words[idx]) - 1; } return 0; }
void SortingCompetition::bubbleSort(){ for(int j = 0; j<getWordCount();j++){ for(int y = 0;y<(getWordCount()-1);y++){ if (compareWords(words2[y],words2[y+1])== 1){ char* temp1 =words2[y+1]; words2[y+1]=words2[y]; words2[y] = temp1; } } } }
void SortingCompetition::bubbleSort(){ //move greatest values to the end of array for(int j = 0; j<getWordCount();j++){ for(int y = 0; y < (getWordCount()-1); y++){ if (compareWords(words2[y],words2[y+1])>= 1){ char* temp1 = words2[y+1]; words2[y+1] = words2[y]; words2[y] = temp1; } } } }
double NaiveBayesianClassifier::getProbability(QString category, const QStringList &text) { long totalWordCount = 0; // TODO: do caching QValueList<QString> categories = m_categories.keys(); for( QValueList<QString>::const_iterator it = categories.constBegin(); it != categories.constEnd(); ++it ) { totalWordCount += getCount(*it); } double catCount = log(getCount(category)); double probability = catCount; probability -= log(totalWordCount); for ( QStringList::ConstIterator iter = text.constBegin(); iter != text.constEnd(); ++iter ) { long wordCount = getWordCount(category, *iter); if( wordCount != 0) { //long wordProbability = wordCount; probability -= log(wordCount); probability += catCount; } else { probability -= log(MIN_CLASS_OCCURRENCE); probability += catCount; } } return probability; }
//function copies values stored in the vector into an array of length-prefixed char* bool SortingCompetition::prepareData(){ bool x = false; //clear contents of words2 to prevent overlap later for(int i = 0; i < getWordCount(); i++){ delete[] words2[i]; } if (getWordCount() > 0) delete[] words2; setWordCount(words.size()); // Loop first finds the length of the word, and then //creates length prefixed strings try{ words2 = new char*[getWordCount()]; } catch(std::bad_alloc&) { cout<<"BAD ALLOC"<<endl; } for(int i = 0; i < getWordCount(); i++){ //determine length of string and store as ASCII character char length = strlen(words.at(i)); //new string is 2 elements longer than original //for the length prefix and /0 char* newString = new char[length+2]; newString[0] = length; strcpy(&(newString[1]),words[i]); //add new length prefixed string to end of words2 words2[i] = newString; } //if no errors occured return true x = true; return x; }
/* * Sorts words based on greatest frequency of word count. */ void sortWords(wordData* firstWord) { int firstCount, nextCount, swapped; wordData* i = firstWord; do { swapped = 0; i = firstWord; while (i->next != NULL) { firstCount = getWordCount(i); nextCount = getWordCount(i->next); if (firstCount < nextCount) { swapWords(i, i->next); swapped = 1; } i = i->next; } } while(swapped); }
unsigned int WriMoImp::getRunningTotal(const Date& date) const { unsigned int runningTotal = 0; for (Date currentDate = getStartDate(); currentDate <= date; currentDate = currentDate.next()) { runningTotal += getWordCount(currentDate); } return runningTotal; }
bool CPinyinTrie::load(const char *fname) { free(); bool suc = false; int fd = open(fname, O_RDONLY); if (fd == -1) return false; m_Size = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); #ifdef HAVE_SYS_MMAN_H suc = (m_mem = (char*)mmap(NULL, m_Size, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED; #else suc = (m_mem = new char [m_Size]) != NULL; suc = suc && (read(fd, m_mem, m_Size) > 0); #endif close(fd); suc = suc && ((m_words = new TWCHAR*[getWordCount()]) != NULL); if (suc) { TWCHAR *p = (TWCHAR*)(m_mem + getStringOffset()); for (int i = 0, sz = getWordCount(); i < sz; ++i) { m_words[i] = p; while (*p++) ; } for (unsigned i = 1; i < 100; ++i) { if (*m_words[i] != WCH_NULL && *m_words[i] != WCH_LESSTHAN) m_SymbolMap[wstring(m_words[i])] = i; } } return suc; }
void SortingCompetition::outputData(const string& outputFileName){ //output sorted data (in words2) fout.open(outputFileName.c_str(), ios::out); if(!fout.good()){ cerr << "file could not be opened" << endl; } for(int i = 0; i < getWordCount(); i++){ fout << &(words2[i][1])<<endl; } fout.close(); }
SortingCompetition::~SortingCompetition(){ //deletes dynamically allocated memory for (int i = 0; i < getWordCount(); i++) { delete[] words2[i]; } delete[] words2; for(size_t i = 0; i < words.size(); i++) { delete[] words.at(i); } words.clear(); }
void text::encodeAndFold (const generationContext& ctx, utility::outputStream& os, const size_t firstLineOffset, size_t* lastLineLength, const int flags) const { size_t curLineLength = firstLineOffset; word::generatorState state; for (size_t wi = 0 ; wi < getWordCount() ; ++wi) { getWordAt(wi)->generate(ctx, os, curLineLength, &curLineLength, flags, &state); } if (lastLineLength) *lastLineLength = curLineLength; }
bool text::operator==(const text& t) const { if (getWordCount() == t.getWordCount()) { bool equal = true; std::vector <shared_ptr <word> >::const_iterator i = m_words.begin(); std::vector <shared_ptr <word> >::const_iterator j = t.m_words.begin(); for ( ; equal && i != m_words.end() ; ++i, ++j) equal = (**i == **j); return (equal); } return (false); }
//making random file filled with specified length by pulling form //the list of words collected from input.txt void SortingCompetition::makeRandomFile(int size,char* name){ //seed random number generator srand(static_cast<unsigned int>(time(0))); //open input file ofstream randomFile(name); int dummy = getWordCount(); //populating input file with random words form base input.txt file for(int i = 0;i<size;i++){ int index = rand()%(words.size()); char* dummyWord = words.at(index); //write random word tou randomFile file randomFile<<words.at(index)<<" "; } //close file randomFile.close(); }
float WriMoImp::getRunningAverageWordCountForDate( const Date& date) const { if (!m_calendar.dateInRange(date)) { throw DateOutOfRangeError(); } unsigned int sum = 0; unsigned int numDays = 0; Date currentDate = getStartDate(); do { sum += getWordCount(currentDate); numDays++; currentDate = currentDate.addDays(1); } while (currentDate <= date); return (float)sum / numDays; }
void text::createFromString(const string& in, const charset& ch) { size_t asciiCount = 0; size_t asciiPercent = 0; removeAllWords(); // Check whether there is a recommended encoding for this charset. // If so, the whole buffer will be encoded. Else, the number of // 7-bit (ASCII) bytes in the input will be used to determine if // we need to encode the whole buffer. encoding recommendedEnc; const bool alwaysEncode = ch.getRecommendedEncoding(recommendedEnc); if (!alwaysEncode) { asciiCount = utility::stringUtils::countASCIIchars(in.begin(), in.end()); asciiPercent = (in.length() == 0 ? 100 : (100 * asciiCount) / in.length()); } // If there are "too much" non-ASCII chars, encode everything if (alwaysEncode || asciiPercent < 60) // less than 60% ASCII chars { appendWord(make_shared <word>(in, ch)); } // Else, only encode words which need it else { bool is8bit = false; // is the current word 8-bit? bool prevIs8bit = false; // is previous word 8-bit? unsigned int count = 0; // total number of words for (size_t end = in.size(), pos = 0, start = 0 ; ; ) { if (pos == end || parserHelpers::isSpace(in[pos])) { const string chunk(in.begin() + start, in.begin() + pos); if (pos != end) ++pos; if (is8bit) { if (count && prevIs8bit) { // No need to create a new encoded word, just append // the current word to the previous one. shared_ptr <word> w = getWordAt(getWordCount() - 1); w->getBuffer() += " " + chunk; } else { if (count) { shared_ptr <word> w = getWordAt(getWordCount() - 1); w->getBuffer() += ' '; } appendWord(make_shared <word>(chunk, ch)); prevIs8bit = true; ++count; } } else { if (count && !prevIs8bit) { shared_ptr <word> w = getWordAt(getWordCount() - 1); w->getBuffer() += " " + chunk; } else { appendWord(make_shared <word> (chunk, charset(charsets::US_ASCII))); prevIs8bit = false; ++count; } } if (pos == end) break; is8bit = false; start = pos; } else if (!parserHelpers::isAscii(in[pos])) { is8bit = true; ++pos; } else { ++pos; } } } }
void SortingCompetition::algorithmTester(void){ int A = 1; int stepSize = 10000; int max = 10001; //number of sorting methods implemented int sortingCount = 1; //setting number of words in RandomInput.txt this->inputSize = A*stepSize; //output header to file for output ofstream output("SortingAnalysis.txt"); output<<"N,"; for(int y = 0;y<sortingCount;y++){ switch(y){ case 0: output<<"Bubble Sort,"; case 1: output<<"Merge Sort,"; } } output<<endl; //close output file temporarily while make the randomInput file output.close(); while(A*stepSize<max){ //function call to make random input file from the words collected from input.txt makeRandomFile(A*stepSize,"RandomInput.txt"); //processing now done on setFileName("RandomInput.txt"); //prepare words2 with RandomInput file instead readRandomData(stepSize); prepareData(); //re-open output file and append to the end output.open("SortingAnalysis.txt",std::ios_base::app); //output input size to output file cout<<A*stepSize<<", "; //find average of all sorting methods for this specific //input size for(int i = 0;i<sortingCount;i++){ double sumRuntime = 0; for(int j = 0;j<30;j++){ //declare 2 time points std::chrono::time_point<std::chrono::system_clock> start, end; //store current time (now()) in start start = std::chrono::system_clock::now(); //decide which sorting method to use switch(i){ case 0: bubbleSort(); case 1: mergeSort(0,getWordCount()-1); } //store time(now()) in end end = std::chrono::system_clock::now(); //get No. of seconds elapsed & output duration //need to do this multiple times & get average std::chrono::duration<double> elapsed_seconds = end-start; sumRuntime += elapsed_seconds.count(); }//end of 30 runtimes //find average runtime double average = sumRuntime/30; //output to file the average run time and size cout<<fixed<<setprecision(9)<<average<<", "; }//processed all sorting algorithms //increment input size A +=1; this->inputSize = A*stepSize; cout<<endl; } }
//sort the data by length, then alphabetized by calling private sort functions void SortingCompetition::sortData(){ multisort(0,getWordCount()-1, 3); }
/* bool SortingCompetition::prepareData() { bool x = false; for(size_t i = 0; i < words2.size(); i++) { delete[] words2.at(i); } words2.clear(); //copy data from original data structure over for(size_t i = 0; i < words.size(); i++) { words2.push_back(new char[strlen(words.at(i))+1]); strcpy(words2.at(i), words.at(i)); } x = true; return x; } */ void SortingCompetition::sortData() { //bubbleSort(); mergeSort(0,getWordCount()-1); }