1   package eu.fbk.dkm.pikes.resources;
2   
3   import eu.fbk.utils.core.CommandLine;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.io.BufferedReader;
8   import java.io.File;
9   import java.io.FileReader;
10  import java.io.IOException;
11  import java.util.HashMap;
12  
13  /**
14   * Created by alessio on 21/05/15.
15   */
16  
17  public class SentiWordNet {
18  
19  	private static final Logger LOGGER = LoggerFactory.getLogger(SentiWordNet.class);
20  
21  	private static File path = null;
22  	private static HashMap<String, PosNegPair> values = null;
23  
24  	public static void setPath(File path) {
25  		SentiWordNet.path = path;
26  	}
27  
28  	public static void init() throws IOException {
29  		if (values == null && path != null) {
30  			values = new HashMap<>();
31  
32  			BufferedReader reader = new BufferedReader(new FileReader(path));
33  			String line;
34  			while ((line = reader.readLine()) != null) {
35  				line = line.trim();
36  				if (line.startsWith("#")) {
37  					continue;
38  				}
39  				String[] parts = line.split("\t");
40  				if (parts.length < 4) {
41  					continue;
42  				}
43  
44  				String pos = parts[0];
45  				String id = parts[1];
46  				Double posScore = Double.parseDouble(parts[2]);
47  				Double negScore = Double.parseDouble(parts[3]);
48  
49  				String wnID = id + "-" + pos;
50  
51  				values.put(wnID, new PosNegPair(posScore, negScore));
52  			}
53  			reader.close();
54  		}
55  	}
56  
57  	public static PosNegPair searchValue(String wnID) {
58  		try {
59  			init();
60  		} catch (Exception e) {
61  			// continue...
62  		}
63  
64  		if (values == null) {
65  			return null;
66  		}
67  
68  		return values.get(wnID);
69  	}
70  
71  	public static void main(String[] args) {
72  		try {
73  			final CommandLine cmd = CommandLine
74  					.parser()
75  					.withName("sentiwordnet-loader")
76  					.withHeader(
77  							"Produces NAF files, a TSV file with sentiment annotations "
78  									+ "and an HTML file with annotated sentences "
79  									+ "starting from the MPQA v.2 corpus")
80  					.withOption("i", "input", "the corpus file", "DIR",
81  							CommandLine.Type.FILE_EXISTING, true, false, true)
82  					.withLogger(LoggerFactory.getLogger("eu.fbk.fssa")).parse(args);
83  
84  			final File inputFile = cmd.getOptionValue("input", File.class);
85  
86  			SentiWordNet.setPath(inputFile);
87  			SentiWordNet.init();
88  			System.out.println(SentiWordNet.searchValue("00478311-a"));
89  
90  		} catch (final Throwable ex) {
91  			CommandLine.fail(ex);
92  		}
93  
94  	}
95  }