提取字符串组件以存储为函数、模式匹配器中的哈希映射键?
extract string component to store as hash map key within function, pattern matcher?
我将数据存储在以下路径的文件中:
/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);
}
}
如果我对你的问题的理解正确,你只想查找以 /
结尾的部分(文件名不会包含它)。在那种情况下
(\w+/)+
should do the trick(顺便说一句,我们不会在 Java 的正则表达式中转义 /
)
但是,如果您的数据始终采用 path/to/file
形式并且您只想提取 path/to
那么您不需要正则表达式,您可以使用文件 class 及其 getParent
方法类似
String data = new File("/home/yamada/data/train/atheism/file_name.txt").getParent();
System.out.println(data);
这将 return \home\yamada\data\train\atheism
所以你将有 /
而不是 \
,但如果你想在中使用此数据,这应该不是问题Java(File
接受两个分隔符)。
比那简单多了:
String dir = filename.replaceAll("/[^/]*$", "");
我将数据存储在以下路径的文件中:
/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);
}
}
如果我对你的问题的理解正确,你只想查找以 /
结尾的部分(文件名不会包含它)。在那种情况下
(\w+/)+
should do the trick(顺便说一句,我们不会在 Java 的正则表达式中转义 /
)
但是,如果您的数据始终采用 path/to/file
形式并且您只想提取 path/to
那么您不需要正则表达式,您可以使用文件 class 及其 getParent
方法类似
String data = new File("/home/yamada/data/train/atheism/file_name.txt").getParent();
System.out.println(data);
这将 return \home\yamada\data\train\atheism
所以你将有 /
而不是 \
,但如果你想在中使用此数据,这应该不是问题Java(File
接受两个分隔符)。
比那简单多了:
String dir = filename.replaceAll("/[^/]*$", "");