2017-01-23 4 views
1

Also habe ich dieses Wrapper-Programm, mit dem ich zwei Mengen von einer Methode zurückgeben kann.Verbinden einer Wrapper-Klasse mit einer anderen Klasse

** Wrapper-Klasse **

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

Die Methode Ich möchte dies binden mit ist:

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

Wie Sie sehen können, was ich in diesem Verfahren zu tun versuche, ein statt Eigenname und extrahieren die linken und rechten Wörter dieses Eigennamens.Eg für ein Stück "Kollegen Rhode Island Solution Provider" meine Ausgabe ist:

LINKS WORTE: fellow OF: Rhode Island
richtigen Worte: Lösungsanbieter: Rhode Island

Jetzt möchte ich diese in einer Karte setzen, wo Rhode Island der Schlüssel und die Werte hierfür ist, sind Lösung Anbieter und Kollege.

Wenn ich versuche, das die Ausgabe get Karte zu drucken:

{[Rhode Island] = [[email protected]]}

Wie bekomme ich die richtige Ausgabe?

+1

Ich verstehe nicht, warum Sie ein 'Set' als Schlüssel verwenden. –

+0

Ich verstehe nicht, warum Sie 'Set' als Wert und' String' verwenden, nicht ein 'Set ' zum Speichern von 'leftWords' und' rightWords'. –

+0

Ich benutzte Set früher, aber änderte es. Ich kann zurückkehren, wenn das mehr Sinn macht. – serendipity

Antwort

0

Ich weiß nicht, ob es das einzige Problem ist, aber Ihre Klasse Words überschreibt nicht toString() Methode.

Nicht sicher über Ihre Java-Fähigkeit. Es tut mir leid, wenn ich das posten, was Ihnen bekannt ist. System.out.println(...) Aufrufe toString() Methode, um Nachricht für das Objekt zu erhalten. Durch das Überschreiben Standard mit Ihrer eigenen Implementierung

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

Sie ändern [email protected] Ihren eigenen Ausgang.

+0

@Thomas F. Danke für die Antwort. Ich würde mich selbst einen Anfänger in Java nennen. Also, was Sie sagen, ist, dass ich die Überschreibung aus dem Wrapper auskommentieren muss? – serendipity

+0

Nein, Sie müssen meinen Code in den Wrapper kopieren. Und es scheint mir, dass Sie die Map-Semantik nicht richtig verstehen, sehen Sie sich Hilfe an. –

+0

Könnten Sie das näher erläutern? Der Grund, warum ich den Satz für linke und rechte Wörter nicht mehr verwendete, ist, dass ich diese linken und rechten Wörter mit einigen Wortlisten vergleichen muss, um das Eigenname in Name, Ort oder Organisation zu klassifizieren. Bei einem Set als Datenstruktur muss ich es später in ein String-Format zum Matching umwandeln. Daher entschied man sich, String von Anfang an zu verwenden. Wird dies ein Problem verursachen? – serendipity

Verwandte Themen