【问题标题】:extract string component to store as hash map key within function, pattern matcher?提取字符串组件以存储为函数中的哈希映射键,模式匹配器?
【发布时间】:2015-02-19 03:19:57
【问题描述】:

我将数据存储在以下路径的文件中:

/home/yamada/data/train/atheism/file_name.txt

我使用这些数据来填充哈希映射,存储数据的来源及其内容,如下所示。

/home/yamada/data/test/sports/t.s_1.txt, [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0]
/home/yamada/data/test/politics/t.p_0.txt, [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
/home/yamada/data/test/atheism/t.a_0.txt, [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/home/yamada/data/test/science/t.s_0.txt, [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]

但是,我只想存储指向目录的路径,而不是特定文件,如下所示:

/home/yamada/data/train/atheism

以下正则表达式命令能够根据regex101.com提取我感兴趣的组件:

(home\/yamada\/data\/train\/atheism)

我如何使用 java 模式匹配器来确保只有前面提到的字符串,包括目录的路径,而不是文件名,被保存到哈希映射?

模式匹配器是这个操作的最佳选择吗?

以下是填充哈希映射的方法。

public static void perceptron_data_struc_generateur(Set<String> GLOBO_DICT, 
                                                        Map<File, ArrayList<String> > fileDict,
                                                        Map<File, int[] > perceptron_input)
    {
        //create a new entry in the array list 'perceptron_input'
        //with the key as the file name from fileDict
            //create a new array which is the length of GLOBO_DICT
            //iterate through the indicies of GLOBO_DICT
                //for all words in globo dict, if that word appears in fileDict,
                //increment the perceptron_input index that corresponds to that
                //word in GLOBO_DICT by the number of times that word appears in fileDict

        //so i can get the index later
        List<String> GLOBO_DICT_list = new ArrayList<>(GLOBO_DICT);

        for (Map.Entry<File, ArrayList<String>> entry : fileDict.entrySet()) 
        {
            int[] cross_czech = new int[GLOBO_DICT_list.size()];
            //initialize to zero
            Arrays.fill(cross_czech, 0);

            for (String s : GLOBO_DICT_list)
            {

                for(String st : entry.getValue()) 
                {
                    if( st.equals(s) )
                    {
                        cross_czech[ GLOBO_DICT_list.indexOf( s ) ] = cross_czech[ GLOBO_DICT_list.indexOf( s ) ] +1;
                    }
                }
            }
            perceptron_input.put( entry.getKey() , cross_czech);    
        }
    }

【问题讨论】:

    标签: java regex pattern-matching


    【解决方案1】:

    比这简单一点:

    String dir = filename.replaceAll("/[^/]*$", "");
    

    【讨论】:

    • 这会截断文件名吗?
    • 这不是你的意思吗?目录名称?
    【解决方案2】:

    如果我正确理解您的问题,您只想找到以/ 结尾的部分(文件名将没有它)。在那种情况下

    (\w+/)+
    

    should do the trick(顺便说一句,我们不会在 Java 的正则表达式中转义 /


    但是如果您的数据始终采用path/to/file 形式,并且您只想提取path/to,那么您不需要正则表达式,您可以使用 File 类及其getParent 方法,例如

    String data = new File("/home/yamada/data/train/atheism/file_name.txt").getParent();
    System.out.println(data);
    

    这将返回\home\yamada\data\train\atheism,因此您将使用/ 而不是\,但如果您想在Java 中使用此数据,这应该不是问题(File 接受两个分隔符)。

    【讨论】:

    • 也许我可以将它与@Bohemian 的代码结合使用
    • @Yamada_Tarō Bohemians 解决方案将创建新字符串,并在最后一个 / 之后删除全部内容。您可以尝试将我的解决方案与它结合起来,但我认为没有必要。无论如何,我用非正则表达式解决方案更新了我的答案,这似乎比以前的解决方案更容易。
    • 很高兴你喜欢它。如果您知道正则表达式,波西米亚解决方案也很容易。它将尝试查找并删除以/ 开头的子字符串,直到字符串结尾($)将只有非/ 字符([^/]*)。
    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 2015-03-16
    • 2012-10-03
    • 2013-11-20
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多