gpt4 book ai didi

java - 在 Java 中获取 'trigrams'

转载 作者:行者123 更新时间:2023-11-29 03:00:05 28 4
gpt4 key购买 nike

我在使用 Java 获取 trigrams 时遇到了一些问题。我的程序目前可以很好地获取 bigrams,但是当我尝试实现相同的方法结构并将其更改为获取 trigrams 时,它似乎无法正常工作。我希望 trigrams 获取 arraylist 中所有可能的单词组合,例如

Original = [eye, test, find, free, nhs]
Trigram = [eye test find, 2, eye test free, 3, eye test nhs, 4, eye find free, 3, eye find nhs, 4, eye free nhs, 5, etc...]

数字决定了第一个词和最后一个词之间的距离,应该得到arraylist中每一个3的词组合。这目前适用于 bigrams...

Original = [eye, test, find, free, nhs]
Bigram = [eye test, 1, eye find, 2, eye free, 3, eye nhs, 4, test find, 1, test free, 2, test nhs, 3, find free, 1, etc..]

方法如下

public ArrayList<String> bagOfWords;
public ArrayList<String> bigramList = new ArrayList<String>();
public ArrayList<String> trigramList = new ArrayList<String>();


public void trigram() throws FileNotFoundException{
PrintWriter tg = new PrintWriter(new File(trigramFile));
// CREATES THE TRIGRAM
for (int i = 0; i < bagOfWords.size() - 1; i++) {
for (int j = 1; j < bagOfWords.size() - 1; j++) {
for(int k = j + 1; k < bagOfWords.size(); k++){
int distance = (k - i);
if (distance < 4){
trigramList.add(bagOfWords.get(i) + " " + bagOfWords.get(j) + " " + bagOfWords.get(k) + ", " + distance);
}
}
}
}


public void bigram() throws FileNotFoundException{
// CREATES THE BIGRAM
PrintWriter bg = new PrintWriter(new File(bigramFile));
for (int i = 0; i < bagOfWords.size() - 1; i++) {
for (int j = i + 1; j < bagOfWords.size(); j++) {
int distance = (j - i);
if (distance < 4){
bigramList.add(bagOfWords.get(i) + " " + bagOfWords.get(j) + ", " + distance);
}
}
}

任何人都可以帮我修改 trigram() 方法来创建一个适合我需要的 trigram 吗?感谢您的帮助。

最佳答案

你想要ji+1 开始,不是吗?另外,我认为你让i数到远。它应该停在 bagOfWords.size() - 2 .我不确定你为什么检查 distance < 4 .这将丢弃有效的组。

public void trigram() throws FileNotFoundException{
PrintWriter tg = new PrintWriter(new File(trigramFile));
// CREATES THE TRIGRAM
for (int i = 0; i < bagOfWords.size() - 2; i++) {
for (int j = i + 1; j < bagOfWords.size() - 1; j++) {
for(int k = j + 1; k < bagOfWords.size(); k++){
int distance = (k - i);
trigramList.add(bagOfWords.get(i) + " " + bagOfWords.get(j) + " " + bagOfWords.get(k) + ", " + distance);
}
}
}

关于java - 在 Java 中获取 'trigrams',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35629875/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com