gpt4 book ai didi

java - 提取字符串组件以存储为函数、模式匹配器中的 HashMap 键?

转载 作者:行者123 更新时间:2023-11-30 11:08:21 25 4
gpt4 key购买 nike

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

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

我使用此数据来填充 HashMap ,存储数据的来源及其内容如下。

/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 模式匹配器来确保只有前面提到的字符串,包括目录的路径,而不是文件名,被保存到 HashMap 中?

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

下面是填充 HashMap 的方法。

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);
}
}

最佳答案

它比那简单多了:

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

关于java - 提取字符串组件以存储为函数、模式匹配器中的 HashMap 键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598082/

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