处理中的 SplitTokens 问题

SplitTokens issue in Processing

下面是一段简短的代码,可以计算字数等等,但我遇到了一个奇怪的问题:

当我运行它原来的字符串中的某些单词没有出现在屏幕上!?怎么会?

我是不是忘记了一些使用 splitTokens 的东西?!非常感谢您的提前帮助。

祝一切顺利,

L

IntDict counts;
String[] tokens;

void setup() {
    size(1000, 1000);
    background(0);
    counts = new IntDict();
    String[] lines = {
        "If only you were paying a bit attention to me sometimes.",
        "I am not just a care giver I also need soemone to hug me tenderly."
    };
    String alltext = join(lines, " ");
    tokens = splitTokens(alltext, "\n\";.?!'():\n ");

    for (int i = 0; i < tokens.length; i++) {
        String word = tokens[i].toLowerCase();

        if (counts.hasKey(word)) {
            counts.increment(word);
        } else {
            counts.set(word, 1);
        }
        println(word);
    }

    String[] keys = counts.keyArray();

    for (int i = 0; i < keys.length; i++) {
        textSize(20);
        float x = 50;
        float y = 50 + 15 * i;
        text(keys[i], x, y);
    }
}

void draw() {}

您在屏幕上呈现的不是拆分 tokens,它只是独特的单词(counts' 键)。

如果您调用 printArray(tokens);,您会在控制台中看到出现的每个单词,包括重复的单词:

IntDict counts;
String [] tokens;

void setup() {
  size(1000, 1000);
  background(0);
  counts = new IntDict();
  String [] lines = {"If only you were paying a bit attention to me sometimes.",
    "I am not just a care giver I also need soemone to hug me tenderly."};
  String alltext = join(lines, " ");
  tokens = splitTokens(alltext, "\n\";.?!'():\n ");
  
  println("with duplicates"); 
  printArray(tokens);
  
  for (int i =0; i<tokens.length; i++) {
    String word = tokens[i].toLowerCase();

    if (counts.hasKey(word)) {
      counts.increment(word);
      println("\tfound duplicate", word);
    } else {
      counts.set(word, 1);
    }
    //println(word);
  }

  String []keys = counts.keyArray();
  
  println("without duplicates"); 
  printArray(keys);
  
  for (int i=0; i<keys.length; i++) {

    textSize(20);
    float x = 50;
    float y= 50+15*i;
    text(keys[i], x, y);
  }
}

void draw() {
}

如果您想呈现所有单词,只需在调用 text()

的第二个 for 循环中迭代(并访问)tokens 而不是 keys