mathsgod / semantic-splitter-php
这是一个基于语义意义将文本段落分割成句子的PHP库。
1.1.0
2024-07-17 08:47 UTC
Requires (Dev)
- mathsgod/openai-client: ^1.3
README
这是一个简单的工具,可以根据语义意义将文本分割成句子。具有相似意义的句子将被组合在一起。每个句子应以换行符分隔。
安装
composer require mathsgod/semantic-splitter-php
使用方法
$splitter = new TextSplitter\SemanticTextSplitter(new MyEmbeddingRetriever()); $sentences= $splitter->split("I am a sentence. I am another sentence. I am a sentence that is a question? 這是一個中文句子。 這是另一個中文句子。 如果句子意思接近, 這個工具會把他們放在一起。"); print_r($sentences);
/// 输出
Array
(
[0] => I am a sentence.
I am another sentence.
I am a sentence that is a question?
[1] => 這是一個中文句子。
這是另一個中文句子。
如果句子意思接近, 這個工具會把他們放在一起。
如果句子意思不接近, 這個工具會把他們分開。
作者: 陳大文
)
嵌入检索器
Semantic Text Splitter 需要一个嵌入检索器才能工作。你可以通过实现 TextSplitter\EmbeddingRetrieverInterface
接口来实现自己的检索器。
class MyEmbeddingRetriever implements TextSplitter\EmbeddingRetrieverInterface { public function getEmbedding(string $text): array { // Implement your own embedding retriever here // for example, you can use OpenAI to get the embedding of the text return [0.1, 0.2, 0.3]; } }