【问题标题】:Data generation Python数据生成 Python
【发布时间】:2020-07-15 09:12:18
【问题描述】:

我正在尝试基于现有数据集生成数据集,我能够实现一种随机更改文件内容的方法,但我无法将所有这些都写入文件。另外,我还需要将更改的单词数写入文件,因为我想使用这个数据集来训练神经网络,你能帮帮我吗?

输入:每个文件有 2 行文本。

输出:文件有3(可能)行:第一行没有变化,第二行根据方法变化,第三显示变化的单词数(如果对于深度学习任务最好不要这样做,我很乐意提供建议,因为我是初学者)

from random import randrange
import os

Path = "D:\corrected data\\"
filelist = os.listdir(Path)

if __name__ == "__main__":
    new_words = ['consultable', 'partie ', 'celle ', 'également ', 'forte ', 'statistiques ', 'langue ', 
'cadeaux', 'publications ', 'notre', 'nous', 'pour', 'suivr', 'les', 'vos', 'visitez ', 'thème ', 'thème  ', 'thème ', 'produits', 'coulisses ', 'un ', 'atelier ', 'concevoir  ', 'personnalisés  ', 'consultable', 'découvrir ', 'fournit ', 'trace ', 'dire ', 'tableau', 'décrire', 'grande ', 'feuille ', 'noter ', 'correspondant', 'propre',]
    nb_words_to_replace = randrange(10)

    #with open("1.txt") as file:
    for i in filelist:
       # if i.endswith(".txt"):  
            with open(Path + i,"r",encoding="utf-8") as file:
               # for line in file:
                    data = file.readlines()
                    first_line = data[0]
                    second_line = data[1]
                    print(f"Original: {second_line}")
                   # print(f"FIle: {file}")
                    second_line_array = second_line.split(" ")
                    for j in range(nb_words_to_replace):
                        replacement_position = randrange(len(second_line_array))

                        old_word = second_line_array[replacement_position]
                        new_word = new_words[randrange(len(new_words))]
                        print(f"Position {replacement_position} : {old_word} -> {new_word}")

                        second_line_array[replacement_position] = new_word

                    res = " ".join(second_line_array)
                    print(f"Result: {res}")
            with open(Path + i,"w") as f:
                       for line in file:
                          if line == second_line:
                                f.write(res)

【问题讨论】:

  • 你是因为训练数据少吗?你想实现过采样吗?
  • @AhmedSunny 我只需要一个不同的数据集,我有一个包含文本(2 行)及其简要说明(1 行)的数据集,但我反过来随机替换单词“破坏”数据以训练神经网络评估句子的结构(评估这句话是否有意义)。

标签: python database recurrent-neural-network


【解决方案1】:

简而言之,您有两个问题:

  • 如何正确替换文件的第 2 行(和第 3 行)。
  • 如何跟踪更改的字数。

如何正确替换文件的第 2 行(和第 3 行)。

您的代码:

with open(Path + i,"w") as f:
   for line in file:
      if line == second_line:
      f.write(res)

阅读未启用。 for line in file 不起作用。 f 已定义,但使用 file 代替。要解决此问题,请改为执行以下操作:

with open(Path + i,"r+") as file:
   lines = file.read().splitlines()    # splitlines() removes the \n characters
   lines[1] = second_line
   file.writelines(lines)

但是,您想向其中添加更多行。我建议您以不同的方式构建逻辑。


如何跟踪更改的字数。

添加变量changed_words_count并在old_word != new_word时增加它


结果代码:

for i in filelist:
    filepath = Path + i

    # The lines that will be replacing the file
    new_lines = [""] * 3
    
    with open(filepath, "r", encoding="utf-8") as file:
        data = file.readlines()
        first_line = data[0]
        second_line = data[1]
        
        second_line_array = second_line.split(" ")

        changed_words_count = 0
        for j in range(nb_words_to_replace):
            replacement_position = randrange(len(second_line_array))

            old_word = second_line_array[replacement_position]
            new_word = new_words[randrange(len(new_words))]

            # A word replaced does not mean the word has changed.
            # It could be replacing itself.
            # Check if the replacing word is different
            if old_word != new_word:
                changed_words_count += 1
            
            second_line_array[replacement_position] = new_word
        
        # Add the lines to the new file lines
        new_lines[0] = first_line
        new_lines[1] = " ".join(second_line_array)
        new_lines[2] = str(changed_words_count)
        
        print(f"Result: {new_lines[1]}")
    
    with open(filepath, "w") as file:
        file.writelines(new_lines)

注意:代码未经测试。

【讨论】:

    猜你喜欢
    • 2018-10-12
    • 2011-06-22
    • 2018-04-17
    • 1970-01-01
    • 2013-04-08
    • 2014-04-08
    • 2022-01-14
    • 2020-05-21
    • 2018-09-20
    相关资源
    最近更新 更多