{"id":17157204,"url":"https://github.com/neveldo/textgenerator","last_synced_at":"2025-05-16T13:03:09.394Z","repository":{"id":49339159,"uuid":"46236321","full_name":"neveldo/TextGenerator","owner":"neveldo","description":"TextGenerator is a PHP package that aims to generate automated texts  from data.","archived":false,"fork":false,"pushed_at":"2024-10-23T20:38:28.000Z","size":150,"stargazers_count":92,"open_issues_count":5,"forks_count":19,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-05-09T12:42:12.087Z","etag":null,"topics":["automated","dataset","generation","php","text"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/neveldo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-15T21:20:14.000Z","updated_at":"2024-11-13T20:23:51.000Z","dependencies_parsed_at":"2024-11-14T08:01:41.051Z","dependency_job_id":"c6aac293-0202-4265-bf83-9fb82a2fc187","html_url":"https://github.com/neveldo/TextGenerator","commit_stats":{"total_commits":134,"total_committers":6,"mean_commits":"22.333333333333332","dds":0.09701492537313428,"last_synced_commit":"dcff11224e51418d4bcb10b8c0b2360f60a498e6"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neveldo%2FTextGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neveldo%2FTextGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neveldo%2FTextGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neveldo%2FTextGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neveldo","download_url":"https://codeload.github.com/neveldo/TextGenerator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254535826,"owners_count":22087398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automated","dataset","generation","php","text"],"created_at":"2024-10-14T22:08:30.859Z","updated_at":"2025-05-16T13:03:09.371Z","avatar_url":"https://github.com/neveldo.png","language":"PHP","readme":"# TextGenerator\n\nTextGenerator is a PHP package that aims to generate automated texts  from data. Feel free to comment and contribute.\n\n## Features\n\n- Text generation from template\n- Tags replacement\n- Text functions (core functions : random, random with probability, shuffle, if, loop, variables assignment, ...)\n- Nested function calls\n- Skip parts that contain empty values to prevent inconsistency in the generated text\n\n## Tags\n\nThe tags that appears in the template are replaced by the matching values. Example :\n\nData :\n\n    ['my_tag' =\u003e 'dolor']\n\nTemplate :\n\n    Lorem @my_tag ipsum\n\nOutput :\n\n    Lorem dolor ipsum\n\n## Template indentation\n\nUse the ';;' special marker to indent your template. This marker can be inserted at any position in the template, any space characters (including tabs and line breaks) following the marker will be removed.\n\nTemplate :\n\n    Quare hoc quidem praeceptum, cuiuscumque est. ;;\n        ad tollendam amicitiam valet. ;;\n            potius praecipiendum fuit. ;;\n    ut eam diligentiam adhiberemus.\n\nOutput :\n\n    Quare hoc quidem praeceptum, cuiuscumque est. ad tollendam amicitiam valet. potius praecipiendum fuit. ut eam diligentiam adhiberemus.\n\n## Core functions :\n\n### 'set'\n\nSet new tag within the template in order to be used further.\n\nData :\n\n    [\n        [\n            'sex' =\u003e 'f',\n        ]\n    ]\n\nTemplate example :\n\n    #set{@intro|how are you ?};;\n    #set{@who|#if{sex == 'm'|boy|girl}};;\n    #set{@hello|#random{Hello|Goodbye|Hi}};;\n    @hello @who @intro\n\nOutput example :\n\n    Hi girl how are you ?\n\n### 'if'\n\nDisplay text depending on a condition. The first parameter is the condition to check for. The second parameter is returned if the condition is true. The third optional parameter is returned if the condition is false.\nRead more about the syntax for the conditions on the [Symfony website](http://symfony.com/doc/current/components/expression_language/syntax.html).\nExamples :\n\n    #if{@val == 5|the value equals 5}\n    #if{@val == 5|the value equals 5|the value doesn't equal 5}\n    #if{@val \u003c 5 or @val \u003e 15|the value is lower that 5 or greater that 15|the value is between 5 and 15}\n    #if{(@val \u003e 5 and @val \u003c 15) or (@val \u003e 10 and @val \u003c 30)|then statement ...|else statement ...}\n\n### 'expr'\n\nReturns the evaluated expression. Read more about the syntax for the expressions on the [Symfony website](http://symfony.com/doc/current/components/expression_language/syntax.html).\nExamples :\n\n    #expr{@age - (@current_year - @first_movie_year)}\n    #expr{(@value / @population) * 100}\n\n### 'filter'\n\nFilter the arguments in order to ouput a result, here are some examples :\n\n    #filter{upperfirst|@word} will output Lorem (if @word = lorem)\n    #filter{round|@value|2} will output 56.23 (if @value = 56.23346)\n    #filter{timestamp|d/m/Y|1470339485} will output 04/08/2016\n    #filter{timestamp|Y} will output 2016\n    #filter{date|2016-08-09|Y-m-d|d/m/Y} will output 09/08/2016\n    #filter{number|@value} will output 564,564 (if @value = 564564)\n    #filter{number|@value|0|,| } will output 564 564 (if @value = 564564)\n\nAvailable filters : round, ceil, floor, max, min, rand, number, lower, upper, lowerfirst, upperfirst, upperwords, trim, substring, replace, timestamp, date. \nFor the filters directly mapped to PHP functions, you can get more information with the PHP documentation.\nCustom filters can easily be added through the FilterFunction::addFilter() method.\n\n### 'loop'\n\nHandle loop on a tag that contains an array of multiple data. Arguments list :\n\n - 1/ The tag that contains an array to loop on\n - 2/ Maximum number of items to loop on ('*' to loop on all elements)\n - 3/ Whether the items should be shuffled or not (true/false)\n - 4/ Separator between each item\n - 5/ Separator for the last item\n - 6/ The template for each item\n\nExample with the tag 'tag_name' that contains the array `[['name' =\u003e 'Bill'], ['name' =\u003e 'Bob'], ['name' =\u003e 'John']]`\n\n    Hello #loop{@tag_name|*|true|, | and |dear @name}.\n    \nIt will output : `Hello dear John, dear Bob and dear Bill.`\n\n### 'random'\n\nReturn randomly one of the arguments\n\nTemplate example :\n\n    #random{first option|second option|third option}\n\nOutput example :\n\n    second option\n\n### 'reprandom'\n\nReturn a reproducable random choice, based on the Mersenne Twister. Using the same `seed` value will lead to the same choice every time.\n\nTemplate example :\n\n    #random{seed|first option|second option|third option}\n\nOutput example :\n\n    second option\n\n### 'prandom'\n\nReturn randomly one of the arguments, taking account of the probability set to each value. In the example below, the first parameter 'one' will have 80% of chance to be output.\n\nTemplate example :\n\n    #prandom{80:first option|10:second option|10:third option}\n\nOutput example :\n\n    first option\n\n### 'shuffle'\n\nReturn the arguments shuffled. The first argument is the separator between each others.\n\nTemplate example :\n\n    #shuffle{ |one|two|three}\n\nOutput example :\n\n    two three one\n\n### 'choose'\n\nReturn the chosen argument among the list. The first argument is the ID of the argument to output (starting from 1).\n\nData :\n\n    [\n        [\n            'my_choice' =\u003e '2',\n        ]\n    ]\n\nTemplate example :\n\n    #choose{1|one|two|three} #choose{@my_choice|one|two|three}\n\nOutput example :\n\n    one two\n\nFor instance, 'choose' function can be used in combination with 'set' function :\n\nTemplate example :\n\n    #set{my_choice|#random{1|2|3}};;\n    Lorem #choose{@my_choice|one|two|three} ipsum #choose{@my_choice|first|second|third}\n\nOutput example :\n\n    Lorem two ipsum second\n\n### 'coalesce'\n\nReturns the first non empty value.\n\nData :\n\n    [\n        [\n            'my_tag1' =\u003e '',\n            'my_tag2' =\u003e '',\n            'my_tag3' =\u003e 'Hello',\n            'my_tag4' =\u003e 'Hi',\n        ]\n    ]\n\nTemplate example :\n\n    #coalesce{@my_tag1|@my_tag2|@my_tag3|@my_tag4}\n\nOutput example :\n\n    Hello\n\n### 'rmna'\n\nReturn the argument only if it does not contain any empty values. It allows to prevent the display of sentences with missing values.\n\nData :\n\n    [\n        [\n            'tag1' =\u003e '', // or null\n            'tag2' =\u003e 'ok', \n        ]\n    ]\n\nTemplate example :\n\n    #rmna{test 1 : @tag1};;\n    #rmna{test 2 : @tag2}\n\nOutput :\n\n    test 2 : ok\n    \n## Complete example :\n\nTemplate :\n\n    #set{@pronoun|#if{@sex == 'm'|He|She}};;\n    @firstname @lastname is an @nationality #if{@sex == 'm'|actor|actress} of @age years old. ;;\n    @pronoun was born in @birthdate in @birth_city (@birth_country). ;;\n    #shuffle{ |;;\n        #random{Throughout|During|All along} #if{sex == 'm'|his|her} career, #random{@pronoun|@lastname} was nominated @nominations_number time#if{@nominations_number \u003e 1|s} for the oscars and has won @awards_number time#if{@awards_number \u003e 1|s}.|;;\n        #if{@awards_number \u003e 1 and (@awards_number / @nominations_number) \u003e= 0.5|@lastname is accustomed to win oscars.}|;;\n        @firstname @lastname first movie, \"@first_movie_name\", was shot in @first_movie_year (at #expr{@age - (#filter{timestamp|Y} - @first_movie_year)} years old).|;;\n        One of #if{@sex == 'm'|his|her} most #random{famous|important|major} #random{film|movie} is @famous_movie_name and has been released in @famous_movie_year. ;;\n            #prandom{20:|80:Indeed, }@famous_movie_name #random{earned|gained|made|obtained} $#filter{number|@famous_movie_earn} #random{worldwide|#random{across|around} the world}. ;;\n            #loop{@other_famous_movies|*|true|, | and |@name (@year)} are some other great movies from @lastname.;;\n    }\n\nData :\n\n    [\n        [\n            'firstname' =\u003e 'Leonardo',\n            'lastname' =\u003e 'DiCaprio',\n            'birthdate' =\u003e 'November 11, 1974',\n            'age' =\u003e 41,\n            'sex' =\u003e 'm',\n            'nationality' =\u003e 'American',\n            'birth_city' =\u003e 'Hollywood',\n            'birth_country' =\u003e 'US',\n            'awards_number' =\u003e '1',\n            'nominations_number' =\u003e '6',\n            'movies_number' =\u003e '37',\n            'first_movie_name' =\u003e 'Critters 3',\n            'first_movie_year' =\u003e '1991',\n            'famous_movie_name' =\u003e 'Titanic',\n            'famous_movie_year' =\u003e '1997',\n            'famous_movie_earn' =\u003e '2185372302',\n            'other_famous_movies' =\u003e [\n                [\n                    'name' =\u003e 'Catch Me If You Can',\n                    'year' =\u003e '2002'\n                ],\n                [\n                    'name' =\u003e 'Shutter Island',\n                    'year' =\u003e '2010'\n                ],\n                [\n                    'name' =\u003e 'Inception',\n                    'year' =\u003e '2010'\n                ],\n            ]\n        ],\n        [\n            'firstname' =\u003e 'Jodie',\n            'lastname' =\u003e 'Foster',\n            'birthdate' =\u003e 'November 19, 1962',\n            'age' =\u003e '51',\n            'sex' =\u003e 'f',\n            'nationality' =\u003e 'American',\n            'birth_city' =\u003e 'Los Angeles',\n            'birth_country' =\u003e 'US',\n            'awards_number' =\u003e '2',\n            'nominations_number' =\u003e '4',\n            'movies_number' =\u003e '75',\n            'first_movie_name' =\u003e 'My Sister Hank',\n            'first_movie_year' =\u003e '1972',\n            'famous_movie_name' =\u003e 'Taxi Driver',\n            'famous_movie_year' =\u003e '1976',\n            'famous_movie_earn' =\u003e '28262574',\n            'other_famous_movies' =\u003e [\n                [\n                    'name' =\u003e 'The Silence of the Lambs',\n                    'year' =\u003e '1991'\n                ],\n                [\n                    'name' =\u003e 'Contact',\n                    'year' =\u003e '' // Empty values are skipped by the parser\n                ],\n                [\n                    'name' =\u003e 'The Accused',\n                    'year' =\u003e '1988'\n                ],\n            ]\n        ],\n    ]\n\nOutput :\n\n\u003e Leonardo DiCaprio is an American actor of 41 years old. He was born in November 11, 1974 in Hollywood (US). One of his most famous film is Titanic and has been released in 1997. Indeed, Titanic obtained $2,185,372,302 around the world. Catch Me If You Can (2002), Inception (2010) and Shutter Island (2010) are some other great movies from DiCaprio. Leonardo DiCaprio first movie, \"Critters 3\", was shot in 1991 (at 16 years old). All along his career, He was nominated 6 times for the oscars and has won 1 time.\n\n\u003e Jodie Foster is an American actress of 51 years old. She was born in November 19, 1962 in Los Angeles (US). Foster is accustomed to win oscars. One of her most important film is Taxi Driver and has been released in 1976. Indeed, Taxi Driver obtained $28,262,574 worldwide. The Accused (1988) and The Silence of the Lambs (1991) are some other great movies from Foster. Jodie Foster first movie, \"My Sister Hank\", was shot in 1972 (at 7 years old). Throughout her career, Foster was nominated 4 times for the oscars and has won 2 times.\n\n## Create a new function\n\nYou can extend the TextGenerator capabilities by adding your own text funtions. In order to create a new function for the TextGenerator, you just have to implement the FunctionInterface and call registerFunction() method on the TextGenerator instance. Then, you will be able to call it from your templates.\n\n## Install\n\n    $ composer require neveldo/text-generator\n\n## Running tests and linters\n\n```\ndocker build -t text-generator .\ndocker run -it --rm -v $(pwd):/app text-generator\n```\n\n```\ncomposer install\nvendor/bin/phpunit --display-deprecations\nvendor/bin/phpstan analyse src tests sample -l 9\nvendor/bin/php-cs-fixer fix src\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneveldo%2Ftextgenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneveldo%2Ftextgenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneveldo%2Ftextgenerator/lists"}