RSS Feed in Java

yuro

Mitglied
Hallooo,

Ich soll zu einem bestehenden Programm eine Erweiterung machen und zwar RSS.

Jetzt weiss ich aber nicht wie ich das ganze beginnen kann. Ich kann mal aufschreiben was erweitert werden soll.

Programmbsp:

public class document {
private String title; //Titel zB des RSS Eintrags

public void setTitle(String title);
public void readTitleFromText();
public String getTitle();
}

public class VSM {
public void loadFolder(String folder);
public void readFeeds(String[] URLs)
public void readFeed(String URLs)
}

Funktionen sollen folgendes leisten:

setTitle:
- Setzt den Titel "title" des Doku auf einen von außen gegebenen Wert.

readTitelFromText:
- Funktion sinnvoll, wenn der Text im HTML Format vorliegt.
- mit regul. Ausdruck soll Inhalt von <title> aus "text" ausgelesen u. in "titel" gespeichert werden.

getTitle:
- gibt den Wert von "title" zurück

loadFolder:
- liest angegeb. Ordner ein u. macht eine Schleife über alle Textdokus darin, bsp. alle mit Endung .html
- für jedes Textdoku "loadDocument" aufrufen.
- Funktion soll sich für jeden Unterordner rekursiv selbst aufrufen.

readFeeds:
- man übergibt ein Array von URLs zu RSS feeds.
- für jedes Element des Arrays wird readFeed aufgerufen.

readFeed:
- feed wird empfangen mit Methoden der open source Bibliothek.
- folgende Klassen können verwendet werden:
class URL, SyndFeedInput, SyndFeed, XmlReader, SyndEntryImpl.
- mit den Methoden:
SyndEntryImpl.getTitleEx().getValue() u. SyndEntryImpl.getDescription().getValue() (können zb Titel u. Beschreibung ausgelesen werden)
- für jeden Eintrag wird ein Dokument angelegt u. Titel + Inhalt eingelesen.

Wäre echt kool wenn mir jmd helfen könnte..

Würde mich über eine Antwort freuen.
 
bis jetzt hab ich ein groben quellcode da ich ja nicht weiss wie ich das umsetzen soll.. hab echt keine Idee... und forsch die ganze zeit schon noch infos. könntest du mir irgendwie zur hilfe gehen?? ich würd dir die quellcodes privat schicken??
 
Private Sachen sind ja nicht im Sinne des Forums. Mindestens getter und setter wirst du ja hinkriegen. Google sollte auch genügend Materials liefern.
Du kannst deinen Code auch hier reinstellen, damit mehrere Menschen helfen können und auch die Lösung für die Nachwelt bleibt.
 
Hier mal alles was ich hab:

Main.java :
Code:
public class Main {
    public static void main(String[] args) {
        VSM data = new VSM();
        data.appendDocument("Ein Porsche ist ein Auto.");
        data.appendDocument("Ein IPhone ist ein Handy.");
        data.appendDocument("Mein Auto ist ein Porsche.");
        data.appendDocument("Mein Iphone.");
        data.appendDocument("Ein Porsche ist ein Porsche.");
        data.appendDocument("Ein Porsche ist ein Auto und ein Porsche ist ein Auto.");
//        data.appendDocument("Das ist mein Porsche.");
//        data.appendDocument("Kaufe ein Iphone.");


        //debug
        data.printStatistics();

        //search
        data.search("Porsche");


    }//end of main()
}//end of class

document.java :
Code:
public class document {
    private String text = "";

    public void document() {
    }
    public void setText(String newText) {
        text = newText;
    }
    public String getText() {
        return text;
    }
}

VSM.java :
Code:
import java.util.ArrayList;
import java.util.Collections;
public class VSM {

    //declaration of attributs
    private ArrayList<String> words; // dictionary
    private ArrayList<document> docs; // list of the documents
    private ArrayList<vector> vectors; // vectors
    private ArrayList<document> searchDocs; // Search documents
    private ArrayList<vector> searchVectors; // Search vectors
    private String[] stopWords = new String[3]; // stopword
    private ArrayList<result> mathResults;// mathresults

