001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.text.nlp.sentiment.type; 031 032import java.util.HashMap; 033import java.util.HashSet; 034import java.util.Map; 035import java.util.Set; 036 037/** 038 * Encapsulate a simple sentiment that something is positive, negative or 039 * neutral. 040 * 041 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 042 * 043 */ 044public class BipolarSentiment implements Sentiment { 045 /** 046 * The states of a bipolar sentiment 047 * 048 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 049 * 050 */ 051 public static enum State { 052 /** 053 * positive sentiment 054 */ 055 POSITIVE, 056 /** 057 * negative sentiment 058 */ 059 NEGATIVE, 060 /** 061 * Neither positive nore negative, neutral sentiment (or objective for 062 * example) 063 */ 064 NEUTRAL 065 } 066 067 /** 068 * a neutral sentiment instance 069 */ 070 public static final BipolarSentiment NEUTRAL = new BipolarSentiment(State.NEUTRAL); 071 /** 072 * a negative sentiment instance 073 */ 074 public static final BipolarSentiment NEGATIVE = new BipolarSentiment(State.NEGATIVE); 075 /** 076 * a positive sentiment instance 077 */ 078 public static final BipolarSentiment POSITIVE = new BipolarSentiment(State.POSITIVE); 079 private State state; 080 081 /** 082 * Initialize sentiment as {@link State#NEUTRAL} 083 */ 084 public BipolarSentiment() { 085 this(State.NEUTRAL); 086 } 087 088 /** 089 * Instantiate the sentiment 090 * 091 * @param positive 092 */ 093 public BipolarSentiment(State positive) { 094 this.state = positive; 095 } 096 097 /** 098 * @return sentiment == NEGATIVE 099 */ 100 public boolean negative() { 101 return state == State.NEGATIVE; 102 } 103 104 /** 105 * @return positive 106 */ 107 public boolean positive() { 108 return state == State.POSITIVE; 109 } 110 111 /** 112 * @return positive 113 */ 114 public boolean neutral() { 115 return state == State.NEUTRAL; 116 } 117 118 /** 119 * @return the bipolar sentiment 120 */ 121 public State sentiment() { 122 return state; 123 } 124 125 @Override 126 public Map<String, ?> asMap() { 127 final HashMap<String, Object> ret = new HashMap<String, Object>(); 128 ret.put("state", state); 129 return ret; 130 } 131 132 @Override 133 public void fromMap(Map<String, ?> map) throws UnrecognisedMapException { 134 if (!map.containsKey("state")) { 135 throw new UnrecognisedMapException("state"); 136 } 137 this.state = (State) map.get("state"); 138 } 139 140 @Override 141 public boolean equals(Object obj) { 142 if (!(obj instanceof BipolarSentiment)) 143 return false; 144 final BipolarSentiment that = (BipolarSentiment) obj; 145 return that.state == this.state; 146 } 147 148 @Override 149 public String toString() { 150 return this.state.name(); 151 } 152 153 /** 154 * @return a list of the {@link BipolarSentiment} instances 155 */ 156 public static Set<BipolarSentiment> listBipolarSentiment() { 157 final Set<BipolarSentiment> ret = new HashSet<BipolarSentiment>(); 158 ret.add(POSITIVE); 159 ret.add(NEUTRAL); 160 ret.add(NEGATIVE); 161 return ret; 162 } 163 164}