gpt4 book ai didi

java - 将包装类连接到另一个类

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:19 25 4
gpt4 key购买 nike

所以我有这个包装程序,它使我能够从一个方法返回两个数量。

** 包装类**

public class Words 
{

private String leftWords;
private String rightWords;

public Words(String leftWords, String rightWords) {
this.leftWords = leftWords;
this.rightWords = rightWords;
}

public String getLeftWords() {
return leftWords;
}

public String getRightWords() {
return rightWords;
}



@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result
+ ((leftWords == null) ? 0 : leftWords.hashCode());
result = prime * result
+ ((rightWords == null) ? 0 : rightWords.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Words other = (Words) obj;
if (leftWords == null)
{
if (other.leftWords != null)
return false;
}
else if (!leftWords.equals(other.leftWords))
return false;
if (rightWords == null)
{
if (other.rightWords != null)
return false;
}
else if (!rightWords.equals(other.rightWords))
return false;
return true;
}
}

我想与之联系的方法是:

private static Map <Set<String>,Set<Words>> getLeftRightWords(LinkedHashMap<Set<String>,Set<Integer>> nnpIndexTokens, NLChunk chunk) throws FileNotFoundException
{
// Map <Set<String>,Set<Integer>> nnpMap = new LinkedHashMap<Set<String>, Set<Integer>>();
Map <Set<String>,Set<Words>> contextMap = new LinkedHashMap<Set<String>, Set<Words>>();
Set<Words> leftRightWords = new HashSet<Words>();


//for(NLChunk chunk : sentence.getChunks()){

if(chunk.getStrPostags().contains("NNP")){


String leftWords = "";
String rightWords = "";

int chunkStartIndex = chunk.getStartIndex();
int chunkEndIndex = chunk.getEndIndex();

//nnpMap = getNNPs(chunk);


String previous = null;
int previousNnpEndIndex = 0;
int previousNnpStartIndex = 0;


for (Map.Entry<Set<String>, Set<Integer>> entry : nnpIndexTokens.entrySet()){


for (Iterator<String> i = entry.getKey().iterator(); i.hasNext();){
Set<Integer> entryIndex = null;
int nnpStartIndex = 0;
int nnpEndIndex = 0;

String currentElement = i.next();


//Deriving values for beginning and ending of chunk
//and beginning and ending of NNP

if (!(entry.getValue().isEmpty())){

if (currentElement.trim().split(" ").length > 1){
entryIndex = entry.getValue();
nnpStartIndex = entryIndex.iterator().next();
nnpEndIndex = getLastElement(entryIndex);

}

else {
entryIndex = entry.getValue();
nnpStartIndex = entryIndex.iterator().next();
nnpEndIndex = nnpStartIndex;
}

}

if(!(chunkStartIndex<=nnpStartIndex && chunkEndIndex>=nnpEndIndex)){
continue;
}
//Extracting LEFT WORDS of the NNP

//1)If another NNP is present in left words, left words of current NNP start from end index of previous NNP
if (previous != null && chunk.toString().substring(chunkStartIndex, nnpStartIndex).contains(previous)){

int leftWordsEndIndex = nnpStartIndex;
int leftWordsStartIndex = previousNnpEndIndex;


for (NLWord nlword : chunk.getTokens())
{
if(nlword.getIndex()>=leftWordsStartIndex
&& nlword.getIndex()<leftWordsEndIndex )
leftWords+=nlword.getToken() +" ";


}

System.out.println("LEFT WORDS:" + leftWords+ "OF:"+ currentElement);

}

//2) If no left words are present

if (chunkStartIndex == nnpStartIndex){
System.out.println("NO LEFT WORDS");

}
//3) Normal case where left words consist of all the words left of the NNP starting from the beginning of the chunk
else {


for (NLWord nlword : chunk.getTokens())
{
if(nlword.getIndex()>=chunkStartIndex
&& nlword.getIndex()<nnpStartIndex )
leftWords+=nlword.getToken() +" ";


}

System.out.println("LEFT WORDS:" + leftWords+ "OF:"+ currentElement);
}


//Extracting RIGHT WORDS of NNP
if (entry.getKey().iterator().hasNext()){// entry.getKey().iterator().hasNext()){

String nextElement = entry.getKey().iterator().next();

//1)If another NNP is present in right words, right words of current NNP start from end index of current NNP to beginning of next NNP
if (nextElement !=null && nextElement != currentElement && chunk.toString().substring(entry.getValue().iterator().next(), chunkEndIndex).contains(nextElement)){

int rightWordsStartIndex = entryIndex.iterator().next();
int rightWordsEndIndex = entry.getValue().iterator().next();


//String rightWord="";

for (NLWord nlword : chunk.getTokens())
{
if(nlword.getIndex()>=rightWordsStartIndex
&& nlword.getIndex()<rightWordsEndIndex )
rightWords+=nlword.getToken() +" ";


}

System.out.println("LEFT WORDS:" + leftWords+ "OF:"+ currentElement);
}
}

//2) If no right words exist
if(nnpEndIndex == chunkEndIndex){
System.out.println("NO RIGHT WORDS");
//continue;
}

//3) Normal case where right words consist of all the words right of the NNP starting from the end of the NNP till the end of the chunk
else {

for (NLWord nlword : chunk.getTokens())
{
if(nlword.getIndex()>=nnpEndIndex+1
&& nlword.getIndex()<=chunkEndIndex )
rightWords+=nlword.getToken() +" ";


}

System.out.println("RIGHT WORDS:" + rightWords+ "OF:"+ currentElement);
}



if (previous == null){
previous = currentElement;
previousNnpStartIndex = nnpStartIndex;
previousNnpEndIndex = nnpEndIndex;
}

Words contextWords = new Words(leftWords.toString(), rightWords.toString());
leftRightWords.add(contextWords);


}
contextMap.put(entry.getKey(), leftRightWords);
}//nnps set

}

System.out.println(contextMap);
return contextMap;
}

正如你所看到的,我在这个方法中试图做的是获取一个专有名词并提取该专有名词的左右单词。例如,对于一个 block “罗德岛解决方案提供商伙伴”,我的输出是:

LEFT WORDS:fellow OF:Rhode Island
RIGHT WORDS:solution provider OF:Rhode Island

现在我想将它们放入 map 中,其中罗德岛是关键,其值是解决方案提供商和伙伴。

当我尝试打印此 map 时,输出为:

{[Rhode Island ]=[com.gyan.siapp.nlp.test.Words@681330f0]}

如何获得正确的输出?

最佳答案

我不知道这是否是唯一的问题,但您的类 Words 不会覆盖toString() 方法。

不确定您的 Java 技能水平。如果我发布你熟悉的内容,我很抱歉。System.out.println(...) 调用 toString() 方法来获取对象的消息。通过使用您自己的实现覆盖默认值

@Override
public String toString(){
return "leftWords: "+leftWords+", rightWords: "+rightWords;
}

您将 com.gyan.siapp.nlp.test.Words@681330f0 更改为您自己的输出。

关于java - 将包装类连接到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805746/

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