/*
 * Created on May 11, 2005
 *
 */
package com.kellegous.mas864.clusterss;

import java.util.HashSet;

import com.kellegous.mas864.clusterss.nlp.WordExtractor;

/**
 * @author knorton
 * Copyright (C) 2005 MIT Media Lab, All Right Reserved.
 */
public class Article {
    private String 	id;
    private String 	title;
    private String 	contents;
    private Feed		feed;
    private HashSet	dict;
    
    public Article(Feed feed, String id, String title, String contents) {
        this.id = id;
        this.title = title;
        this.contents = Article.stripHTML(contents);
        this.feed = feed;
        this.index();
    }
    
    public Feed getFeed() {
        return this.feed;
    }
    
    public String getID() {
        return this.id;
    }
    
    public String getTitle() {
        return this.title;
    }
    
    public String getContents() {
        return this.contents;
    }
    
    public HashSet getVector() {
        return this.dict;
    }
    
    private void index() {
        this.dict = WordExtractor.extractAsSet(this.contents);
    }
    
    public static String stripHTML(String content) {
        int bi=0,ei=0;
        StringBuffer sb = new StringBuffer(content.length());
        while (ei>=0) {
            ei = content.indexOf('<',ei);
            if (ei<0) {
                if (bi<content.length())
                    sb.append(content.substring((bi==0)?0:bi+1));
                break;
            }
            sb.append(content.substring(bi,ei));
            ei = content.indexOf('>',ei);
            bi = ei+1;
        }
        return stripEntities(sb.toString()).trim();
    }
    
    private static String stripEntities(String content) {
        int bi=0,ei=0;
        StringBuffer sb = new StringBuffer(content.length());
        while (ei>=0) {
            ei = content.indexOf('&',ei);
            if (ei<0) {
                if (bi<content.length())
                    sb.append(content.substring((bi==0)?0:bi+1));
                break;
            }
            sb.append(content.substring(bi,ei));
            ei = content.indexOf(';',ei);
            bi = ei+1;
        }
        return sb.toString();
    }
    
    public String toString() {
        return this.title;
    }
}
