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



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

**

包装类**

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

如您所见,我在这种方法中试图做的是获取一个专有名词并提取该专有名词的左右单词。例如,对于块"罗德岛解决方案提供商",我的输出是:

左词:同胞:罗德岛
右话:解决方案提供商:罗德岛

现在我想把它们放在一个地图中,其中罗德岛是键,其值是解决方案提供商和研究员。

当我尝试打印此地图时,输出结果为:

{[罗德岛]=[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更改为您自己的输出。

最新更新