首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中不带Scanner.class的k-shingles中的单独文本

在Java中不带Scanner.class的k-shingles中的单独文本
EN

Stack Overflow用户
提问于 2020-05-14 21:46:41
回答 1查看 135关注 0票数 0

我正在尝试用k-shingles分隔一个文本,遗憾的是我不能使用扫描仪。如果最后一块瓦片太短,我想填上"_“。我走了这么远:

代码语言:javascript
复制
public class Projektarbeit {

    public static void main(String[] args) {
        testKShingling(7, "ddssggeezzfff");
    }

    public static void testKShingling(int k, String source) {
        //first eliminate whitespace and then fill up with withespaces to match target.length%shingle.length() == 0
        String txt = source.replaceAll("\\s", "");

        //get shingles
        ArrayList<String> shingles = new ArrayList<String>();
        int i;
        int l = txt.length();
        String shingle = "";

        if (k == 1) {
            for(i = 0; i < l; i++){
                shingle = txt.substring(i, i + k);
                shingles.add(shingle);
            };
        }
        else {
            for(i = 0; i < l; i += k - 1){
                try {
                    shingle = txt.substring(i, i + k);
                    shingles.add(shingle);
                }
                catch(Exception e) {
                    txt = txt.concat("_");
                    i -= k - 1;
                };
            };
        }
        System.out.println(shingles);
    }
}

Output: [ddssgge, eezzfff, f______]

它几乎可以工作,但在使用示例中给定的参数时,最后一个shingle是不必要的(它应该是ddssgge,eezzfff

你知道怎么做得更漂亮吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-15 13:38:30

要使发布的代码正常工作,只需添加break和catch块的末尾:

代码语言:javascript
复制
catch(Exception e) {
     txt = txt.concat("_");
     i -= k - 1;
      break;
};

话虽如此,我不会使用异常来控制程序。异常只是:应该用于运行时错误。通过控制循环参数来避免StringIndexOutOfBoundsException

代码语言:javascript
复制
public static void main(String[] args) {
    testKShingling(3, "ddssggeezzfff");
}

public static void testKShingling(int substringLength, String source) {

    //todo validate input
    String txt = source.replaceAll("\\s", "");
    //get shingles
    ArrayList<String> shingles = new ArrayList<>();
    int stringLength = txt.length();

    if (substringLength == 1) {
        for(int index = 0; index < stringLength; index++){
            String shingle = txt.substring(index, index + substringLength);
            shingles.add(shingle);
        };
    }
    else {
        for(int index = 0; index < stringLength -1 ; index += substringLength - 1){
            int endIndex = Math.min(index + substringLength, stringLength);
            String shingle = txt.substring(index, endIndex);
            if(shingle.length() < substringLength){
                shingle = extend(shingle, substringLength);
            }
            shingles.add(shingle);

        };
    }
    System.out.println(shingles);
}

private static String extend(String shingle, int toLength) {

    String s = shingle;
    for(int index = 0; index < toLength - shingle.length(); index ++){
        s = s.concat("_");
    }
    return s;
}

testKShingling的另一种实现

代码语言:javascript
复制
public static void testKShingling(int substringLength, String source) {

    //todo validate input
    String txt = source.replaceAll("\\s", "");
    ArrayList<String> shingles = new ArrayList<>();

    if (substringLength == 1) {
        for(char c : txt.toCharArray()){
            shingles.add(Character.toString(c));
        };
    }
    else {
        while(txt.length() > substringLength) {
            String shingle = txt.substring(0, substringLength); 
            shingles.add(shingle);
            txt = txt.substring(substringLength - 1); //remove first substringLength - 1 chars 
        }

        if(txt.length() < substringLength){  //check the length of what's left 
            txt = extend(txt, substringLength); 
        }
        shingles.add(txt); //add what's left 
    }
    System.out.println(shingles);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61798969

复制
相关文章

相似问题

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