
Pythonでの高速で軽量でカスタマイズ可能なファジーおよびセマンティックテキスト検索。
Neofuzzは、ベクトル化と近隣の近隣検索手法に基づいたファジー検索ライブラリです。
これで、Levenshtein距離を使用して検索結果を再注文できます! n-gramプロセスまたはベクトル化されたプロセスが結果を正しく順序付けない場合があります。これらの場合、インデックス付きコーパスからより多くの例を取得し、それらの結果をlevenshtein距離で改良することができます。
from neofuzz import char_ngram_process
process = char_ngram_process ()
process . index ( corpus )
process . extract ( "your query" , limit = 30 , refine_levenshtein = True )ほとんどのファジー検索ライブラリは、同じカップルのファジー検索アルゴリズム(ハミング距離、レベンシュテイン距離)から地獄を最適化することに依存しています。残念ながら、これらのアルゴリズムの複雑さのために、あなたが望む速度を得る最適化の量はありません。
Neofuzzは、従来のアルゴリズムに依存して特定の速度制限を超えることはできないことを実現し、テキストベクトル化とベクトル空間で近い近隣検索を使用してこのプロセスを高速化します。
速度と精度のジレンマに関しては、Neofuzzは完全な速度になります。
PypiからNeofuzzをインストールできます。
pip install neofuzzプラグアンドプレイエクスペリエンスが必要な場合は、 char_ngram_process()プロセスを使用して、一般的に優れたクイックで汚いプロセスを作成できます。
from neofuzz import char_ngram_process
# We create a process that takes character 1 to 5-grams as features for
# vectorization and uses a tf-idf weighting scheme.
# We will use cosine distance for the nearest neighbour search.
process = char_ngram_process ( ngram_range = ( 1 , 5 ), metric = "cosine" , tf_idf = True )
# We index the options that we are going to search in
process . index ( options )
# Then we can extract the ten most similar items the same way as in
# thefuzz
process . extract ( "fuzz" , limit = 10 )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[( 'fuzzer' , 67 ),
( 'Januzzi' , 30 ),
( 'Figliuzzi' , 25 ),
( 'Fun' , 20 ),
( 'Erika_Petruzzi' , 20 ),
( 'zu' , 20 ),
( 'Zo' , 18 ),
( 'blog_BuzzMachine' , 18 ),
( 'LW_Todd_Bertuzzi' , 18 ),
( 'OFU' , 17 )]カスタムプロセスを作成することにより、Neofuzzの動作をカスタマイズできます。フードの下で、すべてのネオファズプロセスは同じ2つのコンポーネントに依存しています。
テキストの単語/セマンティックコンテンツにもっと興味がある場合は、機能としても使用できます。これは、特に文学作品などの長いテキストでは非常に便利です。
from neofuzz import Process
from sklearn . feature_extraction . text import TfidfVectorizer
# Vectorization with words is the default in sklearn.
vectorizer = TfidfVectorizer ()
# We use cosine distance because it's waay better for high-dimentional spaces.
process = Process ( vectorizer , metric = "cosine" )ファジー検索プロセスの速度では十分ではないことがわかります。この場合、いくつかのマトリックス分解方法またはトピックモデルを使用して、生成されたベクトルの二量体を減らすことが望ましい場合があります。
たとえば、私はNMF(優れたトピックモデルと信じられないほど速いモデル)を使用しています。
from neofuzz import Process
from sklearn . feature_extraction . text import TfidfVectorizer
from sklearn . decomposition import NMF
from sklearn . pipeline import make_pipeline
# Vectorization with tokens again
vectorizer = TfidfVectorizer ()
# Dimensionality reduction method to 20 dimensions
nmf = NMF ( n_components = 20 )
# Create a pipeline of the two
pipeline = make_pipeline ( vectorizer , nmf )
process = Process ( pipeline , metric = "cosine" )NeoFuzzを使用すると、セマンティック埋め込みを簡単に使用でき、注意ベースの言語モデル(BERT)、単純なニューラルワードまたはドキュメントの埋め込み(Word2vec、doc2vec、fasttextなど)、またはopenaiのLLMの両方を使用できます。
sklearn互換性のあるベクトルザーがたくさん組み込まれているembetterを試すことをお勧めします。
pip install embetter from embetter . text import SentenceEncoder
from neofuzz import Process
# Here we will use a pretrained Bert sentence encoder as vectorizer
vectorizer = SentenceEncoder ( "all-distilroberta-v1" )
# Then we make a process with the language model
process = Process ( vectorizer , metric = "cosine" )
# Remember that the options STILL have to be indexed even though you have a pretrained vectorizer
process . index ( options )