Text Parsing Help

edited January 2016 in How To...

I'm working on a project for college that I could use a little bit of help with. Essentially, I want letters to correspond to different musical notes.

I need processing to recognize an input of letters as variables and to take action based on which variable as it reads through them.

Can anyone give any insight on how to assign variables to simple letters input as a string or an array or something?

Thanks

Tagged:

Answers

  • Not really, but thank you. Let me explain a little better...

    I want to have an input of text placed in the program, for example "Hello"

    I'm looking to have the program basically read the input text, recognize that there are 5 letters, being H-E-L-L-O, and cause an action based on which letter it is.

    For example, changing background color might be the variable. H is orange, e is yellow, l is green, and o is black. I want it to read through the inputed text and act upon it.

    I can't figure out how to get processing to recognize the inputed text as variables

  • edited July 2014

    Perhaps what you're looking for is invoke a different method for a given letter.
    There's an undocumented function called method("").
    It invokes a method w/ the same name as the passed String argument.

    Here's an example which uses method(""):
    http://studio.processingtogether.com/sp/pad/export/ro.9eDRvB4LRmLrr/latest

    But you gotta run it in Java Mode instead and uncomment the "Java only" line and comment out the "For JS".

  • Thanks I'll look into it and give it a shot! Appreciate the help

  • Can you explain this a little better to me? Sorry I'm relatively new with the program.

    I just want processing to recognize the string of text as predefined variables

  • It's in the links i pointed...

    String s = "";
    
    
    
    void draw(){
        text(s, 40,40);
        }
    
    void keyPressed(){
        if(key != CODED){
        s+=str(key);
         }
        }
    
  • That coding just lets you type and have it appear on screen. I want the text to already be placed in the code and have it run through. Something like this:

    String[] message = {"hello", "thank", "you"};
    int index = int(0,2);
    
    
    void setup()
    {  size (640, 480); 
    }   
    
     void draw() {
          background(78, 93, 75);
          image(img, 0,0); 
          frameRate(.5); 
           int s = second();
           int a = (s % 2);
          if (a == 0) {
            index = int(message.length); 
    }
    
    String[] pieces = splitTokens(message[index]);
    

    I want to use that splitTokens type of coding to basically split the letters in the string, then when it gets to each letter to perform a task assigned to that letter

  • I just want Processing to recognize the string of text as predefined variables.

    In Java Mode, we can't declare variables nor define functions on-the-fly!
    They gotta be typed in as source code and then compiled!

    However, Java has an advanced feature called reflection.
    The aforementioned method("") function used in my online example is an easy shortcut to accomplish that.

    Let's say you wanna invoke a method called E(). Then you issue method("E"); in order to call that!
    Of course, method E() gotta pre-exist within the source code!
    I believe you're gonna need 26*2 methods. 1 for each alphabet letter including lower & upper cases!

  • Answer ✓

    First task, iterate on each letter of your messages:

    String[] messages = 
    {
      "hello", "thank", "you"
    };
    final int RATE = 20;
    int idxMessage;
    int idxLetter;
    
    void setup()
    {  
      size(640, 480);
    }  
    
    void draw() 
    {
      if (frameCount % RATE != 0)
        return;
      background(78, 93, 75);
      textSize(12);
      text(idxMessage + " " + idxLetter, 10, 20);
      if (idxLetter < messages[idxMessage].length())
      {
        char letter = messages[idxMessage].charAt(idxLetter++);
        textSize(50);
        text(letter, 10, 100);
      }
      else
      {
        idxMessage++;
        idxLetter = 0;
        if (idxMessage >= messages.length)
          exit();
      }
    }
    
  • Answer ✓

    And an example of making various parameters depending on the current letter:

    String[] messages = 
    {
      "hello", "thank", "you", "supercalifragilisticexpialidocious", "my name is bond, james bond"
    };
    final int RATE = 20;
    int idxMessage;
    int idxLetter;
    
    void setup()
    {  
      size(640, 480);
      ellipseMode(CORNER);
    }  
    
    void draw() 
    {
      if (frameCount % RATE != 0)
        return;
      background(78, 93, 75);
      textSize(12);
      text(idxMessage + " " + idxLetter, 10, 20);
      if (idxLetter < messages[idxMessage].length())
      {
        char letter = messages[idxMessage].charAt(idxLetter++);
        textSize(50);
        text(letter, 10, 100);
        processLetter(letter);
      }
      else
      {
        idxMessage++;
        idxLetter = 0;
        if (idxMessage >= messages.length)
          exit();
      }
    }
    
    void processLetter(char c)
    {
      if (c >= 'a' && c < 'f')
      {
        fill(#FF0000);
      }
      else if (c >= 'f' && c < 'k')
      {
        fill(#FFFF00);
      }
      else if (c >= 'k' && c < 'p')
      {
        fill(#00FF00);
      }
      else if (c >= 'p' && c < 'u')
      {
        fill(#00FFFF);
      }
      else if (c >= 'u' && c < 'z')
      {
        fill(#0000FF);
      }
    
      if (c >= 'a' && c < 'h')
      {
        rect(300, 100, 100, 100);
      }
      else if (c >= 'h' && c < 'o')
      {
        ellipse(300, 100, 100, 100);
      }
      else if (c >= 'o' && c < 'z')
      {
        triangle(300, 100, 350, 300, 400, 100);
      }
    }
    
  • edited July 2014

    A more streamlined example using an interface to wrap up a custom method called script().
    Each anonymous instance of it is assigned to a letter-indexed array slot.
    Therefore it's much closer to what you're looking for. That is, individual letter variables: *-:)

    // forum.processing.org/two/discussion/6549/text-parsing-help
    
    interface CustomMethod {
      void script();
    }
    
    static final int LEN = 'z' + 1;
    final CustomMethod[] methods = new CustomMethod[LEN];
    
    static final String[] WORDS = {
      "abc", "def", "ace", "bdf", "abcdef"
    };
    
    void setup() {
      createMethods();
    
      for (String w: WORDS) {
        print("\n\n" + w + ": \t");
    
        for (char c: w.toCharArray())
          if (isWithinCharIndexRange(c)) {
            methods[c].script();
            print(" - ");
          }
      }
    
      exit();
    }
    
    static final boolean isWithinCharIndexRange(char c) {
      return c>='A' & c<='Z' | c>='a' & c<='z';
    }
    
    void createMethods() {
      methods['A'] = methods['a'] = new CustomMethod() {
        @ Override public void script() {
          print("apple");
        }
      };
    
      methods['B'] = methods['b'] = new CustomMethod() {
        @ Override public void script() {
          print("banana");
        }
      };
    
      methods['C'] = methods['c'] = new CustomMethod() {
        @ Override public void script() {
          print("cashew");
        }
      };
    
      methods['D'] = methods['d'] = new CustomMethod() {
        @ Override public void script() {
          print("date");
        }
      };
    
      methods['E'] = methods['e'] = new CustomMethod() {
        @ Override public void script() {
          print("elderberry");
        }
      };
    
      methods['F'] = methods['f'] = new CustomMethod() {
        @ Override public void script() {
          print("fig");
        }
      };
    }
    
Sign In or Register to comment.