Code Monkey home page Code Monkey logo

profanity's Introduction

Profanity

StyleCI Status Build For PHP Total Downloads Latest Stable Version License

What is Profanity?

Profanity is a PHP library that allows blocking bad words out of a given string. It currently blocks more than 2.555 words in different languages. It's very easy to use and can filter a 100 chars string in about 0.0021 seconds!

Sample Filter

This example is a simple string that is going to be filtered using this library.

$clean_words = Profanity::blocker('My cool string bitch')->filter()

// My cool string *****

Documentation

Installation

First, require it using composer:

composer require consoletvs/profanity

Laravel

To install it on laravel, simply add the service provider and the alias in config/app.php:

Service Provider:

ConsoleTVs\Profanity\ProfanityServiceProvider::class,

Alias (optional):

'Profanity' => ConsoleTVs\Profanity\Facades\Profanity::class,

Usage

PHP

$words = 'My bad word bitch';
$clean_words = \ConsoleTVs\Profanity\Builder::blocker($words)->filter();
// My bad word *****

Laravel

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ConsoleTVs\Profanity\Facades\Profanity;
// or using the alias:
// use Profanity;

class HomeController extends Controller
{
    /**
     * Simple test function.
     *
     * @return string
     */
    public function test()
    {
        $words = 'My bad word bitch';

        return Profanity::blocker($words)->filter();
        // My bad word *****
    }
}

Methods

Constructor

  • blocker($text, $blocker = '****')

    Setup the text and the blocker to use.

    note: The blocker defaults to ****

Class Methods

  • strict(true/false)

    Set the strict mode to enabled or disabled. Strict mode replaces the string even if it's not a word: assets => (tagged as ass) => **** Disable to ensure normal usage. Defaults to false.

  • strictClean(true/false)

    Set the strict clean mode to enabled or disabled. Strict clean mode ensures the first blocked character is repeated for each bad word character. If so, a bad word of length = 3 (ass) will result in a ***, instead of the blocker (defaults to ****). If the blocker is -**- then the same example as above would produce --- as strict clean true and -**- as false. Defaults to true.

  • clean()

    Returns true if the text have no bad words on it.

  • filter()

    Filters the text with the given blocker.

  • badWords()

    Return the bad words found in the text.

  • text($text)

    Sets the text to filter.

  • blocker($blocker)

    Sets the string that will replace the bad words.

  • dictionary($dictionary)

    Sets the dictionary. If string is provided, it will be treated as a path and will try to decode the json contained in the path. If array is provided, it will set the dictionary.

    Example dictionary:

    [
        {
            "language": "en",
            "word": "bitch"
        },
        {
            "language": "en",
            "word": "fuck"
        }
    ]

License

MIT License

Copyright (c) 2017 Erik Campobadal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

profanity's People

Contributors

ariaieboy avatar bobbypiper avatar christhompsontldr avatar consoletvs avatar dev-graduan avatar mrtawil avatar sangnguyenplus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

profanity's Issues

New feature requested: matching not just a single word

Supposed we have a dictionary like this:

[ { "language": "en", "word": "wrapping men" }, { "language": "en", "word": "wrinkled starfish" }, ]

Can we have the badWords() to pick out only these whole strings. For example:

Sentence 1: a men is wrapping gift -> no problem
Sentence 2: a gift is wrapping men -> bad words detected -> Result ["language" => "en", "word" =>"wrapping men"]

Thank you!

How can I specify my own wordlists?

I need to do it in Spanish from Mexico and there are a lot of bad words missing. Updating directly the Dictionary is not an option since I am using composer.

production WARNING php 8.1

[2023-02-23 09:11:23] production.WARNING: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in vendor/consoletvs/profanity/Classes/Blocker.php on line 135

PHP 8.1 exception with illuminate/support 5.4.36

The badWords() function throws an exception in PHP 8.1

public function badWords()
    {
       // exception thrown here:
       return collect($this->dictionary)->filter(function ($value) {
          
        })->toArray();
    }

ErrorException: Return type of Illuminate\Support\Collection::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

I tried to update illuminate/contracts and illuminate/support to v 8.78 but get this issue:

