Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "cTrieNode.h"
- #include <string>
- #include <vector>
- #include <algorithm>
- class cTrie
- {
- private:
- cTrieNode * root;
- int insertedCount;
- int nodesCount;
- int deletedCount;
- int maxNgramLen;
- int findCall;
- int uniqueNgramCount;
- void traversePrint(string & prefix, cTrieNode & node);
- void saveQueryToResult(vector<string> nGrams);
- public:
- cTrie();
- ~cTrie();
- void insert(string nGram);
- bool search(string nGram);
- void remove(string nGram);
- void processNgram(string line);
- void print();
- void printStatistics();
- static vector<string> nGramsResult;
- };
- vector<string> cTrie::nGramsResult;
- cTrie::cTrie()
- {
- this->insertedCount = 0;
- this->nodesCount = 0;
- this->deletedCount = 0;
- this->maxNgramLen = 0;
- this->uniqueNgramCount = 0;
- this->root = new cTrieNode();
- }
- cTrie::~cTrie()
- {
- }
- void cTrie::insert(string nGram)
- {
- int key = 0;
- int nGramLen = 0;
- cTrieNode * actualNode = root;
- for (int i = 0; i < nGram.length(); i++)
- {
- key = nGram[i];
- if (actualNode->child[key] == nullptr)
- {
- cTrieNode * newNode = new cTrieNode();
- actualNode->child[key] = newNode;
- this->nodesCount++;
- }
- actualNode = actualNode->child[key];
- nGramLen++;
- }
- if (actualNode->flag == false)
- {
- actualNode->flag = true;
- this->insertedCount++;
- if (nGramLen > this->maxNgramLen)
- this->maxNgramLen = nGramLen;
- }
- }
- bool cTrie::search(string nGram)
- {
- int key = 0;
- cTrieNode * actualNode = root;
- for (int i = 0; i < nGram.length(); i++)
- {
- key = nGram[i];
- if (key < 0 || key > 255)
- return false;
- if (actualNode->child[key] == nullptr)
- return false;
- actualNode = actualNode->child[key];
- }
- if (actualNode == nullptr)
- return false;
- return actualNode->flag;
- }
- void cTrie::remove(string nGram)
- {
- int key;
- cTrieNode * actualNode = root;
- for (int i = 0; i < nGram.length(); i++)
- {
- key = nGram[i];
- if (actualNode->child[key] == nullptr)
- return;
- actualNode = actualNode->child[key];
- }
- if (actualNode == nullptr)
- return;
- if (actualNode->flag == true)
- {
- actualNode->flag = false;
- //this->deletedCount++;
- this->insertedCount--;
- }
- }
- void cTrie::processNgram(string line)
- {
- vector<string> nGramsVector;
- int spacePosition = 0;
- bool isFirstSpace = false;
- string partOfNgram = line;
- int i = 0;
- while (true)
- {
- for (i = 0; i < partOfNgram.length(); i++)
- {
- if (i > this->maxNgramLen)
- break;
- if (partOfNgram[i] == ' ')
- {
- if (search(partOfNgram.substr(0, i)) == true)
- {
- if (!(std::find(nGramsVector.begin(), nGramsVector.end(), partOfNgram.substr(0, i)) != nGramsVector.end()))
- {
- nGramsVector.push_back(partOfNgram.substr(0, i));
- this->uniqueNgramCount++;
- }
- }
- if (isFirstSpace == false)
- {
- spacePosition = i + 1;
- isFirstSpace = true;
- }
- }
- }
- if (search(partOfNgram.substr(0, i)) == true)
- {
- if (!(std::find(nGramsVector.begin(), nGramsVector.end(), partOfNgram.substr(0, i)) != nGramsVector.end()))
- {
- nGramsVector.push_back(partOfNgram.substr(0, i));
- this->uniqueNgramCount++;
- }
- }
- if (partOfNgram.find(" ") != string::npos)
- {
- partOfNgram = partOfNgram.substr(spacePosition, partOfNgram.length() - spacePosition);
- isFirstSpace = false;
- }
- else
- break;
- }
- this->saveQueryToResult(nGramsVector);
- }
- void cTrie::print()
- {
- printf("\n*****************************\VYPIS NGRAMU Z TRIE\n\n");
- string str = "";
- this->traversePrint(str, *root);
- }
- void cTrie::traversePrint(string & prefix, cTrieNode & node)
- {
- if (node.flag)
- cout << prefix << endl;
- for (int i = 0; i < ALPHABET_SIZE; ++i)
- {
- int next = i;
- cTrieNode * actualBlock = node.child[i];
- if (actualBlock)
- {
- prefix.push_back(next);
- traversePrint(prefix, *actualBlock);
- prefix.pop_back();
- }
- }
- }
- void cTrie::printStatistics()
- {
- printf("\n*****************************\ INFORMACE O TRII\n\n");
- printf("Pocet vlozenych ngramu: %d\n", this->insertedCount);
- printf("Maximalni delka ngramu: %d\n", this->maxNgramLen);
- printf("Pocet vytvorenych uzlu: %d\n", this->nodesCount + 1);
- printf("Pocet nalezenych ngramu: %d\n", this->uniqueNgramCount);
- //printf("Pocet smazanych ngramu: %d\n", this->deletedCount);
- }
- void cTrie::saveQueryToResult(vector<string> nGrams)
- {
- string resultNgram = "";
- int length = nGrams.size();
- for (int i = 0; i < length; i++)
- {
- if(i == length - 1)
- resultNgram += nGrams[i];
- else
- resultNgram += nGrams[i] + "|";
- }
- nGramsResult.push_back(resultNgram);
- //cout << resultNgram << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement