【问题标题】:How can I create a large file with random but sensible English words?如何创建一个包含随机但合理的英文单词的大文件?
【发布时间】:2019-12-01 14:05:06
【问题描述】:

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

是否有任何工具可以创建包含随机但合理的英语句子的大文件? 谢谢

【问题讨论】:

  • 古腾堡计划? gutenberg.org
  • 谢谢,但我发现只有小型电子书,大的或多或少 1MB。我需要一个 1GB 以上的文件

标签: bigdata large-data large-files large-data-volumes


【解决方案1】:

一个简单的 python 脚本可以创建一个伪随机的单词文档。我有一个我在一年前写的一个任务:

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 个单词用一个新行分隔。

【讨论】:

    【解决方案2】:

    我编写了这个简单的 Python 脚本,它在 Project Gutenberg 网站上抓取并在本地文件文本中写入文本(编码:us-ascii,如果您想使用其他人请参阅http://www.gutenberg.org/files/)。此脚本可与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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      相关资源
      最近更新 更多