2017-03-26 3 views
-1

私はC++を新しくしました。逆索引を作ろうとしていますが、コードを理解できません。私は単語の頻度を数えたいと思う。あなたはこのコードと単語の頻度を数える方法を説明できますか?この問題を解決するために私を助けてください。逆インデックスノードの追加語

class node{ 
public: 
node() { 
    clear(); 
} 
node(char z) { 
    clear(); 
} 
~node() { 
    for (int x = 0; x < MAX_NODES; x++) { 
     if (next[x]) { 
      delete next[x]; 
     } 
    } 
} 
void clear() { 
    for (int x = 0; x < MAX_NODES; x++){ 
     next[x] = 0; 
     isWord = false; 
    } 
} 
bool isWord; 
int count;//frq 
std::vector<std::string> files; 
node* next[MAX_NODES]; 
map<string, int> counts; 
}; 
class index { 
public: 
void add(std::string s, std::string fileName) { 
    std::transform(s.begin(), s.end(), s.begin(), tolower); 
    std::string h; 
    int freq=0; 
    for (std::string::iterator i = s.begin(); i != s.end(); i++) { 
     if (*i == 32) { 
      pushFileName(addWord(h), fileName); 
      h.clear(); 
      continue; 
     } 
     h.append(1, *i); 
    } 
    if (h.length()){ 
     pushFileName(addWord(h), fileName); 
    } 
} 
void findWord(std::string s, map<string, int> counts) { 
    std::vector<std::string> v = find(s); 
    if (!v.size()) { 
     std::cout <<"'"<< s + "' is not found!\n"; 
     return; 
    } 
    std::cout << "'" << s << "' is found in:\n"; 
    for (std::vector<std::string>::iterator i = v.begin(); i != v.end(); i++) { 
     std::cout << *i << "\n"; 

    } 
    std::cout << "frequency is : "; 

} 
private: 
void pushFileName(node* n, std::string fn) { 
    std::vector<std::string>::iterator i = std::find(n->files.begin(), n->files.end(), fn); 
    if (i == n->files.end()){ 
     n->files.push_back(fn); 
     n->count; 
    } 
} 

const std::vector<std::string>& find(std::string s) { 
    size_t idx; 
    std::transform(s.begin(), s.end(), s.begin(), tolower); 
    node* rt = &root; 
    for (std::string::iterator i = s.begin(); i != s.end(); i++) { 
     idx = _CHARS.find(*i); 
     if (idx < MAX_NODES) { 
      if (!rt->next[idx]){ 
       return std::vector<std::string>(); 
      } 
      rt = rt->next[idx]; 
     } 
    } 
    if (rt->isWord) return rt->files; 
    return std::vector<std::string>(); 
} 
node* addWord(std::string s) { 
    size_t idx; 
    node *rt = &root, *n; 
    for (std::string::iterator i = s.begin(); i != s.end(); i++) { 
     idx = _CHARS.find(*i); 
     if (idx < MAX_NODES) { 
      n = rt->next[idx]; 
      if (n){ 
       rt = n; 
       continue; 
      } 
      n = new node(*i); 
      rt->next[idx] = n; 
      rt = n; 
     } 
    } 
    rt->isWord = true; 
    rt->count++; 
    return rt; 
} 
node root; 
}; 

class index { 
public: 
void add(std::string s, std::string fileName) { 
    std::transform(s.begin(), s.end(), s.begin(), tolower); 
    std::string h; 
    int freq=0; 
    for (std::string::iterator i = s.begin(); i != s.end(); i++) { 
     if (*i == 32) { 
      pushFileName(addWord(h), fileName); 
      h.clear(); 
      continue; 
     } 
     h.append(1, *i); 
    } 
    if (h.length()){ 
     pushFileName(addWord(h), fileName); 
    } 
} 
void findWord(std::string s, map<string, int> mFilesFreq) { 
    std::vector<std::string> v = find(s); 
    if (!v.size()) { 
     std::cout <<"'"<< s + "' is not found!\n"; 
     return; 
    } 
    std::cout << "'" << s << "' is found in:\n"; 
    for (std::vector<std::string>::iterator i = v.begin(); i != v.end(); i++) { 
     std::cout << *i << "\n"; 

    } 
    std::cout << "frequency is : "; 

} 

答えて

1

あなたは回addの数が与えられた単語のために呼ばれたカウントされている場合、あなたはおそらくrt->count++;rt->isWord = true;を交換し、あなたのstruct nodeint countbool isWordを交換したいです。

+0

私は、(ファイルとカウントの代わりに) 'map counts'を使用します。ここで' string'はファイル名で、 'int'はカウントです。カウントを更新するには、 'rt-> counts [filename] ++;' –

関連する問題