【问题标题】:Recreating lyrics (words) from term frequency counts (numbers)从词频计数(数字)重新创建歌词(单词)
【发布时间】:2013-12-09 22:26:26
【问题描述】:

我正在尝试根据词频计数“重新创建”音乐歌词。我有两个源数据文件。第一个是我正在使用的歌词语料库中最常用的 5000 个术语的列表,按照从最常用 (1) 到最少使用 (5000) 的顺序排列。第二个文件是歌词库本身,由超过 200,000 首歌曲组成。

每个“歌曲”都是一个逗号分隔的字符串,如下所示:

SONGID1,SONGID2,1:13,2:10,4:6,7:15,....

其中前两个条目是歌曲的 ID 标签,后跟术语(冒号左侧的数字)和该术语在歌曲中使用的次数(右侧的数字)冒号)。在上面的示例中,这意味着“I”(5000 个最常用词条中的第一个词条“1”)在这首歌曲中出现了 13 次,而“the”(第二常用词条)出现了 10 次,等等。

我想要做的是从这种termID:termCount 格式转到实际“重新创建”原始(尽管是乱码)歌词,我将冒号左侧的数字设置为实际术语,然后列出这些术语给定冒号右侧的术语计数的适当次数。同样,使用上面的简短示例,我首选的结果输出是:

SONGID1, SONGID2, I I I I I I I I I I I I I the the the the the the the the the the and and and and and and and...

等等。谢谢!

【问题讨论】:

    标签: parsing text processing word-frequency


    【解决方案1】:

    也许以下(未经测试)会激发您的灵感。你没有说如何你希望它输出,所以你可能想将print()s 更改为文件写入或其他内容。

    //assumes that each word is on its own line, sorted from most to least common
    String[] words = loadStrings("words.txt");
    
    //two approaches: 
    //loadStrings() again, but a lot of memory usage for big files. 
    //buffered reader, which is more complicated but works well for large files.
    BufferedReader reader = createReader("songs.txt");
    String line = reader.readLine();
    while(line != null){
      String[] data = line.split(",");
      print(data[0] + ", " + data[1]); //the two song IDs
      for(int i = 2; i < data.length; i++){ 
        String[] pair = data[i].split(":");
        // inelegant, but clear. You may have to subtract 1, if
        // the words index from 1 but the array indexes from 0
        for(int j = 0; j < int(pair[1]); j++)
          print(words[int(pair[0])] + " ");
      }
      println();
      line = reader.readLine();
    }
    reader.close();
    

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      相关资源
      最近更新 更多