2016-02-21 139 views
-2

這是一個家庭作業問題,我堅持了很長時間。我們正在學習一些關於搜索引擎技術的索引文件,統計每個單詞出現在它們中的次數。必須創建兩個類; 期限文件創建一個簡單的搜索引擎模型

期限類有:字符串命名WORD,INT頻率(字計數頻率),一個構造函數中的一個字和一個頻率和初始化對象,獲取的方法和設置方法(但沒有爲freq設置方法)。

文獻類,問題詢問創建名爲術語存儲的字符串的話(I給出由空格分開它們),日提交的測量SQRT(求和雙大小的陣列(術語 [i] .freq)^ 2);其中freq是字符串中每個不同單詞的頻率。我必須創建一個構造函數,它將單個String參數作爲全文,將其轉換爲小寫,將其分解爲白色接下來我要創建一個靜態的排序方法,而不需要java.util.Arrays的幫助來按字母順序排序字符串數組。然後我將創建一個countDistinctStrings方法取排序後的字符串作爲參數,並計算唯一字數(忽略重複)。然後調用一個名爲的索引內容,它將文檔中的單詞數組作爲參數,填充條目數組,並計算大小,我剛纔提到的方法首先確定原始數組中有多少不同的項,然後創建一個條目數組的大小。它將排序順序中的條款的唯一字符串,並提供頻率在一張桌子裏。輸出應該是這樣的: +輸入文檔的文本: 我想我找到了一些更好的聲音,沒有人聽說過我希望我有一個更好的聲音,唱一些更好的話

詞頻列表: a〜1,有〜1,〜1,〜1,〜3,〜〜,〜〜〜,〜〜〜,〜〜〜,〜〜〜〜〜〜 ,發現〜1,沒有了〜1,聲音〜1,也就是說〜1(但在一個表)幅度 6.708

這是迄今爲止我嘗試:期限

class Term { 
private String word; 
private int freq; 
Term(String word,int freq){ 
    this.word = word; 
    this.freq = freq; 
} 

public String getWord() 
{ 
    return word; 
} 

public int getFreq() 
{ 
    return freq; 
} 

public void setFreq(int freq) 
{ 
    this.freq = freq; 
} 

} 

然後文件類:(注意代碼是不完整的,因爲我不知道如何採取下一步,即如何讓數組排序方法爲其他countDistinctStrings.I甚至不能用我的對象調用的方法,這將引發異常線程「main」顯示java.lang.NullPointerException

import java.util.*; 

public class Document 
{ 
String text; 
private double magnitude; 
static String terms []; 

Document(String text) { 
this.text = text; 
text = text.toLowerCase(); 
String [] terms = text.split("\\s+"); 
for (String s : terms) 
System.out.println(s); 

} 
private static void sort (String [] terms) { 
    String tempVar; 
    for (int i = 0; i < terms.length;i++) { 
     for(int j = 0; j <terms.length ; j++) { 
       if(terms[i].compareTo(terms[j]) > 0) { 
         tempVar = terms [j]; 
         terms [j ]= terms [i]; 
         terms [i] = tempVar; 
       } 
      } 
     } 
     } 
private void countDistinctStrings() { 
    Document.sort(terms); 
} 


public double getMagnitude(){ 
return magnitude; 
} 
public static void main(String [] args) { 
Scanner input = new Scanner(System.in); 
System.out.println("Enter the document text : "); 
String text = input.nextLine(); 
Document object = new Document(text); 
object.sort(terms); 

} 
} 

我是一個beginner.I會很感激別人的幫助和時間在我的學習process.This功課現在已經過期了,但我真的很喜歡nt做到這一點並理解它。

+0

Afif,您的代碼的第一個問題是在Document的構造函數中。您錯誤地重新定義了條款。刪除'String []'部分,因此它只是'terms = text.split ...並且你會越過這個錯誤。 –

+0

@ Ian.Many謝謝你的建議,現在它不會拋出空指針異常。但是,我仍然沒有得到按字母順序排序的字符串。 – Afif

+0

我運行了你的代碼,它確實按照相反的順序排序。如果要按順序排序,請將compareTo行從「>」更改爲「<」。 –

回答

1
  1. 卸下 '字符串[]' 從構造
  2. 更改 '>' 到 '<' 上的compareTo線

    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in); 
        System.out.println("Enter the document text : "); 
        String text = input.nextLine(); 
        Document object = new Document(text); 
        object.sort(terms); 
        System.out.println("--- AFTER SORT ----"); 
        for (String s : terms) 
         System.out.println(s); 
    
    } 
    

輸出:

Enter the document text : 
I wish I found some better sounds no one's ever heard I wish I had a better voice that sang some better words 
i 
wish 
i 
found 
some 
better 
sounds 
no 
one's 
ever 
heard 
i 
wish 
i 
had 
a 
better 
voice 
that 
sang 
some 
better 
words 
--- AFTER SORT ---- 
a 
better 
better 
better 
ever 
found 
had 
heard 
i 
i 
i 
i 
no 
one's 
sang 
some 
some 
sounds 
that 
voice 
wish 
wish 
words 
+0

@Ian,是的它的工作。讓我看看我是否可以將它們存儲在一個數組中,並計算出頻率,以獲得與問題中提到的完全相同的輸出。但是,您幫助我解決了最困難的部分。我無法找到什麼是錯,但只是一個標誌,「<」,喲......上帝保佑你。 – Afif

+1

非常好完成Afif。大部分完美的代碼。如果您在下一節遇到問題,請告訴我。乾杯。 –