    public VSM() {

        //init the attributs
        words = new ArrayList<String>();
        docs = new ArrayList<document>();
        vectors = new ArrayList<vector>();
        searchDocs = new ArrayList<document>();
        searchVectors = new ArrayList<vector>();

        stopWords[0] = "ein";
        stopWords[1] = "ist";
        stopWords[2] = "und";
    }//end if constructor

    public vector appendDocument(String doc) {
        //make a new document
        document newDoc = new document();
        newDoc.setText(doc);

        //only lower case
        doc = doc.toLowerCase();
        //remove . , etc
        doc = doc.replaceAll("[^a-zöÖäÄüÜ]", " ");

        //add it to the doc list
        docs.add(newDoc);

        //for debugging only
        //Original Text
        //System.out.println("Original: " + newDoc.getText());
        //Modified Text
        //System.out.println("Modified Text: " + doc);

        //building the wordbag
        String[] temp = docToWordBag(doc);

        //for debugging only
        /*System.out.println("Length of temp after docToWordBag(): " + temp.length);
        for (String m : temp) {
        System.out.println("Element in temp: " + m);
        }*/

        for (int i = 0; i < temp.length; i++) {
            if (temp[i].contentEquals(" ") || words.contains(temp[i])) {
                //do nothing because was a stop word
                } else {
                //a word is not a stopword, than add it to the wordbag
                words.add(temp[i]);

                //for debugging only
                //System.out.println("word to wordbag added: " + temp[i]);
                //System.out.println("Length of vectors: " + vectors.size());

                //and at a vector component for a new word in the wordbag
                //for each vector in list vectors
                for (int j = 0; j < vectors.size(); j++) {
                    vectors.get(j).appendZero();
                    //for debugging only
                    //System.out.println("vectors appendZero()");
                }

            }//end of else

        }//end of for

        //make a vector of the new document
        vector newVector;
        newVector = docToVector(temp);

        //for debugging only
        //newVector.printVector();


        //for debugging only
        //System.out.println("Length of vectors after added: " + vectors.size());

        //and add it to the array list
        vectors.add(newVector);

        //return the vector
        return newVector;

    }//end of appendDocument()

    public String[] docToWordBag(String doc) {

        String[] wordbag;
        wordbag = doc.split(" ");

        return removeStopwords(wordbag);
    }

    public vector docToVector(String[] doc) {
        vector v = new vector(words.size());
        double d = 0.0;

        //go through doc and count the element of the list
        int j = 0;
        while (j < words.size()) {

            //for debugging only
            //System.out.println("Lengths of wordbag: " + words.size());

            for (int i = 0; i < doc.length; i++) {
                String temp = words.get(j);
                if (doc[i].contentEquals(temp)) {
                    //one element found in doc thats in the wordbag
                    d++;

                    //for debugging only
                    //System.out.println("if of docToVector()");

                } else {
                    //no element found thats in the wordbag
                    //for debugging only
                    //System.out.println("else of docToVector()");
                }

            }//end of for

            v.setComponent(j, d);
            j++;
            //set the counter to 0.0
            d = 0.0;
        }//end of while
        return v;
    }//end of docToVector()

    protected String[] removeStopwords(String[] wordbag) {
        //for debugging only
        //System.out.println("Length of wordbag: " + wordbag.length);

        for (int i = 0; i < wordbag.length; i++) {
            for (int j = 0; j < stopWords.length; j++) {
                if (wordbag[i].equals(stopWords[j])) {
                    wordbag[i] = " ";

                    //for debugging only
                    //System.out.println("if of removeStopwords()" + i + j);
                } else {
                    //do nothing
                    //for debugging only
                    //System.out.println("else of removeStopwords()" + i + j);
                }
            }//end of stopwords-for()

        }//end of wordbag-for()

        return wordbag;
    }//end of removeStopwords()

    public double angle(int docID1, int docID2) {
        return Math.toDegrees(vectors.get(docID1).angle(searchVectors.get(docID2)));
    }

    public double cosineSim(int docID1, int docID2) {
        return vectors.get(docID1).cosineSim(searchVectors.get(docID2));
    }

    public double scalarproduct(int docID1, int docID2) {
        return vectors.get(docID1).scalarproduct(searchVectors.get(docID2));
    }

    public double distance(int docID1, int docID2) {
        return vectors.get(docID1).distance(searchVectors.get(docID2));
    }

    public double length(int docID) {
        return vectors.get(docID).length();
    }

