Randomize lines of two text files in the same order separately



  • The python-script plugin here, for notepad++ allows randomizing lines but for a singletext file. However, am trying to randomize two text files with the same number of lines (over hundred lines each) at the same time so that the same order of randomization is maintained.

    That is if line 2 in file 1 and file 2 are house and blue respectively as shown below,

    file 1;
    ball
    house
    cow
    vehicle
    mass

    file 2;
    red
    blue
    fat
    bus
    kilogram

    After randomizing, if line 3 of file 1 is house, line 3 in file 2 must also be blue (maintaining the connection after randomizing) as shown below

    file 1;
    vehicle
    cow
    house
    mass
    ball

    file 2;
    bus
    fat
    blue
    kilogram
    red

    Again the two files are separate. I could combine them and randomize a single text file after then I separate them but this will involve a whole lot of steps. The challenge is having the two text files separate and orderly randomized. Is there a way to achieve this? Thanks in advance.



  • @Gideon-Addai
    You are right that it could be done by combining the 2 files, then using the random script and then tearing the 2 files apart, and yes it would take several steps. None of the steps are particularly onerous, however. It just depends on how you want to approach it. As an example I’d go with the following steps:

    1. for each file, put the line number at the start of the line, might be best to have an a suffix for first file and b for the second.
    2. combine both files and sort numerically, so now each pair of lines are together a first, b second.
    3. combine the 2 lines together.
    4. Run the Pythonscript.
    5. divide the lines and put a 1 in front of the b suffix lines.
    6. sort numerically again, so the b suffix lines go after the a suffix lines.
    7. divide the text back into their own files and remove the numbers and suffix.

    Those steps are somewhat simplified, but should work.

    The alternative is to ‘adjust’ the Pythonscript to cater for multiple files. Most of the hard work is done. I would suggest (without looking too closely at the code) that it would be achievable even if you aren’t heavily into Pythonscript. It depends on whether you want to get your feet wet learning something new?

    Terry



  • @Gideon-Addai ,

    I haven’t clicked that link, because it’s a domain I don’t recognize and a good way to infect my computer is to click on random links found in forums.

    Assuming there’s really a script there that does what you say, then that script shows how to access the contents of an open file using the PythonScript plugin inside Notepad++. The rest is just a change of algorithm and is really a Python coding question, not a “how to use Notepad++” or even “how to use Notepad++'s PythonScript plugin to access the notepad+±specific aspects of a script”

    If you had given me something safe to start from (preferably with trying to make the changes yourself first, before asking for free code-writing service our help in converting code from doing function X to doing function Y), I would have edited what you gave. But since I won’t go to an unknown site, I would have to do too much framework for what I have time for right now.

    As a consolation prize, here is the algorithm I would use (and it’s the algorithm that’s often the hardest thing to come up with):

    1. using the editor1 and editor2 objects in PythonScript (I assume the example used the editor object, which is equivalent to whichever of those two was active; the two objects I listed would be used if file1 was in editor1 and file2 in editor2), read each file row by row, into separate arrays (file_array1 and file_array2, for example)
    2. if length(file_array1) <> length(file_array2), do error processing (either pick the shorter length, or bomb out with an error for the user to correct the data)
    3. generate a range of indexes the right size for file_array1 and file_array2 (if each has 100 lines, it would be 0…99) – range(0, length(file_array1)) or similar (syntax from memory, untested)
    4. shuffle (randomize) that range – I assume your link did a shuffle similar to this, so you can look at how that code did it
    5. foreach index idx in the shuffled range, grab the idxth element from each of the arrays, and write them to that appropriate line in the original files

    Sorry I couldn’t be more help for now… Maybe later today, I’ll find a Round Tuit to implement that code.



  • @PeterJones
    The link was OK, I did check myself. Finding the code on github was a bit more effort, but here it is.

    # RandomizeLines.py for Notepad++ Python Scripting plugin
    # https://github.com/ethanpil/npp-randomizelines
    #
    # A Notepad++ Python Scriping Plugin to randomize lines in the editor
    #
    # Version 1.0
    #
    ## Installation
    #  1. Copy **RandomizeLines.py** to **\plugins\PythonScript\scripts\** in your NPP folder
    #  2. Restart NPP. It will now appear as a menu item under Plugins...Python Script...Scripts
    #
    ## Usage
    #  1. Select the lines to randomize, or select nothing.
    #  2. Go to the NPP menu, Plugins...Python Script...Scripts...RandomizeLines and click!
    #  3. If selected text is detected, it reorders the lines and replaces the selected text, otherwise the entire contents of current document.
    #  4. Undo is available if you dont like the results
    #
    ## Notes
    # The plugin attempts to detect your EOL setting and replicate it when replacing with the randomized lines
    #
    
    import random
    
    #Preserve Undo Capability
    editor.beginUndoAction()
    
    #Get any selected text
    seltext = editor.getSelText()
    seltextlen = len(seltext)
    
    #Preserve EOL Mode when we send back the reordered lines
    EOLMode = editor.getEOLMode()
    
    if EOLMode==0:
      EOLchar = '\r\n'
    elif EOLMode==1:
    	EOLchar = '\r'
    elif EOLMode==2:
        EOLchar = '\n'
    
    
    if seltextlen >= 1 :
    	rawtext = str(seltext)
    	rawtextlines = rawtext.splitlines()
    	random.shuffle(rawtextlines)
    	randomizedtext = EOLchar.join(rawtextlines)
    	editor.replaceSel(randomizedtext)
    else:
    	rawtext = str(editor.getText())
    	rawtextlines = rawtext.splitlines()
    	random.shuffle(rawtextlines)
    	randomizedtext = EOLchar.join(rawtextlines)
    	editor.setText(randomizedtext)	
    

    PS, I should thank Ethan Pil (https://github.com/ethanpil) for the use of his code.
    Terry



  • @PeterJones

    Wow, Peter…perfect reply. Well, at least until you continued with the “As a consolation prize…” and further on. :)


Log in to reply