001 /* 002 NGramJ - n-gram based text classification 003 Copyright (C) 2001 Frank S. Nestel (frank at spieleck.de) 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as published 007 by the Free Software Foundation; either version 2.1 of the License, or 008 (at your option) any later version. 009 010 This program is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 GNU General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public License 016 along with this program (lesser.txt); if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 018 */ 019 020 package de.spieleck.app.ngramj; 021 022 import java.lang.Comparable; 023 024 /** 025 * An n-gram attached with a counter. 026 * 027 * @author Frank S. Nestel 028 * @author $Author: nestefan $ 029 * @version $Revision: 2 $ $Date: 2006-03-27 23:00:21 +0200 (Mo, 27 Mrz 2006) $ $Author: nestefan $ 030 */ 031 public class CountedNGram 032 // extends NGramImpl 033 implements Comparable, NGram 034 { 035 protected int count = 1; 036 protected NGram gram; 037 038 public CountedNGram(NGram ng) 039 { 040 gram = ng; 041 } 042 043 public CountedNGram(byte[] ba, int start, int len) 044 { 045 gram = NGramImpl.newNGram(ba, start, len); 046 } 047 048 public int hashCode() 049 { 050 return gram.hashCode(); 051 } 052 053 public int getCount() { return count; } 054 public void inc() { count++; } 055 056 public NGram getNGram() 057 { 058 return gram; 059 } 060 061 public int compareTo(Object e1) 062 { 063 return ((CountedNGram)e1).getCount() - getCount(); 064 } 065 066 public boolean equals(Object e1) 067 { 068 if ( e1 instanceof CountedNGram ) 069 return getNGram().equals(((CountedNGram)e1).getNGram()); 070 else if ( e1 instanceof NGram ) 071 return e1.equals(getNGram()); 072 return false; 073 } 074 075 public String toString() 076 { 077 return getNGram().toString()+"["+getCount()+"]"; 078 } 079 080 public int getSize() 081 { 082 return getNGram().getSize(); 083 } 084 085 /** 086 * Return a single byte of the NGram. 087 * @throws ArrayIndexOutOfBoundException (implicitly) 088 */ 089 public int getByte(int pos) 090 { 091 return getNGram().getByte(pos); 092 } 093 094 /** 095 * Compare a ngram to a bunch of bytes 096 */ 097 public boolean equals(byte[] bytes, int start, int length) 098 { 099 return getNGram().equals(bytes, start, length); 100 } 101 102 /** 103 * Hand out a special representation of yourself 104 */ 105 public NGramImpl getNGramImpl() 106 { 107 return getNGram().getNGramImpl(); 108 } 109 }