We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Wordle visualization
Page Index Toggle Pages: 1
Wordle visualization (Read 1733 times)
Wordle visualization
Aug 11th, 2009, 9:18am
 
Hi

I've never worked in Processing before, but I'm familiar with Java. I'm writing a Tag Cloud.

Part of my code:
Code:
String[] listOfWords={"horror","thriller","comedy","drama","action"};
int[] numberOfwords ={1,7,3,12,1};

void draw()
{
 for(int i = 0; i < numberOfwords.length; i++)
 {
    int totalInt = numberOfwords[i];
    if(totalInt <= 2)
    {
       fill(204);
       textFont(fontA, 10);
       text(listOfWords[i], 10, 110);
    }
    else if(totalInt > 2 && totalInt < 7)
    {
       fill(51);
       textFont(fontA, 20);
       text(listOfWords[i], 10, 70);
    }
    else
    {
       fill(0);
       textFont(fontA, 30);
       text(listOfWords[i], 10, 30);
    }
  }
}


How can I change the text() in order to display the words next to each other, because at the moment the same size words are placed on top of each other? I also want all the words to be displayed on one line, and to run onto the next line when that line is full.

i.e. I want the output to be: drama thriller comedy action horror

Any help will be appreciated.

Thanks
Re: Wordle visualization
Reply #1 - Aug 11th, 2009, 9:52am
 
2nd and 3rd parameters in text() call are x and y pos. you need to make these parameters and use them to hold where you are.

you can get the width of the text using, er, textWidth:

http://processing.org/reference/textWidth_.html
Re: Wordle visualization
Reply #2 - Aug 11th, 2009, 10:32am
 
Thank you for your reply.

I changed my code to:
Code:
int totalInt = numberOfwords[id];
    if(totalInt <= 2)  //small
    {
       fill(204);
       textFont(font, 10);
       text(listOfWords[id], int(x - textWidth(listOfWords[id])/2), int(y + 50));
       x += textWidth(listOfWords[id]);
       y += textWidth(listOfWords[id]);
    }
    else if(totalInt > 2 && totalInt < 7)  //medium
    {
       fill(51);
       textFont(font, 20);
       text(listOfWords[id], int(x - textWidth(listOfWords[id])/3), int(y + 30));
       x += textWidth(listOfWords[id]);
       y += textWidth(listOfWords[id]);
    }
    else  //large
    {
       fill(0);
       textFont(font, 30);
       text(listOfWords[id], int(x - textWidth(listOfWords[id])/4), int(y + 10));
       x += textWidth(listOfWords[id]);
       y += textWidth(listOfWords[id]);
    }


but the words are still placed on top of each other. What am I doing wrong?

Thanks
Re: Wordle visualization
Reply #3 - Aug 11th, 2009, 1:59pm
 
it's gotta be the x and y position in te text calls.

add some printlns of the values and see if there's anything wrong with them

i don't get the int(x - textWidth(listOfWords[id])/2) part. you're adding the width to x each time, this should be enough.

i also don't get why you're adding textWidth to y (the vertical coordinate)
Re: Wordle visualization
Reply #4 - Aug 11th, 2009, 8:34pm
 
Hi

When I change the code to:
Code:
int totalInt = numberOfwords[id];
    if(totalInt <= 2)  //small
    {
       fill(204);
       textFont(font, 10);
       text(listOfWords[id], int(x), int(y + 50));
       println(x);
       x = x + textWidth(listOfWords[id]);
       println(x);
    }


the output is:
0.0
30.624998
0.0
28.333332


Why does it change back to 0.0 the second time it goes through? I haven't declared x to be 0.


Thanks
Re: Wordle visualization
Reply #5 - Aug 12th, 2009, 1:48am
 
we can't tell because we can't see all your code.

where are you initialising x? what is its scope? it needs to be global, outside both setup() and draw().
Re: Wordle visualization
Reply #6 - Aug 12th, 2009, 8:43am
 
The words are displaying correct now, thank you. I declared x global, and static.

I've only got one more question.
I'm sorting the words according to their frequency - that's working.
If the words have the same value (frequency) they are supposed to be sorted alphabetically. I'm using the sort(), but something isn't working because I'm getting a NullPointerException.

My code:
Code:
for(int i = 0; i < arrayLength; i++)
       {
           for(int b = 0; b < arrayLength; b++)
           {
               if(numberOfwords[b] < numberOfwords[i])
               {
                   temp = numberOfwords[b];
                   numberOfwords[b] = numberOfwords[i];
                   numberOfwords[i] = temp;
                   
                   tempString = listOfWords[b];
                   listOfWords[b] = listOfWords[i];
                   listOfWords[i] = tempString;
               }
               else if(numberOfwords[b] == numberOfwords[i])
               {
                   listOfWords = sort(listOfWords);
               }
           }
       }
       println(Arrays.toString(numberOfwords) + "\n" + Arrays.toString(listOfWords));


Is there something wrong with my sort()?
Can I say? if(numberOfwords[b].equals(numberOfwords[i]))

Thanks
Re: Wordle visualization
Reply #7 - Aug 12th, 2009, 9:52am
 
we can't tell because we can't see all your code.

seriously, it's a lot easier to debug when we can see *and run* the whole thing.

instead you give us an uncommented slice of code and tell us there's a null pointer exception in it WITHOUT TELLING US WHERE.

the nested loop looks unnecessary. i really can't think what you're trying to do there. add printlns to the inner loop so you can see what it's doing (which will be a lot more than you expect / require)

having different arrays for words and frequencies is a bad idea, you end up with two lists to maintain the order of, use a class containing both instead.

and that way you can write a comparator that would sort on frequency and then alphabetically, do it all in one go.

(sorry, am in a bad mood. but people really do need to make it easier for other people to help them)
Re: Wordle visualization
Reply #8 - Aug 12th, 2009, 1:29pm
 
ah, ok, i realised later (whilst doing the washing up) what the sort was doing (is a bubble sort, almost). but i still think it's doing too much work.

the call to sort() does look a bit odd - the rest of the loop is concerned with ordering element i and b, the sort() will sort the entire array.

but that doesn't explain the NPException

(which could actually be in the println() - always annoying)
Page Index Toggle Pages: 1