Problem 1
- illuminate/support v5.4.36 requires illuminate/contracts 5.4.* -> found illuminate/contracts[v5.4.0, ..., 5.4.x-dev] but it conflicts with your root composer.json require (^8.78).
- consoletvs/profanity 3.1.0 requires illuminate/support 5.4.|5.5.|5.6.|5.7.|5.8.|6.|7.|8. -> satisfiable by illuminate/support[v5.4.36].
- consoletvs/profanity is locked to version 3.1.0 and an update of this package was not requested.

Filtering a specific language

Hi

I don't think this is possible, but would like to suggest an option to set a specific language when filtering.
For instance, if I want to filter Portuguese word only, I would pass the pt to the blocker or filter: blocker($words, $lang) or filter( $lang).
If $lang is not set, all words should be used.

Thanks

Whole word matching option

Hi,

Thank you for sharing the code. Is it possible to have whole word only matching option? For example, in my string there is a word "circumstances" and it is picked out by badWords() as "cum" :-( Thanks!

Kashida character problem

hello i am back with another problem from awsome languages persian and arabic :D
here is a wikipedia page that explain this character
https://en.wikipedia.org/wiki/Kashida

this character wont change the meaning of a word and its just using for justification
here is some example

word => Raħīm | meaning => merciful | normal => رحیم | Kashida => رحــیم

the problem is that if someone use a bad word with this character this package can't recognize it as a bad word

Inserting Persian And Arabic dictionary and Getting the badwords

Hi again
I want to insert an array of Persian and Arabic dictionary array and get the badwords

The text cleanness is discovered correctly but the badwords function returns unknown characters

$bad = Profanity::blocker('فحش است')->dictionary([['language' => 'fa' , 'word' => 'فحش' ]])->badWords();

returns
array (size=1) 0 => array (size=2) 'language' => string 'fa' (length=2) 'word' => string '������' (length=6)

Please help me with this problem
Thank you

Wrong detection

Hi. for example we have string like this:

$value = '<p><span style="color: rgb(246,139,106);">Lauren J.</span>, Hi</p>';

In this case 139 will be replaced as *** because:

Profanity::blocker(trim($value))->badWords() detects the bad word:

Array
(
    [2261] => Array
        (
            [language] => zh
            [word] => 13.
        )
)

Is it possible to fix? as '139' != '13.'

Laravel 10 install error

I'm trying to upgrade a Laravel 9 project to Laravel 10. when I install this package I get:

Problem 1
    - consoletvs/profanity[1.0.0, ..., 1.0.2, 2.0.0, ..., 2.0.1] require illuminate/support ^5.4 -> found illuminate/support[v5.4.0, ..., v5.8.36] but these were not loaded, likely because it conflicts with another require.
    - consoletvs/profanity 3.0.0 requires illuminate/support 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.* -> found illuminate/support[v5.4.0, ..., v5.8.36, v6.0.0, ..., v6.20.44] but these were not loaded, likely because it conflicts with another require.
    - consoletvs/profanity 3.0.1 requires illuminate/support 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.* -> found illuminate/support[v5.4.0, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6] but these were not loaded, likely because it conflicts with another require.
    - consoletvs/profanity[3.0.2, ..., 3.1.0] require illuminate/support 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*|8.* -> found illuminate/support[v5.4.0, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
    - consoletvs/profanity 3.2.0 requires illuminate/support 5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|9.* -> found illuminate/support[v5.4.0, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.9] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires consoletvs/profanity * -> satisfiable by consoletvs/profanity[1.0.0, 1.0.1, 1.0.2, 2.0.0, 2.0.1, 3.0.0, ..., 3.2.0].

You can also try re-running composer require with an explicit version constraint, e.g. "composer require consoletvs/profanity:*" to figure out if any version is installable, or "composer require consoletvs/profanity:^2.1" if you know which you need.

How do I resolve this?

persian and arabic filter not working

hello
i tested your package and its didnt work with persian language
$bad = Builder::blocker('کیری')->clean();

this code return true that is incorrect taht word is inside your default badwords list here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.