首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >N-Gram与ArrayList

N-Gram与ArrayList
EN

Stack Overflow用户
提问于 2016-02-24 19:35:50
回答 1查看 1.5K关注 0票数 0

我正在进行一个分析“ngram”的项目。我在我的程序中有一个方法,可以创建大图和三角图。但是,他们只把相邻的单词连在一起,我想让它得到所有单词的组合.

例如,

代码语言:javascript
运行
复制
 Original String - "chilli, start, day, suffer, raynaud, check, raynaudsuk, great, tip, loveyourglov, ram"
 Bigram - "chilli start, start day, day suffer, suffer raynaud, raynaud check, check raynaudsuk, raynaudsuk great, great tip, tip loveyourglov, loveyourglov ram"

但我想要得到字符串中所有单词的组合。例如

代码语言:javascript
运行
复制
Expected Bigram - "chilli start,1, chilli day,2, chilli suffer,3, chilli raynaud,4, chilli check,5, chilli raynaudsuk,6, chilli great,7, chilli tip,8, chilli loveyourglov,9, chilli ram,10, start day,1, etc..."

我怎么才能修改我的方法来产生这样的比例尺呢?

代码语言:javascript
运行
复制
public ArrayList<String> bigramList;
ArrayList<String> fullBagOfWords = new ArrayList<String>();


public void bigramCreator(){
    int i = 0;
    bigramList = new ArrayList<String>();
    for(String bi : fullBagOfWords){
        int n = 2;
        if (i <= fullBagOfWords.size() - n) {
            String bigram = "";
            for (int j = 0; j < n-1; j++)
            bigram += fullBagOfWords.get(i + j) + " ";
            bigram += fullBagOfWords.get(i + n - 1);
            bigramList.add(bigram);
            i++;
        }
    }
}

非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-24 19:45:34

如果我正确理解这个任务,它应该非常简单。

代码语言:javascript
运行
复制
for (int i = 0; i < fullBagOfWords.size() - 1; i++) {
    for (int j = i + 1; j < fullBagOfWords.size(); j++) {
        bigramList.add(fullBagOfWords.get(i) + " " + fullBagOfWords.get(j) + ", " + (j - i));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35611406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档