TrieNode & buildTrieFromDict(ifstream & dictFile) { /* * while not end of file * file get line to string * prepend the word with '*', append the word with '\0' * insert word to trie * if faile to insert * output error message break and return * * current node = root * charIndex = string head */ int indexStr = 0; string word; TrieNode & root = createRoot(); while (!dictFile.eof()) { getline(dictFile, word); word.push_back(END_OF_WORD); if (insertWord(root, word) == false) { cout << "memory runs out" << endl; break; } } return root; }
int main(int argc, char **argv) { lnode head=NULL; char word[1024]; while(!feof(stdin)) { if (scanf("%s",word)) { // If we can read another white space terminated string if (normalize(word)) { // And if that string is non-empty after normalization... // printf ("Read word %s\n",word); head=insertWord(word,head); } } } lnode node; int i=1; for(node=head; node; node=getNextLnode(node)) { printf("%3d. - %3d - %s\n",i++,getLnodeCount(node),getLnodeValue(node)); } lnode next; for(node=head; node; node=next) { next=getNextLnode(node); deleteLnode(node); } return 0; }
void ChatLogWin::showChatLog(QVector<ShareQQMsgPtr> &chat_logs) { ui_->msgbrowse->clear(); ShareQQMsgPtr msg; foreach (msg, chat_logs) { QQGroupChatMsg *chat_msg = static_cast<QQGroupChatMsg*>(msg.data()); qint64 time = chat_msg->time(); QDateTime date_time; date_time.setMSecsSinceEpoch(time * 1000); QString time_str = date_time.toString("dd ap hh:mm:ss"); insertNameLine(convertor_->convert(chat_msg->sendUin()) + " " + time_str, Qt::blue); for (int i = 0; i < chat_msg->msg_.size(); ++i) { if (chat_msg->msg_[i].type() == QQChatItem::kQQFace) { insertQQFace(chat_msg->msg_[i].content()); } else if (chat_msg->msg_[i].type() == QQChatItem::kWord) insertWord(chat_msg->msg_[i].content(), QFont(), Qt::black, 9); else { insertImg(chat_msg->msg_[i].content()); } } }
/* * Reads file of specified fileName and stores word frequency into * wordData linked list. int story indicates which story count to increment. */ void readFile(wordData **firstWord, char* fileName, int story) { FILE *infile = fopen(fileName, "r"); char tempWord[WORD_LENGTH]; while(fscanf(infile, "%s", tempWord) == 1) { if (searchWord(*firstWord, cleanWord(tempWord), story) == 0) insertWord(firstWord, tempWord, story); } }
void FrequencyTrie::constructTree(string dictionaryFilename) { ifstream fileStream; fileStream.open(dictionaryFilename.c_str()); string word; while (getline(fileStream, word)) { insertWord(word); } fileStream.close(); }
int loadArray(char *inFileName, char ***array, int *count, int *capacity) { FILE *inFile; /* file stream */ char word[WORD_LENGTH]; /* this is the ONLY auto array we'll need */ if ((inFile = fopen(inFileName, "r")) == NULL) /* opens file "inFileName" for reading */ { fprintf(stderr,"Error opening input file, %s\n", inFileName); return -1; } *array = (char **)malloc((*capacity) * sizeof(char*)); /* we only dereference once in order to pass-by-reference. ***array is a reference to an array of strings. The extra * is to denote that we are passing-by-reference. */ if (*array == NULL) { fprintf(stderr, "Malloc of array in loadArray failed!\n"); return -1; } printf("\nReading file '%s' (each . is 1000 words read)\n", inFileName); *count = 0; /*dereference the reference to count*/ while (fscanf(inFile, "%s", word) == 1) { if ((*count+1) >= *capacity) { doubleArraySize(array, capacity); } if (insertWord(array, count, word, capacity) != 0) { fprintf(stderr," Insert returned an error!\n"); fclose(inFile); return 1; } if (*count % 1000 == 0) { printf("."); fflush(stdout); /* stdout is buffered, so have to force flush */ } } fclose(inFile); return 0; }
AVLNode* AVLIndex::insertWord(char *&nextWord, int ID) { if (head == NULL) { return (head = new AVLNode(nextWord, ID)); } else { return insertWord(head, nextWord, ID); } }
void wordCount1(article *art) { char word[WORD_MAX_LENGTH]; FILE *fp_read; fp_read = fopen("temp.txt", "rb"); while (!feof(fp_read)){ fscanf(fp_read, "%s", word); if (insertWord(word, art)){ art->uni_word++; } art->word_count++; } }
void insertWord(const std::string & s) { if (s.empty()) { return; } auto currentNode = root; for (auto sit = s.begin(); sit != s.end(); ++sit) { currentNode = getNodeThatMatches(*sit, currentNode->children); } auto sit = s.begin(); while (std::next(sit) != s.end()) { auto suffix = std::string(std::next(sit), s.end()); insertWord(suffix); sit = std::next(sit); } }
int main() { //printf("Hello world!\n"); int ch; FILE *fp; char word[100]; Node *root = CreateNode(); while(1) { ch = showMenu(); switch(ch) { case 1: //printf("Enter Word:\n"); //scanf("%s",word); fp = fopen("dictionary.txt","r"); if(fp!=NULL) { while(!feof(fp)){ fscanf(fp,"%s",word); insertWord(root,word); } } else { printf("\nWrong file path\n"); return -1; } fclose(fp); break; case 2: scanf("%s",word); AutoSuggest(root,word); break; case 3: PrintAllWords(root,""); break; case 4: return 0; default: printf("\nBad choice...\n"); } } return 0; }
int loadArray(char *inFileName, char ***array, int *count, int *capacity) { FILE *inFile; char word[WORD_LENGTH]; /* this is the ONLY auto array we'll need */ if ((inFile = fopen(inFileName, "r")) == NULL) { fprintf(stderr,"Error opening input file, %s\n", inFileName); return -1; } *array = (char **)malloc(*capacity * sizeof(char*)); if (*array == NULL) { fprintf(stderr, "Malloc of array in loadArray failed!\n"); return -1; } printf("Reading file %s (each . is 1000 words read)\n", inFileName); *count = 0; while (fscanf(inFile, "%s", word) == 1) { if (*count >= *capacity) { /* call a function that will double the size of the array and copy its contents */ } if (insertWord(*array, count, word) != 0) { fprintf(stderr," Insert returned an error!\n"); fclose(inFile); return 1; } if (*count % 1000 == 0) { printf("."); fflush(stdout); /* stdout is buffered, so have to force flush */ } } fclose(inFile); return 0; }
void wordCount(article *art) { char word[WORD_MAX_LENGTH]; int i, pos; char *temp; for (i = 0; i < art->length; ++i){ pos = 0; temp = art->all_lines[i]; while(*temp == 32){ ++temp; } while (sscanf(temp + pos, "%s", word) == 1){ if (insertWord(word, art)){ art->uni_word++; } art->word_count++; pos += strlen(word) + 1; while(*(temp + pos) == 32){ ++pos; } } } }
/** * @param board: A list of lists of character * @param words: A list of string * @return: A list of string */ vector<string> wordSearchII(vector<vector<char> > &board, vector<string> &words) { v.clear(); us.clear(); ans.clear(); vector<vector<char> > &b = board; n = b.size(); m = n ? b[0].size() : 0; if (!(n || m)) { return ans; } TrieNode *root = new TrieNode(); for (auto it = words.begin(); it != words.end(); ++it) { insertWord(root, *it); } string s = ""; v.resize(n, vector<bool>(m, false)); int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) { v[i][j] = true; s.push_back(b[i][j]); DFS(i, j, s, root->child[b[i][j] - 'a'], b); s.pop_back(); v[i][j] = false; } } for (auto it = us.begin(); it != us.end(); ++it) { ans.push_back(*it); } clearTrie(root); return ans; }
SuffixTree(const std::string & s) : root(new TrieNode(-1)) { insertWord(s); }
AVLNode* AVLIndex::insertWord(AVLNode* &cur, char *&nextWord, int ID) { if (cur == NULL) { cur = new AVLNode(nextWord, ID); return cur; } int result = strcmp(nextWord, cur->word); if (result == 0) { cur->addID(cur, ID); } // if(nextWord == cur -> word) // cur -> addID(cur, ID); else if (result < 0) { //else if(nextWord < cur -> word){ cur->left = insertWord(cur->left, nextWord, ID); if (height(cur->left) - height(cur->right) == 2) { int result2 = strcmp(nextWord, cur->left->word); if (result2 < 0) { //if(nextWord < cur -> left -> word){ cur = rotateLeftOnce(cur); } else { cur = rotateLeftTwice(cur); } } } else if (result > 0) { //else if(nextWord > cur -> word){ cur->right = insertWord(cur->right, nextWord, ID); if (height(cur->right) - height(cur->left) == 2) { int result3 = strcmp(nextWord, cur->right->word); if (result3 > 0) { //if(nextWord > cur -> right -> word){ cur = rotateRightOnce(cur); } else { cur = rotateRightTwice(cur); } } } cur->height = max(height(cur->left), height(cur->right)) + 1; return cur; }
void TextFlow::calculateMesh() { // Save currently set buffer GLint oldBuffer = -1; glGetIntegerv(GL_ARRAY_BUFFER, &oldBuffer); // Get size of space character float pixelOfSpace = 0; Glyph const * pGlyph = mpFont->getGlyph(mFontSize, u' '); if (pGlyph == NULL) { throwWarning( OperationNotifier::Operation::RUNTIME, "TextFlow does not find space sign in font"); } else { pixelOfSpace = mScale * pGlyph->advance.x; } // Create mark for overflow Word overflowMark = calculateWord(TEXT_FLOW_OVERFLOW_MARK, mScale); // Get height of line float lineHeight = mScale * mpFont->getLineHeight(mFontSize); // Go over words in content std::u16string copyContent = mContent; size_t pos = 0; std::u16string token; // Get pararaphs separated by \n std::vector<std::u16string> paragraphs; std::u16string paragraphDelimiter = u"\n"; while ((pos = copyContent.find(paragraphDelimiter)) != std::u16string::npos) { token = copyContent.substr(0, pos); paragraphs.push_back(token); copyContent.erase(0, pos + paragraphDelimiter.length()); } paragraphs.push_back(copyContent); // Last paragraph (paragraphs never empty) // Build flow together from words in paragraphs std::vector<glm::vec3> vertices; std::vector<glm::vec2> textureCoordinates; // Do not generate text flow mesh when there is a failure bool failure = false; // Go over paragraphs (pens are in local pixel coordinate system with origin in lower left corner of element) float yPixelPen = -lineHeight; // First line should be also inside flow for (std::u16string& rPargraph : paragraphs) { // Get words out of paragraph std::vector<Word> words; std::u16string wordDelimiter = u" "; while ((pos = rPargraph.find(wordDelimiter)) != std::u16string::npos) { token = rPargraph.substr(0, pos); rPargraph.erase(0, pos + wordDelimiter.length()); failure |= !insertWord(words, token, mWidth, mScale); } // Add last token from paragraph as well failure |= !insertWord(words, rPargraph, mWidth, mScale); // Failure appeared, forget it if (!failure) { // Prepare some values uint wordIndex = 0; bool hasNext = !words.empty(); // Go over lines to write paragraph while (hasNext && abs(yPixelPen) <= mHeight) { // Collect words in one line std::vector<Word const *> line; float wordsPixelWidth = 0; float newWordsWithSpacesPixelWidth = 0; // Still words in the paragraph and enough space? Fill into line! while (hasNext && newWordsWithSpacesPixelWidth <= mWidth) { // First word should always fit into width because of previous checks wordsPixelWidth += words[wordIndex].pixelWidth; line.push_back(&words[wordIndex]); wordIndex++; if (wordIndex >= words.size()) { // No words in paragraph left hasNext = false; } else { // Calculate next width of line newWordsWithSpacesPixelWidth = std::ceil( (wordsPixelWidth + (float)words[wordIndex].pixelWidth) // Words size (old ones and new one) + (((float)line.size()) - 1.0f) * pixelOfSpace); // Spaces between words } } // If this is last line and after it still words left, replace it by some mark for overflow if (hasNext && abs(yPixelPen - lineHeight) > mHeight && overflowMark.pixelWidth <= mWidth) { line.clear(); wordsPixelWidth = overflowMark.pixelWidth; line.push_back(&overflowMark); } // Decide dynamic space for line float dynamicSpace = pixelOfSpace; if (line.size() > 1) { if (mAlignment == TextFlowAlignment::JUSTIFY && hasNext && line.size() > 1) // Do not use dynamic space for last line { // For justify, do something dynamic dynamicSpace = ((float)mWidth - wordsPixelWidth) / ((float)line.size() - 1.0f); } else { // Adjust space to compensate precision errors in other alignments float calculatedDynamicSpace = (float)mWidth - (wordsPixelWidth / (float)(line.size() - 1)); dynamicSpace = std::min(dynamicSpace, calculatedDynamicSpace); } } // Now decide xOffset for line float xOffset = 0; if (mAlignment == TextFlowAlignment::RIGHT || mAlignment == TextFlowAlignment::CENTER) { xOffset = (float)mWidth - ((wordsPixelWidth + ((float)line.size() - 1.0f) * dynamicSpace)); if (mAlignment == TextFlowAlignment::CENTER) { xOffset = xOffset / 2.0f; } } // Combine word geometry to one line float xPixelPen = xOffset; for (uint i = 0; i < line.size(); i++) { // Assuming, that the count of vertices and texture coordinates is equal for (uint j = 0; j < line[i]->spVertices->size(); j++) { const glm::vec3& rVertex = line[i]->spVertices->at(j); vertices.push_back(glm::vec3(rVertex.x + xPixelPen, rVertex.y + yPixelPen, rVertex.z)); const glm::vec2& rTextureCoordinate = line[i]->spTextureCoordinates->at(j); textureCoordinates.push_back(glm::vec2(rTextureCoordinate.s, rTextureCoordinate.t)); } // Advance xPen xPixelPen += dynamicSpace + line[i]->pixelWidth; } // Advance yPen yPixelPen -= lineHeight; } } } // If failure appeared, clean up if (failure) { // Vertex count will become zero vertices.clear(); textureCoordinates.clear(); } // Get height of all lines (yPixelPen is one line to low now) mFlowHeight = (int)std::max(std::ceil(abs(yPixelPen) - lineHeight), 0.0f); // Vertex count mVertexCount = (GLuint)vertices.size(); // Fill into buffer glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); glBufferData(GL_ARRAY_BUFFER, mVertexCount * 3 * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, mTextureCoordinateBuffer); glBufferData(GL_ARRAY_BUFFER, mVertexCount * 2 * sizeof(float), textureCoordinates.data(), GL_DYNAMIC_DRAW); // Restore old setting glBindBuffer(GL_ARRAY_BUFFER, oldBuffer); }
void menu(char ***array, int *wordCount, int *capacity) { int exit; char option; char word[WORD_LENGTH]; exit = 0; do { printf("\nMenu\n"); printf("'S'earch, 'I'nsert, 'R'emove, 'C'ount, 'P'rint, 'Q'uit\n"); scanf("%c", &option); /* takes in char + newline character see "newline handler" below */ if (option == 's' || option == 'S') { /* call search() */ printf("\nSearch\n"); searchWord(array, wordCount); } else if (option == 'i' || option == 'I') { /* call insert() */ printf("\nInsert\n"); printf("\nEnter a word: "); scanf("%s", word); insertWord(array, wordCount, word, capacity); printf("\nWord inserted.\n"); } else if (option == 'r' || option == 'R') { /* call remove() */ printf("\nRemove\n"); removeWord(array, wordCount); } else if (option == 'c' || option == 'C') { /* call count() */ printf("\nCount\n"); printCount(*wordCount); } else if (option == 'p' || option == 'P') { printf("\nPrint\n"); printArray(*array, *wordCount); } else if (option == 'q' || option == 'Q') { exit = 1; } else { /* invalid entry */ printf("\nInvalid Entry, returning to main menu\n"); } /* newline handler */ while (option != '\n') { scanf("%c", &option); } } while (exit == 0); }
void newWordManageWindow::add() { QString spell = addDialog->spellEdit->text(); QString meaning = addDialog->meaningEdit->text(); insertWord(spell, meaning); }
void Zimindexer::onP(const std::string& word, unsigned pos) { if (trivialWords.find(word) == trivialWords.end()) insertWord(word, inTitle ? 0 : 3, pos); }
static int getWord( FILE *fp, wordlist **list, word *w, unsigned *os ) { static int ret = 0; char *tmp = NULL; int ch = 0; int pv = 0; int ppv = 0; ret &= ~OUT_OF_MEM; if( w->len == 0 || w->len >= w_size ) { ret = 0; while( 1 ) { if( w->len >= w_size ) { w_size += MIN_WORD_LEN; tmp = (char *) realloc( w_buff, w_size ); if( tmp == NULL ) { w_size -= MIN_WORD_LEN; return( OUT_OF_MEM ); } w_buff = tmp; } ppv = pv; pv = ch; ch = fgetc( fp ); if( ch == '\n' || ch == '\t' || ch == ' ' || ch == EOF ) { break; } else { *os += 1; w_buff[ w->len ] = ch; w->len++; } } while( 1 ) { if( ch == ' ' ) { w->spc++; *os += 1; } else if( ch == '\t' ) { w->spc += 8 - *os % 8; *os += 8 - *os % 8; } else { break; } ch = fgetc( fp ); } if( w->len > 0 ) { w_buff[ w->len ] = '\0'; } if( ch == EOF ) { *os = 0; w->spc = 1; ret |= END_FILE; } else if( ch == '\n' ) { *os = 0; w->spc = 1; ret |= END_LINE; } else { ungetc( ch, fp ); } if( w->spc == 0 ) { w->spc = 1; } else if( f_mode & FMT_NOSPACE ) { if( pv == '.' || pv == '?' || pv == '!' ) { if( isupper( ppv ) ) { w->spc = 1; } else { w->spc = 2; } } else { w->spc = 1; } } } w->text = insertWord( list, w_buff, w->len ); if( w->text == NULL ) { ret |= OUT_OF_MEM; } return( ret ); }