compare text in two attached txt files?

Hello, All,

I was so excited to find the Shiffman videos on split and replace. I am trying to compare two files to yield a list of matching words, then have those words be highlighted within the whole-text display on the screen. I am having no luck, though, and am wondering if this kind of text-file-to-text-file comparison can be done, and if so, whether the replace function can be used to replace characteristics instead of words. So, a blue "lovely" replaces a black "lovely", font-wise, but stays in the same place on the page...

Thoughts, anyone? I would be so grateful.

Answers

  • edited March 2018

    Hi, kfrajer, Thanks for the assist! Based on the example there, I wrote this, which unfortunately isn't working. I've tried fiddling around with the arrangement of blocks (meaning, I am uncertain where the function declaration for "replacer" goes), but no luck yet.

    var butlerV; var huliuneg; var butlerneg;

    function preload(){ butlerV = loadStrings("Odyssey_v_butler.txt"); huliuneg = loadStrings("neg_words_use.txt"); } function setup() { createCanvas(windowWidth, windowHeight); background(179, 157, 109);

    } function draw() { background(179, 157, 109);

    textSize(12);
    fill(0);
    textLeading(10);
    textAlign(CENTER);
    text(butlerV, 75, 75, width-150, height-75); 
    
    butlerv.split(/[,\s/]+/);
    huliuneg.split(/[,\s/]+/);
    butlerneg = match(butlerV, huliuneg);
    if (butlerV.contains(huliuneg)) {
        butlerneg.split(/[,\s/]+/);
        butlerV.replace(butlerneg, replacer(12, BOLD, (255, 0, 0)));
    }
    

    function replacer() { textSize(); textStyle(); fill(); }
    }

  • (I'm so sorry, I cannot get the code to copy into a box correctly!)

  • edited March 2018

    here words from text 2 (adjectives) that appear in text 1 (poem) are highlighted in text 1. You could of course use different colors instead of green

    You wrote:

    whether the replace function can be used to replace characteristics instead of words. So, a blue "lovely" replaces a black "lovely", font-wise, but stays in the same place on the page

    in which way is the data "blue" stored in the word lovely in your example?

    Chrisir

    //===========================================================================
    // FINAL FIELDS:
    final int Y_OFFSET=100;
    
    //===========================================================================
    // GLOBAL VARIABLES:
    
    final int TEXT_SIZE=14;
    
    
    String [] poem = new String[15]; // input text
    String [] poem2 = new String[15]; // input text
    
    boolean isHighlighted=false;
    
    String[] adj = {
      "happy", "rotating", "red", "fast", "elastic", "smiley", "unbelievable", "infinite", "surprising", 
      "mysterious", "glowing", "green", "blue", "tired", "hard", "soft", "transparent", "long", "short", 
      "excellent", "noisy", "silent", "rare", "normal", "typical", "living", "clean", "glamorous", 
    };
    
    //===========================================================================
    // PROCESSING DEFAULT FUNCTIONS:
    
    void settings() {
      size(1500, 600);
    }
    
    void setup() {
    
      // textAlign(CENTER, CENTER);
      rectMode(CENTER);
    
      fill(255);
      strokeWeight(2);
      textSize(TEXT_SIZE);
    
      poem[0]="i love you much glamorous darling ";
      poem[1]="i love you much surprising darling ";
      poem[2]="more than anyone on the earth and i";
      poem[3]="like you better than everything in the sky";
      poem[4]="-sunlight and singing welcome your coming";
      poem[5]="although winter may be everywhere";
      poem[6]="with such a silence and such a darkness";
      poem[7]="noone can quite begin to guess";
      poem[8]="(except my life)the true time of year-";
      poem[9]="and if what calls itself a clean world should have";
      poem[10]="the luck to hear such singing (or glimpse such";
      poem[11]="sunlight as will leap higher than high";
      poem[12]="through gayer than gayest someone's heart at your each";
      poem[13]="nearness) everyone certainly would (my";
      poem[14]="most beautiful darling) believe in nothing but love.";
    
      for (int i=0; i<poem.length; i++) {
        poem2[i] =  poem[i].toString();
      }
    }
    
    void draw() {
      background(0);
    
      showRawData();
    }
    
    //===========================================================================
    // PROCESSING DEFAULT INPUT FUNCTIONS:
    
    void keyPressed() {
      if (key==' ')
      {
        if (isHighlighted) {
          // reset
          isHighlighted=false; 
          for (int i=0; i<poem.length; i++) {
            poem[i] =  poem2[i].toString();
          }
        }
        //
        else {
          // highlight 
          isHighlighted=true; 
          for (int i=0; i<poem.length; i++) {
            for (int i2=0; i2<adj.length; i2++) {
              // is this word adj[i2] in poem ? 
              if (poem[i].contains(adj[i2])) {
                poem[i]=poem[i].replace(adj[i2], "<> "+adj[i2]+" <>");
              }
            }
          }
        }
      }
    }
    
    //===========================================================================
    // OTHER FUNCTIONS:
    
    void showRawData() {
    
      fill(255, 0, 0); // red
      text("changing text", 300, 44); 
    
      fill(255, 0, 0); // red
      String a1="";
      for (int i=0; i<33; i++) 
        a1+="+++ Hit space +++";
      text(a1, 0, height-44); 
    
      String textAdd; 
      boolean insideWord=false; 
    
      fill(255); // white
      for (int i=0; i<poem.length; i++) {
        fill(255); // white
        String[] line1 = split (poem[i], " ");
        textAdd="";
        for (int i2=0; i2<line1.length; i2++) {
          if (line1[i2].trim().equals("<>")) {
            if (insideWord) 
            {
              fill(255); // white
              insideWord=false;
            } else {
              fill(0, 255, 0); // green
              insideWord=true;
            }
          } else {
            text(line1[i2], float(300)+textWidth(textAdd), Y_OFFSET + i*2*TEXT_SIZE);
            textAdd+=line1[i2]+" ";
          }
        }
      }
    
      fill(255, 0, 0); // red
      text("Words that are looked for on the left side", 700, 44);
    
      fill(255); // white
      text(join(adj, " "), 
        900, 555, 
        400, 800);
    }
    //
    
Sign In or Register to comment.