neveldo / text-generator
TextGenerator 是一个通过使用模板自动从数据生成文本的工具。
Requires
- php: >=5.5.0
- symfony/expression-language: ^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
README
TextGenerator 是一个PHP包,旨在从数据生成自动化文本。欢迎评论和贡献。
特性
- 从模板生成文本
- 标签替换
- 文本函数(核心函数:随机、随机带概率、洗牌、if、循环、变量赋值等)
- 嵌套函数调用
- 跳过包含空值的部分,以防止生成文本的不一致性
标签
模板中出现的标签将被匹配的值替换。例如
数据
['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(子串)、replace(替换)、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.5亿美元。其他一些迪卡普里奥的优秀电影还包括《盗梦空间》(2010年)和《禁闭岛》(2010年)。迪卡普里奥的第一部电影《怪物3》是在1991年(16岁时)拍摄的。在他的整个职业生涯中,他6次被提名为奥斯卡奖,并赢得了一次。
朱迪·福斯特是一位51岁的美国女演员。她于1962年11月19日出生于美国洛杉矶。福斯特习惯于赢得奥斯卡奖。她最重要的电影之一是1976年上映的《出租车司机》。实际上,《出租车司机》在全球获得了2.82亿美元。其他一些福斯特的优秀电影还包括《被告》(1988年)和《沉默的羔羊》(1991年)。福斯特的第一部电影《我的姐妹汉克》是在1972年(7岁时)拍摄的。在她的整个职业生涯中,福斯特6次被提名为奥斯卡奖,并赢得两次。
创建一个新的函数
您可以通过添加自己的文本函数来扩展TextGenerator的功能。为了为TextGenerator创建一个新的函数,您只需实现FunctionInterface接口,并在TextGenerator实例上调用registerFunction()方法。然后,您就可以从模板中调用它。
安装
$ composer require neveldo/text-generator