2016-07-08 29 views
1

我試圖訪問具有id,名稱,文本和單詞列表的文檔類。我嘗試比較一下我的id和id,找到附在這個id上的單詞列表,找到確切的單詞並返回它的頻率。 任何幫助是高度讚賞。訪問一個類的字段和方法來檢索具有某個值的字段

public class Doc { 
    private int documentID; 
    private static Doc docInstance = null; 
    private String documentText; 
    private ArrayList<String> listOfTokens; 
    static int docCount = 0; 
    int tokFreq = 0; 

    public Doc() { 
     documentID = 0; 
     listOfTokens = new ArrayList<String>(); 
     tokFreq = 0; 
     docCount++; 
    } 

    public static Doc getDocInstance() { 
     if (docInstance == null) { 
      docInstance = new Doc(); 
     } 
     return docInstance; 
    } 

    public ArrayList<String> getListOfTokens() { 
     return listOfTokens; 
    } 

    public void setDocumentID(int x){ 
     if (getDocumentID() != x) 
      this.documentID = x; 
    } 
} 

,我想這

public static void createDocumentVector(TreeMap<Integer,Integer> 
documentVector, TreeMap<String, ArrayList<Integer>>qm, int N) 
{ 
    int eachDoc = 0; 

    Collection<String> allKeys = qm.keySet(); 
    ArrayList<Integer> l1 = new ArrayList<Integer>(); 
    boolean addedTerm = false; 

    /** 
    Obtain an Iterator for Collection 
    */ 
    Iterator<String> itr = allKeys.iterator(); 
    String key; 
    int termFrequency = 0; 
    int documentFrequency = 0; 

    /** 
    Iterate through TreeMap values iterator 
    */ 
    while(itr.hasNext()) 
    { 
     key = (String)itr.next(); 
     Integer LL = 0; 
     l1 = qm.get(key); // Returns value of that key 
     for (int k = 0; k < l1.size(); k++) 
     { 
      LL = l1.get(k); 
      Doc doc = new Doc(); 
      doc.getDocInstance().setDocumentID(LL); 
      int size = doc.getListOfTokens().size(); 
      String[] docIdTokens = doc.getListOfTokens().toArray(new String[size]); 
      for (String s : docIdTokens){ 
       if(s.equalsIgnoreCase(key)){ 
       termFrequency++; 
       } 
      } 

      documentFrequency = l1.size(); 
      eachDoc = getTFIDF(termFrequency, documentFrequency, N); 
      documentVector.put(eachDoc, LL); 
     } 


    } 
} 

它不完全運行,並給出源沒有發現,在調試。 我想改變類文件到這一點:

public class Doc<ListOfTokens> { 

private static int documentID; 
private static Doc docInstance = null; 
private String documentName; 
private String documentText; 
private HashMap<String, Integer> ListOfTokens = new HashMap<String, Integer>(); 
private TreeMap<Integer, ListOfTokens> documentMap = new TreeMap<Integer, ListOfTokens>(); 
int tokFreq = 0; 
static int docCount = 0; 
----- 
} 

但是這將是一個有點複雜,我認爲。所以任何建議都會有很大的幫助。

+0

你看,你不想使用'static' - 這意味着只有這些對象中的一個。在你的代碼中只有一個'documentID' –

回答

0

對於Doc類的documentID屬性,不需要使用關鍵字static。由於您期望會有多個doc對象,並且每個對象都需要具有自己的documentID值,所以需要將documentID用作非靜態字段。

+0

謝謝你的提示,我改變了它,但是還有更多的想法! – Bebo

相關問題