【问题标题】:Generating random URL with real words用真实单词生成随机 URL
【发布时间】:2016-05-05 12:31:50
【问题描述】:

我编写了一个小脚本,它会生成一个随机 URL,它可以工作,但是我希望生成的 URL 有点可信,这意味着我希望它生成真实的单词。截至目前,它会生成 7 个随机字符和数字。

def generate_url(length=7)
  protocall = %w(https:// http://)
  body = rand(36**length).to_s(36)
  num = rand(1..999).to_s
  url = "#{protocall.sample.to_s}#{body}.com/php?id=#{num}"
  puts url
end

#<= http://s857yi5.com/php?id=168
#<= https://6rm0oq3.com/php?id=106
#<= http://skhvk1n.com/php?id=306

所以我正在寻找一种更简单的方法,用真实的单词代替随机的 7 个字符串(请保持在 7 到 10 个字符之间)而不使用外部 gem

我的操作系统是 Windows 7

【问题讨论】:

  • 您可以提供一个短词列表并将其中几个组合成所需的字符串。
  • 如果没有真实单词列表,您无法以编程方式生成“真实”单词,请使用列表并随机选择一些
  • 那么,什么算法产生“KITTEN”?
  • cat 是一个有效的词,而 ykw 不是。如果不使用字典,算法应该如何知道哪些是有效的单词,哪些是无效的。当您仍然需要使用字典时,只需粉红色一个随机单词......
  • 一个词(当被人类阅读时)不是字母的随机集合,字母的顺序决定了它的含义,你无法计算这个顺序 - 它不是基于任何逻辑规则集(问语言学家!)

标签: ruby url random


【解决方案1】:

免责声明:此答案面向在unix 系统中寻求解决此问题的开发人员。然而这并不 为非 Unix 系统解决这个问题。


您可以使用rubysystem calls 来执行此操作。 Unix 系统内置有从文件中抓取随机行的命令。

好消息,unix 系统在usr/share/dict/words 也有完整的英语词典。所以,我会用红宝石做

`shuf -n 1 /usr/share/dict/words`.chomp
=> "dastardly"

注意:这里我使用backtick 作为system 调用。和shuf command get you random line from a file

所以网址是

random_word = `shuf -n 1 /usr/share/dict/words`.chomp
url = "#{random_word}#{body}.com/php?id=#{num}"
=> "wrongfullythisis_body_part.com/php?id=123"

【讨论】:

  • 他在 Windows7 上工作我不确定他是否能够使用这些命令/文件,他必须复制字典并使用 ruby​​/powershell 命令执行某些操作。
【解决方案2】:

Try faker 是一个很酷的 gem,可以生成单词、电子邮件、网址或任何你需要的东西

https://github.com/stympy/faker

我用过很多项目。

hb@hora ~ » irb
2.2.3 :001 > require 'faker'
 => true 
2.2.3 :002 > Faker::Lorem.sentence(3)
 => "Ea esse ex." 
2.2.3 :003 > Faker::Lorem.sentence(3)
 => "Fugiat odio harum." 
2.2.3 :004 > Faker::Lorem.words
 => ["consequuntur", "labore", "optio"] 
2.2.3 :005 > Faker::Lorem.word
 => "error" 
2.2.3 :006 > 

但如果您无法添加外部 gem,您可以创建自己的数组/字典

2.2.3 :013 > dict
 => ["Editors", "and", "critics", "of", "the", "plays", "disdaining", "the", "showiness", "and", "melodrama", "of", "Shakespearean", "stage", "representation", "began", "to", "focus", "on", "Shakespeare", "as", "a", "dramatic", "poet", "to", "be", "studied", "on", "the", "printed", "page", "rather", "than", "in", "the", "theatre", "The", "rift", "between", "Shakespeare", "on", "the", "stage", "and", "Shakespeare", "on", "the", "page", "was", "at", "its", "widest", "in", "the", "early", "19th", "century", "at", "a", "time", "when", "both", "forms", "of", "Shakespeare", "were", "hitting", "peaks", "of", "fame", "and", "popularity", "theatrical"] 
2.2.3 :014 > dict.sample
 => "the" 
2.2.3 :015 > dict.sample
 => "a" 
2.2.3 :016 > dict.sample
 => "disdaining" 
2.2.3 :017 > dict.sample
 => "century" 
2.2.3 :018 > 

该词典是从维基百科的文本复制粘贴到我自己的 irb 然后扫描所有 /w+/

2.2.3 :023 > dict='n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language.'
 => "n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language." 
2.2.3 :024 > dict.scan(/\w+/)
 => ["n", "his", "own", "time", "William", "Shakespeare", "1564", "1616", "was", "rated", "as", "merely", "one", "among", "many", "talented", "playwrights", "and", "poets", "but", "since", "the", "late", "17th", "century", "he", "has", "been", "considered", "the", "supreme", "playwright", "and", "poet", "of", "the", "English", "language"]

【讨论】:

  • without using an external gem
  • 所以您可以创建一个数组(从您自己的字典中)并执行类似 ["word1","word2","word3"].sample 之类的操作,然后您可以从 Internet @987654322 复制字典@ 或者只是从 linux 文件中复制。
  • 我已经改进了我的答案。
【解决方案3】:

您可以通过交替元音和辅音来生成可发音但无意义的单词:tifa zakohu ayanipico wis kicevepys ijoxar uhiq ilay og luh tanise rijux tejod kuyasoq zov wu

【讨论】:

    【解决方案4】:

    您可以在 unix 系统中使用字典中任意数量的单词。您通常可以在路径 /usr/share/dict/words 找到它

    【讨论】:

    【解决方案5】:

    我找到了另一种使用字典列表的方法,如果您使用的是 Windows 并且可以访问 Outlook,您可以使用 Outlooks default.DIC 文件作为单词列表,这将为您提供大量单词,您所要做的就是是将其复制到程序中。参考是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-26
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多