Advertisement
Guest User

TrieCpp

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "cTrieNode.h"
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. class cTrie
  9. {
  10. private:
  11.     cTrieNode * root;
  12.     int insertedCount;
  13.     int nodesCount;
  14.     int deletedCount;
  15.     int maxNgramLen;
  16.     int findCall;
  17.     int uniqueNgramCount;
  18.  
  19.     void traversePrint(string & prefix, cTrieNode & node);
  20.     void saveQueryToResult(vector<string> nGrams);
  21.  
  22. public:
  23.  
  24.     cTrie();
  25.     ~cTrie();
  26.     void insert(string nGram);
  27.     bool search(string nGram);
  28.     void remove(string nGram);
  29.     void processNgram(string line);
  30.     void print();
  31.     void printStatistics();
  32.  
  33.     static vector<string> nGramsResult;
  34. };
  35.  
  36. vector<string> cTrie::nGramsResult;
  37.  
  38. cTrie::cTrie()
  39. {
  40.     this->insertedCount = 0;
  41.     this->nodesCount = 0;
  42.     this->deletedCount = 0;
  43.     this->maxNgramLen = 0;
  44.     this->uniqueNgramCount = 0;
  45.     this->root = new cTrieNode();
  46. }
  47.  
  48. cTrie::~cTrie()
  49. {
  50.  
  51. }
  52.  
  53. void cTrie::insert(string nGram)
  54. {
  55.     int key = 0;
  56.     int nGramLen = 0;
  57.  
  58.     cTrieNode * actualNode = root;
  59.  
  60.     for (int i = 0; i < nGram.length(); i++)
  61.     {
  62.         key = nGram[i];
  63.         if (actualNode->child[key] == nullptr)
  64.         {
  65.             cTrieNode * newNode = new cTrieNode();
  66.             actualNode->child[key] = newNode;
  67.             this->nodesCount++;
  68.         }
  69.  
  70.         actualNode = actualNode->child[key];
  71.         nGramLen++;
  72.     }
  73.  
  74.     if (actualNode->flag == false)
  75.     {
  76.         actualNode->flag = true;
  77.         this->insertedCount++;
  78.  
  79.         if (nGramLen > this->maxNgramLen)
  80.             this->maxNgramLen = nGramLen;
  81.     }
  82. }
  83.  
  84. bool cTrie::search(string nGram)
  85. {
  86.  
  87.     int key = 0;
  88.  
  89.     cTrieNode * actualNode = root;
  90.  
  91.     for (int i = 0; i < nGram.length(); i++)
  92.     {
  93.         key = nGram[i];
  94.  
  95.         if (key < 0 || key > 255)
  96.             return false;
  97.  
  98.         if (actualNode->child[key] == nullptr)
  99.             return false;
  100.  
  101.         actualNode = actualNode->child[key];
  102.     }
  103.  
  104.     if (actualNode == nullptr)
  105.         return false;
  106.  
  107.     return actualNode->flag;
  108. }
  109.  
  110. void cTrie::remove(string nGram)
  111. {
  112.     int key;
  113.  
  114.     cTrieNode * actualNode = root;
  115.  
  116.     for (int i = 0; i < nGram.length(); i++)
  117.     {
  118.         key = nGram[i];
  119.  
  120.         if (actualNode->child[key] == nullptr)
  121.             return;
  122.  
  123.         actualNode = actualNode->child[key];
  124.     }
  125.  
  126.     if (actualNode == nullptr)
  127.         return;
  128.  
  129.     if (actualNode->flag == true)
  130.     {
  131.         actualNode->flag = false;
  132.         //this->deletedCount++;
  133.         this->insertedCount--;
  134.     }
  135. }
  136.  
  137. void cTrie::processNgram(string line)
  138. {
  139.     vector<string> nGramsVector;
  140.     int spacePosition = 0;
  141.     bool isFirstSpace = false;
  142.     string partOfNgram = line;
  143.     int i = 0;
  144.  
  145.     while (true)
  146.     {
  147.         for (i = 0; i < partOfNgram.length(); i++)
  148.         {
  149.             if (i > this->maxNgramLen)
  150.                 break;
  151.  
  152.             if (partOfNgram[i] == ' ')
  153.             {
  154.                 if (search(partOfNgram.substr(0, i)) == true)
  155.                 {
  156.                     if (!(std::find(nGramsVector.begin(), nGramsVector.end(), partOfNgram.substr(0, i)) != nGramsVector.end()))
  157.                     {
  158.                         nGramsVector.push_back(partOfNgram.substr(0, i));
  159.                         this->uniqueNgramCount++;
  160.                     }
  161.                 }
  162.  
  163.                 if (isFirstSpace == false)
  164.                 {
  165.                     spacePosition = i + 1;
  166.                     isFirstSpace = true;
  167.                 }
  168.             }
  169.         }
  170.  
  171.         if (search(partOfNgram.substr(0, i)) == true)
  172.         {
  173.             if (!(std::find(nGramsVector.begin(), nGramsVector.end(), partOfNgram.substr(0, i)) != nGramsVector.end()))
  174.             {
  175.                 nGramsVector.push_back(partOfNgram.substr(0, i));
  176.                 this->uniqueNgramCount++;
  177.             }
  178.         }
  179.  
  180.         if (partOfNgram.find(" ") != string::npos)
  181.         {
  182.             partOfNgram = partOfNgram.substr(spacePosition, partOfNgram.length() - spacePosition);
  183.             isFirstSpace = false;
  184.         }
  185.         else
  186.             break;
  187.     }
  188.  
  189.     this->saveQueryToResult(nGramsVector);
  190. }
  191.  
  192. void cTrie::print()
  193. {
  194.     printf("\n*****************************\VYPIS NGRAMU Z TRIE\n\n");
  195.     string str = "";
  196.     this->traversePrint(str, *root);
  197. }
  198.  
  199. void cTrie::traversePrint(string & prefix, cTrieNode & node)
  200. {
  201.     if (node.flag)
  202.         cout << prefix << endl;
  203.  
  204.     for (int i = 0; i < ALPHABET_SIZE; ++i)
  205.     {
  206.         int next = i;
  207.         cTrieNode * actualBlock = node.child[i];
  208.  
  209.         if (actualBlock)
  210.         {
  211.             prefix.push_back(next);
  212.             traversePrint(prefix, *actualBlock);
  213.             prefix.pop_back();
  214.         }
  215.     }
  216. }
  217.  
  218. void cTrie::printStatistics()
  219. {
  220.     printf("\n*****************************\ INFORMACE O TRII\n\n");
  221.     printf("Pocet vlozenych ngramu: %d\n", this->insertedCount);
  222.     printf("Maximalni delka ngramu: %d\n", this->maxNgramLen);
  223.     printf("Pocet vytvorenych uzlu: %d\n", this->nodesCount + 1);
  224.     printf("Pocet nalezenych ngramu: %d\n", this->uniqueNgramCount);
  225.     //printf("Pocet smazanych ngramu: %d\n", this->deletedCount);
  226. }
  227.  
  228. void cTrie::saveQueryToResult(vector<string> nGrams)
  229. {
  230.     string resultNgram = "";
  231.     int length = nGrams.size();
  232.  
  233.     for (int i = 0; i < length; i++)
  234.     {
  235.         if(i == length - 1)
  236.             resultNgram += nGrams[i];
  237.         else
  238.             resultNgram += nGrams[i] + "|";
  239.     }
  240.  
  241.     nGramsResult.push_back(resultNgram);
  242.     //cout << resultNgram << endl;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement