-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix ld warning * Add ut for standard analyzer * Add ngram analyzer
- Loading branch information
Showing
6 changed files
with
307 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
module; | ||
|
||
#include "string_utils.h" | ||
#include <iostream> | ||
import stl; | ||
import term; | ||
import stemmer; | ||
import analyzer; | ||
import tokenizer; | ||
module ngram_analyzer; | ||
|
||
namespace infinity { | ||
|
||
bool NGramAnalyzer::NextInString(const char *data, | ||
SizeT length, | ||
SizeT *__restrict pos, | ||
SizeT *__restrict token_start, | ||
SizeT *__restrict token_length) { | ||
*token_start = *pos; | ||
*token_length = 0; | ||
SizeT code_points = 0; | ||
for (; code_points < ngram_ && *token_start + *token_length < length; ++code_points) { | ||
if (std::isspace(data[*token_start + *token_length])) { | ||
*pos += UTF8SeqLength(static_cast<u8>(data[*pos])); | ||
*token_start = *pos; | ||
*token_length = 0; | ||
return true; | ||
} | ||
SizeT sz = UTF8SeqLength(static_cast<u8>(data[*token_start + *token_length])); | ||
*token_length += sz; | ||
} | ||
*pos += UTF8SeqLength(static_cast<u8>(data[*pos])); | ||
return code_points == ngram_; | ||
} | ||
|
||
int NGramAnalyzer::AnalyzeImpl(const Term &input, void *data, HookType func) { | ||
unsigned char level = 0; | ||
|
||
SizeT len = input.text_.length(); | ||
if (len == 0) | ||
return 0; | ||
|
||
SizeT cur = 0; | ||
SizeT token_start = 0; | ||
SizeT token_length = 0; | ||
SizeT offset = input.word_offset_; | ||
while (cur < len && NextInString(input.text_.c_str(), len, &cur, &token_start, &token_length)) { | ||
if (token_length == 0) | ||
continue; | ||
func(data, input.text_.c_str() + token_start, token_length, offset, Term::AND, level, false); | ||
offset++; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
} // namespace infinity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "unit_test/base_test.h" | ||
#include <iostream> | ||
import stl; | ||
import term; | ||
import ngram_analyzer; | ||
import standard_analyzer; | ||
|
||
using namespace infinity; | ||
|
||
class NGramAnalyzerTest : public BaseTest {}; | ||
|
||
TEST_F(NGramAnalyzerTest, test1) { | ||
NGramAnalyzer analyzer(2); | ||
TermList term_list; | ||
String input("hello world 123"); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 10U); | ||
ASSERT_EQ(term_list[0].text_, String("he")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("el")); | ||
ASSERT_EQ(term_list[1].word_offset_, 1U); | ||
ASSERT_EQ(term_list[2].text_, String("ll")); | ||
ASSERT_EQ(term_list[2].word_offset_, 2U); | ||
ASSERT_EQ(term_list[3].text_, String("lo")); | ||
ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
ASSERT_EQ(term_list[4].text_, String("wo")); | ||
ASSERT_EQ(term_list[4].word_offset_, 4U); | ||
ASSERT_EQ(term_list[5].text_, String("or")); | ||
ASSERT_EQ(term_list[5].word_offset_, 5U); | ||
ASSERT_EQ(term_list[6].text_, String("rl")); | ||
ASSERT_EQ(term_list[6].word_offset_, 6U); | ||
ASSERT_EQ(term_list[7].text_, String("ld")); | ||
ASSERT_EQ(term_list[7].word_offset_, 7U); | ||
ASSERT_EQ(term_list[8].text_, String("12")); | ||
ASSERT_EQ(term_list[8].word_offset_, 8U); | ||
ASSERT_EQ(term_list[9].text_, String("23")); | ||
ASSERT_EQ(term_list[9].word_offset_, 9U); | ||
} | ||
|
||
TEST_F(NGramAnalyzerTest, test2) { | ||
NGramAnalyzer analyzer(1); | ||
TermList term_list; | ||
String input("abc de fg"); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 7U); | ||
ASSERT_EQ(term_list[0].text_, String("a")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("b")); | ||
ASSERT_EQ(term_list[1].word_offset_, 1U); | ||
ASSERT_EQ(term_list[2].text_, String("c")); | ||
ASSERT_EQ(term_list[2].word_offset_, 2U); | ||
ASSERT_EQ(term_list[3].text_, String("d")); | ||
ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
ASSERT_EQ(term_list[4].text_, String("e")); | ||
ASSERT_EQ(term_list[4].word_offset_, 4U); | ||
ASSERT_EQ(term_list[5].text_, String("f")); | ||
ASSERT_EQ(term_list[5].word_offset_, 5U); | ||
ASSERT_EQ(term_list[6].text_, String("g")); | ||
ASSERT_EQ(term_list[6].word_offset_, 6U); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "unit_test/base_test.h" | ||
|
||
import stl; | ||
import term; | ||
import standard_analyzer; | ||
using namespace infinity; | ||
|
||
class StandardAnalyzerTest : public BaseTest {}; | ||
|
||
TEST_F(StandardAnalyzerTest, test1) { | ||
StandardAnalyzer analyzer; | ||
TermList term_list; | ||
String input("Boost unit tests."); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 3U); | ||
ASSERT_EQ(term_list[0].text_, String("boost")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("unit")); | ||
ASSERT_EQ(term_list[1].word_offset_, 1U); | ||
ASSERT_EQ(term_list[2].text_, String("tests")); | ||
ASSERT_EQ(term_list[2].word_offset_, 2U); | ||
// ASSERT_EQ(term_list[3].text_, PLACE_HOLDER); | ||
// ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
} | ||
|
||
TEST_F(StandardAnalyzerTest, test2) { | ||
StandardAnalyzer analyzer; | ||
TermList term_list; | ||
String input("Boost unit tests."); | ||
analyzer.SetCaseSensitive(true, false); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 3U); | ||
ASSERT_EQ(term_list[0].text_, String("Boost")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("unit")); | ||
ASSERT_EQ(term_list[1].word_offset_, 1U); | ||
ASSERT_EQ(term_list[2].text_, String("tests")); | ||
ASSERT_EQ(term_list[2].word_offset_, 2U); | ||
// ASSERT_EQ(term_list[3].text_, PLACE_HOLDER); | ||
// ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
} | ||
|
||
TEST_F(StandardAnalyzerTest, test3) { | ||
StandardAnalyzer analyzer; | ||
TermList term_list; | ||
String input("Boost unit tests."); | ||
analyzer.SetExtractEngStem(true); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 4U); | ||
ASSERT_EQ(term_list[0].text_, String("boost")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("unit")); | ||
ASSERT_EQ(term_list[1].word_offset_, 1U); | ||
ASSERT_EQ(term_list[2].text_, String("tests")); | ||
ASSERT_EQ(term_list[2].word_offset_, 2U); | ||
ASSERT_EQ(term_list[3].text_, String("test")); | ||
ASSERT_EQ(term_list[3].word_offset_, 2U); | ||
// ASSERT_EQ(term_list[3].text_, PLACE_HOLDER); | ||
// ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
} | ||
|
||
TEST_F(StandardAnalyzerTest, test4) { | ||
StandardAnalyzer analyzer; | ||
TermList term_list; | ||
String input("Boost unit tests."); | ||
analyzer.SetCaseSensitive(true, true); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 4U); | ||
ASSERT_EQ(term_list[0].text_, String("Boost")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("boost")); | ||
ASSERT_EQ(term_list[1].word_offset_, 0U); | ||
ASSERT_EQ(term_list[2].text_, String("unit")); | ||
ASSERT_EQ(term_list[2].word_offset_, 1U); | ||
ASSERT_EQ(term_list[3].text_, String("tests")); | ||
ASSERT_EQ(term_list[3].word_offset_, 2U); | ||
// ASSERT_EQ(term_list[3].text_, PLACE_HOLDER); | ||
// ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
} | ||
|
||
TEST_F(StandardAnalyzerTest, test5) { | ||
StandardAnalyzer analyzer; | ||
TermList term_list; | ||
String input("BoostBoostboostBoostboost unit tests."); | ||
analyzer.Analyze(input, term_list); | ||
|
||
ASSERT_EQ(term_list.size(), 3U); | ||
ASSERT_EQ(term_list[0].text_, String("boostboostboostboostboost")); | ||
ASSERT_EQ(term_list[0].word_offset_, 0U); | ||
ASSERT_EQ(term_list[1].text_, String("unit")); | ||
ASSERT_EQ(term_list[1].word_offset_, 1U); | ||
ASSERT_EQ(term_list[2].text_, String("tests")); | ||
ASSERT_EQ(term_list[2].word_offset_, 2U); | ||
// ASSERT_EQ(term_list[3].text_, PLACE_HOLDER); | ||
// ASSERT_EQ(term_list[3].word_offset_, 3U); | ||
} |