Skip to content

Commit

Permalink
CPP 不能用
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhijun committed Sep 26, 2024
1 parent 8b25a15 commit c37a350
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
56 changes: 40 additions & 16 deletions cpp/ToolGood.Words/BaseSearchEx.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#pragma
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

#include "IntDictionary.cpp"
#include <map>
#include <vector>
#include <string>
#include "TrieNode.h"
#include <iostream>
#include <codecvt>
using std::map;
using std::vector;
using std::string;
using std::wstring;

class BaseSearchEx
{
Expand All @@ -21,32 +27,50 @@ class BaseSearchEx
int* _keywordLengths;

protected:
virtual void SetKeywords2(string _keywords[])

wstring s2ws(string& s)
{
setlocale(LC_ALL, "chs");

const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest, _Source, _Dsize);
wstring result = _Dest;
delete[]_Dest;

setlocale(LC_ALL, "C");
return result;
}

virtual void SetKeywords2(vector<string> _keywords)
{
TrieNode root;
map<int, vector<TrieNode>> allNodeLayers;
vector<vector<TrieNode>> allNodeLayers;
vector<TrieNode> fristLayer;
allNodeLayers.push_back(fristLayer);
int kindex = 0;

for (size_t i = 0; i < _keywords->size(); i++) /// _keywords->size() 这个值有问题
for (size_t i = 0; i < _keywords.size(); i++)
{
string p = _keywords[i];
wstring wcs = s2ws(p);/// to_wide_string 这个有问题

TrieNode nd = root;
for (size_t j = 0; j < p.length(); j++) // 这个 p.length() 对中文有问题
for (size_t j = 0; j < wcs.size(); j++) // 这个 p.length() 对中文有问题
{
nd = nd.Add((char)p.c_str()[j]); // 返回是byte 不是char
nd = nd.Add(wcs[j]); // 返回是byte 不是char
if (nd.Layer == 0) {
nd.Layer = j + 1;
auto find = allNodeLayers.find(nd.Layer);
if (find == allNodeLayers.end())
if (nd.Layer <allNodeLayers.size())
{
allNodeLayers[nd.Layer].push_back(nd);
}
else {
vector<TrieNode> trieNodes;
trieNodes.push_back(nd);
allNodeLayers[nd.Layer] = trieNodes;
}
else
{
find->second.push_back(nd);
allNodeLayers.push_back(trieNodes);
}
}
}
Expand All @@ -55,19 +79,19 @@ class BaseSearchEx
allNode.push_back(root);
for (size_t i = 0; i < allNodeLayers.size(); i++)
{
auto item = allNodeLayers.at(i);
vector<TrieNode> item = allNodeLayers[i];
for (size_t j = 0; j < item.size(); j++)
{
allNode.push_back(item[j]);
}
}
allNodeLayers.clear();

for (size_t i = 0; i < allNode.size(); i++)
for (size_t i = 1; i < allNode.size(); i++)
{
TrieNode nd = allNode[i];
nd.Index = i;
TrieNode r = *(nd.Parent->Failure);
TrieNode r = *(nd.Parent->Failure);// 这里有bug
char c = nd.Char;
while (&r != NULL && (r.m_values.size() == 0 || r.m_values.find(c) == r.m_values.end())) r = *r.Failure;
if (&r == NULL)
Expand Down Expand Up @@ -211,7 +235,7 @@ class BaseSearchEx
private:
int CreateDict(string keywords) {
_dict = new unsigned short[0x10000];
map<char, unsigned int> dictionary;
map<wchar_t, unsigned int> dictionary;
int index = 1;
for (size_t i = 0; i < keywords.size(); i++)
{
Expand Down
8 changes: 4 additions & 4 deletions cpp/ToolGood.Words/ToolGood.Words.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
int main()
{
WordsSearchEx search;
auto ks = new string[2];
ks[0] = "中国";
ks[1] = "国人";
ks[2] = "zg人";
vector<string> ks;
ks.push_back("中国");
ks.push_back("国人");
ks.push_back("zg人");

search.SetKeywords(ks);
auto r = search.FindFirst("我是中国人");
Expand Down
6 changes: 3 additions & 3 deletions cpp/ToolGood.Words/TrieNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class TrieNode
public:
int Index;
int Layer;
char Char;
wchar_t Char;
vector<int> Results;
map<char, TrieNode> m_values;
map<wchar_t, TrieNode> m_values;
TrieNode* Failure;
TrieNode* Parent;
bool IsWildcard;
Expand All @@ -22,7 +22,7 @@ class TrieNode
Parent = NULL;
}

TrieNode Add(char c)
TrieNode Add(wchar_t c)
{
auto find = m_values.find(c);
if (find != m_values.end())
Expand Down
2 changes: 1 addition & 1 deletion cpp/ToolGood.Words/WordsSearchEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WordsSearchEx :BaseSearchEx
public:
WordsSearchEx() = default;

void SetKeywords(string _keywords[]) {
void SetKeywords(vector<string> _keywords) {
this->SetKeywords2(_keywords);
}

Expand Down

0 comments on commit c37a350

Please sign in to comment.