    public void printVector(int docID) {
        vectors.get(docID).printVector();
    }

    public void printStatistics() {
        System.out.println("-----------");
        System.out.println("Statistics:");
        System.out.println("-----------");

        //list documents
        System.out.println("Documents:");
        for (int i = 0; i < docs.size(); i++) {
            System.out.println(i + 1 + ". Text: " + docs.get(i).getText());
        }
        //list words
        System.out.println("Words in wordbag:");
        for (int i = 0; i < words.size(); i++) {
            System.out.println(words.get(i));
        }
        //list vectors
        for (int i = 0; i < vectors.size(); i++) {
            System.out.print("Vector of the ");
            System.out.print(i + 1 + ".document: ");
            printVector(i);
            System.out.println("Length of the vector: " + length(i));
        }
    }//end of printStatistics

    public void search(String query) {

        mathResults = new ArrayList<result>();

        //make a new document
        document newDoc = new document();
        newDoc.setText(query);

        System.out.println("-------------------");
        System.out.println("Search informations");
        System.out.println("-------------------");

        System.out.println("Search word: " + query);
        //only lower case
        query = query.toLowerCase();
        //remove . , etc
        query.replaceAll("[^a-zäöü]", "");
        //make a vector out of the searchword
        vector vSearchword = docToVector(docToWordBag(query));

        searchDocs.add(newDoc);
        searchVectors.add(vSearchword);

        //Output
        //Vector + length
        System.out.print("Vector of the searchword: ");
        vSearchword.printVector();
        System.out.println("Length of searchword: " + vSearchword.length());
        //Mathresults
        for (int i = 0; i < vectors.size(); i++) {

            //Distance
            System.out.print("Distance to the ");
            System.out.print(i + 1 + ".document: ");
            System.out.println(Math.round(1000.0*distance(i, searchVectors.indexOf(vSearchword)))/1000.0);

        }
        for (int i = 0; i < vectors.size(); i++) {
            //Scalarproduct
            System.out.print("Scalarproduct of the ");
            System.out.print(i + 1 + ".document and the searchword: ");
            System.out.println(Math.round(1000.0*scalarproduct(i, searchVectors.indexOf(vSearchword)))/1000.0);
        }
        for (int i = 0; i < vectors.size(); i++) {
            //Angle not sorted
            System.out.print("Angle of the ");
            System.out.print(i + 1 + ".document and the searchword: ");
            System.out.println(Math.round(1000.0*angle(i, searchVectors.indexOf(vSearchword)))/1000.0);
        }
        for (int i = 0; i < vectors.size(); i++) {
            result cosineResult = new result();
            //CosineSim
            cosineResult.setDocumentID(i + 1);
            cosineResult.setResult(cosineSim(i, searchVectors.indexOf(vSearchword)));
            System.out.print("CosineSim of the ");
            System.out.print(i + 1 + ".document and the searchword: ");
            System.out.println(Math.round(1000.0*cosineSim(i, searchVectors.indexOf(vSearchword)))/1000.0);
            mathResults.add(cosineResult);
        }
        //Angle sorted by max
        System.out.println("----------------------------");
        System.out.println("Result of the search machine");
        System.out.println("----------------------------");
        System.out.println("Cosine sort by max:");
        Collections.sort(mathResults);
        //Collections.reverse(mathResults);
        int j = mathResults.size() - 1;
        //go from the end through the list
        for (int i = 0; i < mathResults.size(); i++) {
            System.out.print("CosineSim of the ");
            System.out.print(mathResults.get(j).getDocumentID() + ".document and the searchword: ");
            System.out.println(Math.round(1000.0*mathResults.get(j).getResult())/1000.0);
            j--;
        }

    }//end of search()
}//end of classVSM

Zur Implementierung verwenden wir das open source Paket "ROME".

Die Daten sollen im RSS-Format eingelesen und der Titel extrahiert werden. Die Begriffe die im Titel vorkommen sollen doppelt so stark gewertet werden.

jetzt sollen die Methoden zum Einlesen von RSS Daten programmiert werden. Als Beispiel hab ich mal die Seite hier http://feeds2.feedburner,com/bild .. das heisst ein Array von Strings mit beliebig vielen URLs soll übergeben werden.
 

Zurück
Oben