katoba86 / text-generator
TextGenerator是一个通过使用模板来自动化从数据生成文本的工具。
Requires
- php: >=5.5.0
- symfony/expression-language: ^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
README
TextGenerator是一个旨在从数据中自动生成文本的PHP包。欢迎评论和贡献。
除了PHP包外,Chrome网上应用店还提供Google Spreadsheet插件。它允许用户在Google Spreadsheet中直接从数据生成自动化的文本内容。关于Spreadsheet插件的完整教程可在这里找到。
特性
- 从模板生成文本
- 标签替换
- 文本函数(核心函数:random,带有概率的random,shuffle,if,loop,变量赋值等)
- 嵌套函数调用
- 跳过包含空值的部分,以防止生成文本的不一致性
标签
模板中出现的标签会被匹配的值替换。例如
数据
['my_tag' => 'dolor']
模板
Lorem @my_tag ipsum
输出
Lorem dolor ipsum
模板缩进
使用“;;”特殊标记来缩进您的模板。此标记可以插入模板的任何位置,标记后面的任何空格字符(包括制表符和换行符)将被删除。
模板
Quare hoc quidem praeceptum, cuiuscumque est. ;;
ad tollendam amicitiam valet. ;;
potius praecipiendum fuit. ;;
ut eam diligentiam adhiberemus.
输出
Quare hoc quidem praeceptum, cuiuscumque est. ad tollendam amicitiam valet. potius praecipiendum fuit. ut eam diligentiam adhiberemus.
核心函数
'set'
在模板中设置新的标签,以便进一步使用。
数据
[
[
'sex' => 'f',
]
]
模板示例
#set{@intro|how are you ?};;
#set{@who|#if{sex == 'm'|boy|girl}};;
#set{@hello|#random{Hello|Goodbye|Hi}};;
@hello @who @intro
输出示例
Hi girl how are you ?
'if'
根据条件显示文本。第一个参数是要检查的条件。第二个参数在条件为真时返回。第三个可选参数在条件为假时返回。有关条件的语法,请参阅Symfony网站。示例
#if{@val == 5|the value equals 5}
#if{@val == 5|the value equals 5|the value doesn't equal 5}
#if{@val < 5 or @val > 15|the value is lower that 5 or greater that 15|the value is between 5 and 15}
#if{(@val > 5 and @val < 15) or (@val > 10 and @val < 30)|then statement ...|else statement ...}
'expr'
返回评估的表达式。有关表达式的语法,请参阅Symfony网站。示例
#expr{@age - (@current_year - @first_movie_year)}
#expr{(@value / @population) * 100}
'filter'
过滤参数以输出结果,以下是一些示例
#filter{upperfirst|@word} will output Lorem (if @word = lorem)
#filter{round|@value|2} will output 56.23 (if @value = 56.23346)
#filter{timestamp|d/m/Y|1470339485} will output 04/08/2016
#filter{timestamp|Y} will output 2016
#filter{date|2016-08-09|Y-m-d|d/m/Y} will output 09/08/2016
#filter{number|@value} will output 564,564 (if @value = 564564)
#filter{number|@value|0|,| } will output 564 564 (if @value = 564564)
可用过滤器:round,ceil,floor,max,min,rand,number,lower,upper,lowerfirst,upperfirst,upperwords,trim,substring,timestamp,date。对于直接映射到PHP函数的过滤器,您可以在PHP文档中获取更多信息。可以通过FilterFunction::addFilter()方法轻松添加自定义过滤器。
'loop'
处理包含多个数据的标签的循环。参数列表
- 1/ 包含要循环数组的标签
- 2/ 最大循环项数('*'表示循环所有元素)
- 3/ 项目是否应打乱顺序(true/false)
- 4/ 每个项目之间的分隔符
- 5/ 最后一个项目的分隔符
- 6/ 每个项目的模板
示例:假设有一个名为'tag_name'的标签,它包含数组[['name' => 'Bill'], ['name' => 'Bob'], ['name' => 'John']]
Hello #loop{@tag_name|*|true|, | and |dear @name}.
它将输出:Hello dear John, dear Bob and dear Bill.
'random'
随机返回一个参数
模板示例
#random{first option|second option|third option}
输出示例
second option
'prandom'
随机返回一个参数,考虑每个值设置的概率。在下面的示例中,第一个参数'one'将有80%的几率被输出。
模板示例
#prandom{80:first option|10:second option|10:third option}
输出示例
first option
'shuffle'
返回打乱顺序的参数。第一个参数是它们之间的分隔符。
模板示例
#shuffle{ |one|two|three}
输出示例
two three one
'choose'
从列表中返回所选参数。第一个参数是要输出的参数的ID(从1开始)。
数据
[
[
'my_choice' => 2,
]
]
模板示例
#choose{1|one|two|three} #choose{@my_choice|one|two|three}
输出示例
one two
例如,'choose' 函数可以与 'set' 函数结合使用。
模板示例
#set{my_choice|#random{1|2|3}};;
Lorem #choose{@my_choice|one|two|three} ipsum #choose{@my_choice|first|second|third}
输出示例
Lorem two ipsum second
'coalesce'
返回第一个非空值。
数据
[
[
'my_tag1' => '',
'my_tag2' => null,
'my_tag3' => 'Hello',
'my_tag4' => 'Hi',
]
]
模板示例
#coalesce{@my_tag1|@my_tag2|@my_tag3|@my_tag4}
输出示例
Hello
'rmna'
仅返回不包含任何空值的参数。这可以防止显示缺失值的句子。
数据
[
[
'tag1' => '', // or null
'tag2' => 'ok',
]
]
模板示例
#rmna{test 1 : @tag1};;
#rmna{test 2 : @tag2}
输出
test 2 : ok
完整示例
模板
#set{@pronoun|#if{@sex == 'm'|He|She}};;
@firstname @lastname is an @nationality #if{@sex == 'm'|actor|actress} of @age years old. ;;
@pronoun was born in @birthdate in @birth_city (@birth_country). ;;
#shuffle{ |;;
#random{Throughout|During|All along} #if{sex == 'm'|his|her} career, #random{@pronoun|@lastname} was nominated @nominations_number time#if{@nominations_number > 1|s} for the oscars and has won @awards_number time#if{@awards_number > 1|s}.|;;
#if{@awards_number > 1 and (@awards_number / @nominations_number) >= 0.5|@lastname is accustomed to win oscars.}|;;
@firstname @lastname first movie, "@first_movie_name", was shot in @first_movie_year (at #expr{@age - (#filter{timestamp|Y} - @first_movie_year)} years old).|;;
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. ;;
#prandom{20:|80:Indeed, }@famous_movie_name #random{earned|gained|made|obtained} $#filter{number|@famous_movie_earn} #random{worldwide|#random{across|around} the world}. ;;
#loop{@other_famous_movies|*|true|, | and |@name (@year)} are some other great movies from @lastname.;;
}
数据
[
[
'firstname' => 'Leonardo',
'lastname' => 'DiCaprio',
'birthdate' => 'November 11, 1974',
'age' => 41,
'sex' => 'm',
'nationality' => 'American',
'birth_city' => 'Hollywood',
'birth_country' => 'US',
'awards_number' => 1,
'nominations_number' => 6,
'movies_number' => 37,
'first_movie_name' => 'Critters 3',
'first_movie_year' => 1991,
'famous_movie_name' => 'Titanic',
'famous_movie_year' => 1997,
'famous_movie_earn' => '2185372302',
'other_famous_movies' => [
[
'name' => 'Catch Me If You Can',
'year' => 2002
],
[
'name' => 'Shutter Island',
'year' => 2010
],
[
'name' => 'Inception',
'year' => 2010
],
]
],
[
'firstname' => 'Jodie',
'lastname' => 'Foster',
'birthdate' => 'November 19, 1962',
'age' => 51,
'sex' => 'f',
'nationality' => 'American',
'birth_city' => 'Los Angeles',
'birth_country' => 'US',
'awards_number' => 2,
'nominations_number' => 4,
'movies_number' => 75,
'first_movie_name' => 'My Sister Hank',
'first_movie_year' => 1972,
'famous_movie_name' => 'Taxi Driver',
'famous_movie_year' => 1976,
'famous_movie_earn' => '28262574',
'other_famous_movies' => [
[
'name' => 'The Silence of the Lambs',
'year' => 1991
],
[
'name' => 'Contact',
'year' => null // Empty values are skipped by the parser
],
[
'name' => 'The Accused',
'year' => 1988
],
]
],
]
输出
莱昂纳多·迪卡普里奥是一位41岁的美国演员。他于1974年11月11日出生于好莱坞(美国)。他最著名的电影之一是《泰坦尼克号》,该电影于1997年上映。确实,《泰坦尼克号》在全球获得了218.53亿美元。2002年的《偷天陷阱》,2010年的《盗梦空间》和《禁闭岛》都是迪卡普里奥的杰出电影。莱昂纳多·迪卡普里奥的第一部电影《怪物3》于1991年拍摄(当时16岁)。在他的整个职业生涯中,他6次获得奥斯卡提名,并1次获奖。
朱迪·福斯特是一位51岁的美国女演员。她于1962年11月19日出生于洛杉矶(美国)。福斯特习惯于赢得奥斯卡奖。她的一部重要电影是1976年上映的《出租车司机》。确实,《出租车司机》在全球获得了2.82亿美元。1988年的《控方证人》和1991年的《沉默的羔羊》是福斯特的其他杰出电影。朱迪·福斯特的第一部电影《我的妹妹汉克》于1972年拍摄(当时7岁)。在她的整个职业生涯中,福斯特获得了4次奥斯卡提名,并2次获奖。
创建新函数
您可以通过添加自己的文本函数来扩展TextGenerator的功能。为了为TextGenerator创建新函数,您只需要实现FunctionInterface并调用TextGenerator实例上的registerFunction()方法。然后,您就可以从您的模板中调用它。
安装
$ composer require neveldo/text-generator