001 // Copyright 2012, 2013 Brad Block, Pawjaw, LLC. (an Ohio Limited Liability Company) 002 // 003 // This file is part of JBTCRF. 004 // 005 // JBTCRF is free software: you can redistribute it and/or modify 006 // it under the terms of the GNU General Public License as published by 007 // the Free Software Foundation, either version 3 of the License, or 008 // (at your option) any later version. 009 // 010 // JBTCRF 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 General Public License 016 // along with JBTCRF. If not, see <http://www.gnu.org/licenses/>. 017 018 package com.pawjaw.classification.crf.lmcbt.sequences; 019 020 import com.pawjaw.classification.crf.lmcbt.configurations.Configuration; 021 import com.pawjaw.classification.crf.lmcbt.points.Point; 022 import java.util.List; 023 024 public class ExpandedPointSequence { 025 private Point[] sp_t; 026 027 public void set(Point[] sp_t) { 028 this.sp_t = sp_t; 029 } 030 031 public int length() { 032 return sp_t.length; 033 } 034 035 public int expandedLength(Configuration c) { 036 return sp_t.length * c.label_count_including_start_label; 037 } 038 039 public boolean hasTrueFeature(Configuration c, int sequence_position, int previous_label, int feature_index) { 040 if(feature_index < c.label_count_including_start_label) 041 return previous_label == feature_index; 042 sequence_position += (feature_index - c.label_count_including_start_label) / 043 c.point_features - c.window_radius; 044 if(sequence_position < 0 || sequence_position >= sp_t.length) 045 return false; 046 else if(feature_index < c.valid_window_offset) 047 return sp_t[sequence_position].hasTrueFeature(c, 048 (feature_index - c.label_count_including_start_label) % 049 c.point_features); 050 else 051 return true; 052 } 053 054 public boolean hasTrueFeature(Configuration c, int expanded_sequence_position, int feature_index) { 055 return hasTrueFeature(c, expanded_sequence_position / c.label_count_including_start_label, 056 expanded_sequence_position % c.label_count_including_start_label, 057 feature_index); 058 } 059 060 public List<Integer> getTrueFeatures(Configuration c, int sequence_position, int previous_label, List<Integer> feature_indexes) { 061 int t, T = length(), f, o, fo = c.label_count_including_start_label, w, W = c.window_radius * 2 + 1; 062 feature_indexes.clear(); 063 feature_indexes.add(previous_label); 064 sequence_position -= c.window_radius; 065 for(w = 0;w < W;w++) { 066 if((t = sequence_position + w) >= 0 && t < T) { 067 o = 0; 068 while((f = sp_t[t].nextTrueFeature(c, o++)) >= 0) 069 feature_indexes.add(f + fo); 070 } 071 fo += c.point_features; 072 } 073 for(w = 0;w < W;w++) 074 if((t = sequence_position + w) >= 0 && t < T) 075 feature_indexes.add(fo + w); 076 return feature_indexes; 077 } 078 079 public List<Integer> getTrueFeatures(Configuration c, int expanded_sequence_position, List<Integer> feature_indexes) { 080 return getTrueFeatures(c, expanded_sequence_position / c.label_count_including_start_label, 081 expanded_sequence_position % c.label_count_including_start_label, 082 feature_indexes); 083 } 084 }