0
所以我做了一個程序,從NPR的RSS提要中獲取數據,現在我很好奇如何對提要中的描述進行字數統計,這裏是我的兩個我正在努力鞏固的程序。做RSS頻率的字數
package twp.brady.barry;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import java.net.*;
public class RSSReader {
public static String readRSS(String urlAddress){
try{
URL rssUrl = new URL(urlAddress);
BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
String sourceCode = "";
String line;
while((line = in.readLine())!=null){
if (line.contains("<title>")){
int firstPos = line.indexOf("<title>");
String temp = line.substring(firstPos);
temp = temp.replace("<title>","");
int lastPos = temp.indexOf("</title>");
temp = temp.substring(0,lastPos);
sourceCode += temp+"\n";
}
if (line.contains("<description>")){
int firstPos = line.indexOf("<description>");
String temp = line.substring(firstPos);
temp = temp.replace("<description>","");
int lastPos = temp.indexOf("</description>");
temp = temp.substring(0,lastPos);
sourceCode += temp+"\n";
}
}
in.close();
return sourceCode;}
catch(MalformedURLException ue){
System.out.println("Malformed URL");
}
catch(IOException ioe){
System.out.println("Something went wrong reading the comments");
}
return urlAddress;
}
public static void main(String[] args){
System.out.println(readRSS("http://www.npr.org/rss/rss.php?id=1001"));
}
}
這裏是我的計劃,我發現,幫助做單詞的頻率計數
package twp.brady.barry;
public class FrequencyOfWords
{
public static void main(String[]args)
{
String text = "apples are apples and I love them";
String[] keys = text.split(" ");
String[] uniqueKeys;
int count = 0;
uniqueKeys = getUniqueKeys(keys);
for(String key: uniqueKeys)
{
if(null == key)
{
break;
}
for(String s : keys)
{
if(key.equals(s))
{
count++;
}
}
System.out.println("Count of ["+key+"] is : "+count);
count=0;
}
}
private static String[] getUniqueKeys(String[] keys)
{
String[] uniqueKeys = new String[keys.length];
uniqueKeys[0] = keys[0];
int uniqueKeyIndex = 1;
boolean keyAlreadyExists = false;
for(int i=1; i<keys.length ; i++)
{
for(int j=0; j<=uniqueKeyIndex; j++)
{
if(keys[i].equals(uniqueKeys[j]))
{
keyAlreadyExists = true;
}
}
if(!keyAlreadyExists)
{
uniqueKeys[uniqueKeyIndex] = keys[i];
uniqueKeyIndex++;
}
keyAlreadyExists = false;
}
return uniqueKeys;
}
}
我無法找到一種方法把兩者結合起來,這樣我可以從一個字計數RSS說明。
你有什麼特別的麻煩? –
我不知道如何使用RSSreader程序中的單詞程序的頻率,我試圖使用單詞程序的頻率來計算RSS源的描述和標題中的單詞頻率 –