用哈希映射替换名词 [英] Replacing digits with Noun using hash map

查看:126
本文介绍了用哈希映射替换名词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入句子是:


ram你在哪里


之后,我将得到这个分析树:

 '2 | TYPE | nx0e-VPadjn-Vnx1 1 | RPron | nx-RP-S 0 | NOUN | NXN 3 | NOUN | NXN','1'

我想用'where'替换2,用'where'替换2,然后用0替换ram。

我应该如何使用哈希映射来做到这一点?

解决方案

这个答案基于很多假设,因为您的问题还不够清楚。但我没有足够的代表发表评论。



如果您使用对输入句子的String.split()为:

  String [] words =ram where you.split(); 
//字[0] => ram
//字[1] =>其中
//字[2] =>是
//字[3] =>你

看来你的解析树是通过解析输入句子而生成的。

解析树的第一部分中的每个条目都对应于输入句子中的一个单词。

解析条目中的第一个数字似乎对应于输入句子中每个单词的索引。

因此,解析条目可以被分解为:< word index> |< word category> |< something not clear> p>

因此,看来

  2 | TYPE | nx0e-VPadjn-Vnx1 =>是
1 | RPron | nx-RP-S =>其中
0 | NOUN | NXN => ram
3 | NOUN | NXN =>您

基于这些假设,可以使用使用解析树条目构建的哈希映射。

您需要使用 key =< word index>将解析条目放入地图中; value =< parse entry>

这可以通过将分析树分离到条目然后检索< word index>

一旦建立了该映射,您可以处理输入句子和解析树条目:

  String [] words =ram where you.split(); 

映射< Integer,String> entriesMap = getEntriesMap(parseTree); (假设parseTree只是一个String

for(int i = 0; i< words.length; i ++){
String x = entriesMap.get(i).replaceAll( ^+ i +|,单词[i]);
}

填充地图的方法。有多种方法可以做到这一点。

使用 Pattern 匹配器类与正确的正则表达式可能是最好的方法。

 私人地图< Integer,String> getEntriesMap(String parseTree){
Map< Integer,String> entriesMap = new LinkedHashMap< Integer,String>();

//假设parseTree格式为:'<以空格分隔的解析条目>','1'
//使用String.split()将parseTree拆分为单引号(' )
//返回数组中的第一个元素将包含<用&空格分隔的parse条目>
//在该元素上再次使用String.split(),并用空格分隔解析条目
//对于每个< entry>在<解析条目>
// split< entry>使用管道(|)并使用结果数组中的第一个元素作为键,并使用< entry>作为放入条目映射的值

返回entriesMap;
}

找不到,'1'在分析树的结尾对应于。


My input sentence is:

ram where are you

After that i will get this parse tree:

' 2|TYPE|nx0e-VPadjn-Vnx1 1|RPron|nx-RP-S 0|NOUN|NXN 3|NOUN|NXN ', '1'

I want to replace 2 with 'are' 1 with 'where' and ram with 0 .

How should I do this with hash map?

解决方案

This answer is based on lot of assumptions as your question is not clear enough. But I do not have enough rep to comment.

If you use String.split() on input sentence as:

String[] words = "ram where are you".split(" ");
// words[0] => ram
// words[1] => where
// words[2] => are
// words[3] => you

It appears your parse tree was generated by parsing the input sentence.
Each entry in first section of parse tree corresponds to a word in your input sentence.
First digit in a parse entry seems to correspond to the index of each word in input sentence.

So a parse entry can be broken as: <word index>|<word category>|<something not clear>

So, it seems

2|TYPE|nx0e-VPadjn-Vnx1 => are
1|RPron|nx-RP-S => where
0|NOUN|NXN => ram
3|NOUN|NXN => you

Based on these assumptions its possible to use a hashmap built using parse tree entries.
You will need to put parse entries in to the map using key = <word index>; value = <parse entry>.
That can be done by separating parse tree to entries and then retrieving <word index> from each entry.

Once that map is built you can process input sentence and parse tree entries as:

String[] words = "ram where are you".split(" ");

Map<Integer, String> entriesMap = getEntriesMap(parseTree); // assuming parseTree is just a String

for(int i = 0; i < words.length; i++) {
    String x = entriesMap.get(i).replaceAll("^" + i + "|", words[i]);
}

Method to populate map. There are multiple ways to do this.
Use of Pattern and Matcher classes with proper regex is probably the best way.

private Map<Integer, String> getEntriesMap(String parseTree) {
    Map<Integer, String> entriesMap = new LinkedHashMap<Integer, String>();

    // assuming parseTree format as: '<parse entries separated by spaces>', '1'
    // use String.split() to split the parseTree by single quote (')
    // first element in returning array would contain the <parse entries separated by spaces>
    // use String.split() again on that element with space to separate parse entries
    // for each <entry> in <parse entries>
    //     split <entry> with pipe (|) and use first element in resulting array as the key and <entry> as the value to put in entriesMap

    return entriesMap;
}

Can not figure out what ,'1' at the end of parse tree corresponds to.

这篇关于用哈希映射替换名词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