Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nemorize committed Oct 2, 2022
0 parents commit 62b3ac0
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/.idea/
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MIT License

Copyright (c) 2022 Ji Yong, Kim.

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 theSoftware.

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.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# uwuify-php
Uwuify any sentence or word with various options.

<div id="badges">
<img src="https://img.shields.io/packagist/v/nemo9l/uwuify?color=F85E83&label=uwuify&logo=php&style=flat-square" alt="PHP version >= 8.0" />
<img src="https://img.shields.io/packagist/l/nemo9l/uwuify?color=F85E83&logo=mit&style=flat-square" alt="License MIT" />
<img src="https://img.shields.io/packagist/dm/nemo9l/uwuify?color=F85E83&logo=mit&style=flat-square" alt="Download counts, Sorry for screen reader." />
</div>

## Installation
```bash
composer require nemo9l/uwuify
composer install
```

## Usage

### Basic usage

```php
$uwuifier = new Nemo9l\Uwuify\Uwuify();
$result = $uwuifier->uwuify('Uwuify any sentence or word with various options.');
```

### Advanced usage

```php
// (float $regexModifier = null, float $exclamationModifier = null, array $spaceModifier = [])
$uwuifier = new Nemo9l\Uwuify\Uwuify(0.75, 0.75, [ 'faces' => 0.025, 'actions' => 0.025, 'stutter' => 0.05 ]);
$result = $uwuifier->uwuify('Uwuify any sentence or word with various options.');
```

#### $regexModifier
`$regexModifier` property affects what percentage of regex(defined at `static $_regexMaps`) replacements will be applied to the sentence.
Default value is `1.0` which means 100% of regex replacements will be applied.

#### $exclamationModifier
`$exclamationModifier` property affects what percentage of exclamation marks(defined at `static $_exclamations`) will be replaced.

#### $spaceModifier
`$spaceModifier` property affects what percentage of spaces will be replaced with various options.
It can be an array with following keys:
- `faces` - affects what percentage of spaces will be replaced with faces(defined at `static $_faces`).
- `actions` - affects what percentage of spaces will be replaced with actions(defined at `static $_actions`).
- `stutter` - affects what percentage of spaces will have some appends to make it stutter.

### License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "nemo9l/uwuify",
"description": "String uwuifying library for PHP",
"type": "library",
"license": "MIT",
"homepage": "https://github.com/nemo9l/uwuify-php",
"authors": [
{
"name": "Ji Yong, Kim",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Nemo9l\\Uwuify\\": "src/"
}
},
"require": {
"php": ">=8.0"
}
}
243 changes: 243 additions & 0 deletions src/Uwuify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php /** @noinspection SpellCheckingInspection */

namespace Nemo9l\Uwuify;

