tabuna/similar

Unlock the power of effortless grouping by identifying similar strings based on shared topics within a set of sentences.
93,117 6
Install
composer require tabuna/similar
Latest Version:2.2.0
PHP:^7.3|^8.0
Laravel:8 · 9
License:MIT
Last Updated:Mar 12, 2024
Links: GitHub  ·  Packagist
Maintainer: tabuna

Similar

Unit tests

This is an elementary library for working on identifying similar strings in PHP without using machine learning. It allows you to get groups of one topic from the transferred set of sentences. For example, combine news headlines from different publications, as Google News does.

Installation

Run this at the command line:

$ composer require esplora/similar

Usage

We need to create an object by passing a closure function as an argument, which checks if two strings are similar:

use Esplora\Similar\Similar;

$similar = new Similar(function (string $a, string $b) {
    similar_text($a, $b, $copy);

    return 51 < $copy;
});

Note that you don't need to use similar_text. You can use other implementations like soundex or something else.

Then we have to call the findOut method passing it a one-dimensional array with strings:

$similar->findOut([
    'Elon Musk gets mixed COVID-19 test results as SpaceX launches astronauts to the ISS',
    'Elon Musk may have Covid-19, should quarantine during SpaceX astronaut launch Sunday',

    // Superfluous word
    'Can Trump win with ‘fantasy’ electors bid? State GOP says no',
]);

As a result, there will be only one group containing headers:

'Elon Musk gets mixed COVID-19 test results as SpaceX launches astronauts to the ISS',
'Elon Musk may have Covid-19, should quarantine during SpaceX astronaut launch Sunday',

Keys

The input array stores its keys so that you can do additional processing:

$similar->findOut([
  'kos' => "Trump acknowledges Biden's win in latest tweet",
  'foo' => 'Elon Musk gets mixed COVID-19 test results as SpaceX launches astronauts to the ISS',
  'baz' => 'Trump says Biden won but again refuses to concede',
  'bar' => 'Elon Musk may have Covid-19, should quarantine during SpaceX astronaut launch Sunday',
]);

The result will be two groups:

[
  'foo' => 'Elon Musk gets mixed COVID-19 test results as SpaceX launches astronauts to the ISS',
  'bar' => 'Elon Musk may have Covid-19, should quarantine during SpaceX astronaut launch Sunday',
],
[
  'baz' => 'Trump says Biden won but again refuses to concede',
  'kos' => "Trump acknowledges Biden's win in latest tweet",
],

Objects

It is also possible to pass objects to evaluate more complex conditions. Each passed object must be able to cast to a string via the __toString() method.

$similar->findOut([
    new FixtureStingObject('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'),
]);

License

The MIT License (MIT). Please see License File for more information.

Related Packages

esplora/similar

Unlock the power of effortless grouping by identifying similar strings based on...

137,258 6
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

641,106,524 9,700
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

502,645,077 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

418,291,778 609

Version History

Version Released PHP Laravel License
2.2.0 ^7.3|^8.0 ~8.0|~9.0 MIT
2.1.2 >=7.3 ~8.0 MIT
2.1.1 >=7.3 ~8.0 MIT
2.1.0 >=7.3 ~8.0 MIT
2.0.0 >=7.3 ~8.0 MIT