如何使用Faker生成标题和真实英文文本

How to use Faker to generate titles and real English texts

我的数据库中有一个名为 posts 的 table,每条记录代表 post,其中 titleurlbody .

是否可以使用Faker生成英文标题和正文? body 应该是实际的英文文本。

我浏览了文档,但没有找到我要找的东西。

对于英文文本,你为什么不能这样做?

Faker::Quote.matz
=> "I believe that the purpose of life is, at least in part, to be happy. Based on this belief, Ruby is designed to make programming not only easy but also fun. It allows you to concentrate on the creative side of programming, with less stress."

或尝试:

Faker::Hipster.sentences.sample
=> "Cornhole drinking actually pop-up brooklyn williamsburg wayfarers."

对于标题,只需执行

[Faker::Company.name,  Faker::Company.industry].join(' - ')
=> "Schmitt-Kohler - E-Learning"

但如果你真的需要一些随机的实际句子,你可以从随机的书本文本中提取它们并使用 this gem 轻松抓取句子。

require "tactful_tokenizer"
t = TactfulTokenizer::Model.new
string = `curl http://www.textfiles.com/etext/FICTION/alger-cast-544.txt`;
sentences = t.tokenize_text(string);
sentences.count
=> 6322
sentences.sample

"I'm just staying at a place on Fourteenth Street, but I can't
afford to stay there long, for they charge a dollar a day."