class Uwuify
{
protected static array $_faces = [
'(・`ω´・)',
';;w;;',
'OwO',
'UwU',
'>w<',
'^w^',
'\(^o\) (/o^)/',
'( ^ _ ^)∠☆',
'(U __ U)',
'(*^*)',
'(+_+)',
'(/_;)',
'(^.^)',
'(♥_♥)',
'*(^O^)*',
'*(^o^)*',
'ʕ•ᴥ•ʔ',
'(*^.^*)',
'(。♥‿♥。)'
];

protected static array $_actions = [
'*blushes*',
'*nuzzles*',
'*notices bulge*',
'*whispers to self*',
'*cries*',
'*walks away*',
'*sweats*',
'*boops your nose*',
'*sees bulge*'
];

protected static array $_exclamations = [
'!?',
'?!!',
'?!?1',
'!!11',
'?!1?'
];

protected static array $_regexMaps = [
'/(?:r|l)/' => 'w',
'/(?:R|L)/' => 'W',
'/n([aeiou])/' => 'ny$1',
'/N([aeiou])/' => 'Ny$1',
'/N([AEIOU])/' => 'Ny$1',
'/ove/' => 'uv'
];

public float $regexModifier = 1;
public float $exclamationModifier = 1;
public array $spaceModifier = [ 'faces' => 0.05, 'actions' => 0.075, 'stutters' => 0.1 ];

/**
* Constructor.
*
* @param float|null $regexModifier
* @param float|null $exclamationModifier
* @param array $spaceModifier
*/
public function __construct(float $regexModifier = null, float $exclamationModifier = null, array $spaceModifier = [])
{
if($regexModifier !== null)
{
$this->regexModifier = $regexModifier;
}

if($exclamationModifier !== null)
{
$this->exclamationModifier = $exclamationModifier;
}

if($spaceModifier !== null)
{
if(isset($spaceModifier['faces']) && is_float($spaceModifier['faces']))
{
$this->spaceModifier['faces'] = $spaceModifier['faces'];
}
if(isset($spaceModifier['actions']) && is_float($spaceModifier['actions']))
{
$this->spaceModifier['actions'] = $spaceModifier['actions'];
}
if(isset($spaceModifier['stutters']) && is_float($spaceModifier['stutters']))
{
$this->spaceModifier['stutters'] = $spaceModifier['stutters'];
}
}
}

/**
* Translate some words to uwu from a sentence.
*
* @param string $sentence
* @return string
*/
public function uwuifyWords(string $sentence): string
{
$words = explode(' ', $sentence);
foreach($words as &$word)
{
if(filter_var($word, FILTER_VALIDATE_URL) !== false)
{
continue;
}

if(str_starts_with($word, '@'))
{
continue;
}

foreach(self::$_regexMaps as $regex => $replacement)
{
if($this->getRandomFloat() <= $this->regexModifier)
{
$word = preg_replace($regex, $replacement, $word);
}

if($this->getUppercaseProportion($word) < 0.5)
{
$word = lcfirst($word);
}
}
}

return implode(' ', $words);
}

/**
* Translate some exclamations to uwu from a sentence.
*
* @param string $sentence
* @return string
*/
public function uwuifyExclamations(string $sentence): string
{
$words = explode(' ', $sentence);
foreach($words as &$word)
{
if($this->getRandomFloat() > $this->exclamationModifier)
{
continue;
}

$replacedWord = preg_replace('/[?!]+$/', '', $word);
if($word == $replacedWord)
{
continue;
}

$word = $replacedWord . self::$_exclamations[array_rand(self::$_exclamations)];
}

return implode(' ', $words);
}

/**
* Translate some spaces to faces, action or stutters from a sentence.
*
* @param string $sentence
* @return string
*/
public function uwuifySpaces(string $sentence): string
{
$words = explode(' ', $sentence);

$faceThreshold = $this->spaceModifier['faces'];
$actionThreshold = $this->spaceModifier['actions'] + $faceThreshold;
$stutterThreshold = $this->spaceModifier['stutters'] + $actionThreshold;

foreach($words as &$word)
{
$firstCharacter = substr($word, 0, 1);

if($this->getRandomFloat() <= $faceThreshold && self::$_faces)
{
$word .= ' ' . self::$_faces[array_rand(self::$_faces)];
}
elseif($this->getRandomFloat() <= $actionThreshold && self::$_actions)
{
$word .= ' ' . self::$_actions[array_rand(self::$_actions)];
}
elseif($this->getRandomFloat() <= $stutterThreshold)
{
$stutterCount = rand(1, 3);
$word = str_repeat($firstCharacter . '-', $stutterCount) . $word;
}
}

return implode(' ' , $words);
}

/**
* Uwuify sentences.
*
* @param string $sentence
* @return string
*/
public function uwuify(string $sentence): string
{
$sentence = $this->uwuifyWords($sentence);
$sentence = $this->uwuifyExclamations($sentence);
return $this->uwuifySpaces($sentence);
}

/**
* Get a random float between 0 and 1.
*
* @return float
*/
private function getRandomFloat(): float
{
return mt_rand() / mt_getrandmax();
}

/**
* Get a proportion of uppercase characters in a string.
* @param string $word
* @return float
*/
private function getUppercaseProportion(string $word): float
{
$totalCharacters = strlen($word);
$upperCharacters = 0;

foreach(str_split($word) as $character)
{
if(strtoupper($character) == $character)
{
$upperCharacters++;
}
}

return $upperCharacters / $totalCharacters;
}
}

0 comments on commit 62b3ac0

Please sign in to comment.