Bittle

ChainingTable (Hash Table)

Feb 15th, 2017
98
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include "chainingTable.h"
  4. #include "stringUtil.h"
  5. // main.cpp
  6. void writeToFile(string fileName, string str) {
  7.     ofstream file(fileName);
  8.     if (file.is_open()) {
  9.         file << str;
  10.         file.close();
  11.     }
  12. }
  13.  
  14. void correctRaven(chainingTable table) {
  15.     stringUtil stringUtil;
  16.     ifstream file("raven.txt");
  17.     string str;
  18.     string total = "";
  19.  
  20.     while (file >> str) {
  21.         // read file word by word
  22.         string word = stringUtil.scrubText(str);
  23.  
  24.         if (!table.contains(word)) {
  25.             //cout << "This isn\'t English "<< word<< endl;
  26.             total += word + "\n";
  27.         }
  28.     }
  29.     writeToFile("output.txt", total);
  30. }
  31.  
  32. int main() {
  33.  
  34.     chainingTable table;
  35.  
  36.     ifstream file("dictionary.txt");
  37.     string str;
  38.  
  39.     while (std::getline(file, str)) {
  40.         table.insert(str);
  41.     }
  42.  
  43.     cout << table.averageSearchCost() << endl;
  44.  
  45.     correctRaven(table);
  46.     return 0;
  47. }
  48.  
  49. // ========= chainingTable.h ==========
  50. #include "linkedList.h"
  51.  
  52. class chainingTable {
  53. private:
  54.     //hash table consists of a table of lists
  55.     linkedList *table;
  56.  
  57.     //size of table
  58.     int capacity;
  59.  
  60.     //number of items in hash table
  61.     int numItems;
  62.  
  63.     int getCharInt(char c) {
  64.         if (c >= 'a' && c <= 'z') {
  65.             return c;
  66.         } else {
  67.             return c % 'a';
  68.         }
  69.     }
  70.  
  71.     int H(string str) {
  72.         int prime1 = 60373;
  73.         int prime2 = 80387;
  74.         int prime3 = 90821;
  75.         int h = 9973;
  76.  
  77.         for (int x = 0; x < str.length(); x++) {
  78.             h = (h*prime1) * (str[x]*prime2);
  79.             h%=prime3;
  80.         }
  81.  
  82.         if (h < 0) {
  83.             h *= -1;
  84.         }
  85.         h = h % prime3;
  86.         return h % capacity;
  87.     }
  88.  
  89. public:
  90.  
  91.     chainingTable() {
  92.         capacity = 19471;    // 9973, and 194771 are also prime
  93.         numItems = 0;
  94.         table = new linkedList[capacity];
  95.     }
  96.  
  97.     //use this to measure how good your
  98.     //hash function is.
  99.     double averageSearchCost() {
  100.         double sumSquares = 0;
  101.         for (int i = 0; i < capacity; i++) {
  102.             sumSquares += (table[i].size() * table[i].size());
  103.         }
  104.         return sumSquares / numItems;
  105.     }
  106.  
  107.     void insert(string x) {
  108.         int index = H(x);
  109.         table[index].addBack(x);
  110.  
  111.         numItems += 1;
  112.     }
  113.  
  114.     bool contains(string x) {
  115.         int index = H(x);
  116.         if (table[index].contains(x)) {
  117.             //table[index].display();
  118.             return true;
  119.         }
  120.     }
  121. };
  122.  
  123. // ========= stringUtil.h ==========
  124. //
  125. // Created by Oscar Torres on 2/14/17.
  126. //
  127.  
  128. class stringUtil {
  129. private:
  130.     string toLower(string str) {
  131.         string temp = "";
  132.         for (int x = 0; x < str.length(); x++) {
  133.             if (str[x] >= 'A' && str[x] <= 'Z') {
  134.                 char c = static_cast<char>(str[x] + 32);
  135.                 temp += c;
  136.             } else {
  137.                 temp += str[x];
  138.             }
  139.         }
  140.         return temp;
  141.     }
  142.  
  143.     bool isLetter(char c) {
  144.         return c >= 'a' && c <= 'z';
  145.     }
  146.  
  147.     bool isPunct(char c) {
  148.         return !isLetter(c);
  149.     }
  150.  
  151.     string removePunct(string str) {
  152.         string total = "";
  153.         for (int x = 0; x < str.length(); x++) {
  154.             if (!isPunct(str[x])) {
  155.                 total += str[x];
  156.             }
  157.         }
  158.         return total;
  159.     }
  160.  
  161. public:
  162.  
  163.     string scrubText(string str) {
  164.         return removePunct(toLower(str));
  165.     }
  166. };
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment