我怎样才能用随机但合理的英文单词创建一个大文件?

How can I create a large file with random but sensible English words?

我想用一个非常大的文件(超过 1GB)测试我的基于 MapReduce 框架的 wordcount 软件,但我不知道如何生成它。

有没有什么工具可以用随机但合理的英文句子创建一个大文件? 谢谢

一个简单的python脚本就可以创建一个Pseudo-random的word文档。我有一年前为一项任务写的那个:

import random

file1 = open("test.txt","a") 
PsudoRandomWords = ["Apple ", "Banana ", "Tree ", "Pickle ", "Toothpick ", "Coffee ", "Done "]

index = 0
#Increase the range to make a bigger file
for x in range(150000000):
   #Change end range of the randint function below if you add more words
   index = random.randint(0,6)
   file1.write(PsudoRandomWords[index])
   if x % 20 == 0:
      file1.write('\n')`

只需向列表中添加更多单词,使其更加随机并增加随机函数的索引即可。我刚刚对其进行了测试,它应该会创建一个名为 test.txt 的文档,大小恰好为 1 GB。这将以随机顺序包含列表中的单词,每 20 个单词由一个新行分隔。

我写了这个简单的 Python 脚本,它在 Project Gutenberg 网站上抓取并写入文本(编码:us-ascii,如果你想使用其他人,请参阅 http://www.gutenberg.org/files/) in a local file text. This script can be used in combination with https://github.com/c-w/gutenberg 以进行更准确的过滤(按语言、作者等)

from __future__ import print_function

import requests
import sys

if (len(sys.argv)!=2):
        print("[---------- ERROR ----------] Usage: scraper <number_of_files>", file=sys.stderr)
        sys.exit(1)

number_of_files=int(sys.argv[1])
text_file=open("big_file.txt",'w+')

for i in range(number_of_files):
    url='http://www.gutenberg.org/files/'+str(i)+'/'+str(i)+'.txt'
    resp=requests.get(url)
    if resp.status_code!=200:
        print("[X] resp.status_code =",resp.status_code,"for",url)
        continue
    print("[V] resp.status_code = 200 for",url)
    try:    
        content=resp.text

        #dummy cleaning of the text 
        splitted_content=content.split("*** START OF THIS PROJECT GUTENBERG EBOOK")
        splitted_content=splitted_content[1].split("*** END OF THIS PROJECT GUTENBERG EBOOK")
        print(splitted_content[0], file = text_file)
    except: 
        continue

text_file.close()