1   package eu.fbk.dkm.pikes.resources;
2   
3   import com.google.common.base.Objects;
4   import com.google.common.base.Preconditions;
5   import com.google.common.collect.ImmutableMap;
6   import com.google.common.collect.Multimap;
7   import eu.fbk.rdfpro.util.Environment;
8   import ixa.kaflib.KAFDocument;
9   import ixa.kaflib.Term;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import javax.annotation.Nullable;
14  import java.io.File;
15  import java.util.Collection;
16  import java.util.Map;
17  
18  public final class Intensities extends Lexicon<Intensities.Lexeme> {
19  
20  	private static final Logger LOGGER = LoggerFactory.getLogger(Intensities.class);
21  
22  	private static Intensities instance = null;
23  
24  	public static synchronized void setInstance(@Nullable final Intensities instance) {
25  		Intensities.instance = instance;
26  	}
27  
28  	public static synchronized Intensities getInstance() {
29  		if (instance == null) {
30  			final String location = Objects.firstNonNull(
31  					Environment.getProperty("intensities.home"),
32  					"intensities.tsv");
33  			try {
34  				instance = Lexicon.readFrom(Intensities.class, Lexeme.class, location);
35  			} catch (final Throwable ex) {
36  				throw new Error("Could not read default intensity lexicon at " + location, ex);
37  			}
38  		}
39  		return instance;
40  	}
41  
42  	public static void main(final String... args) throws Exception {
43  
44  		Intensities lexicon = getInstance();
45  
46  		String fileName = "/Users/alessio/Documents/Resources/database.eu.fbk.dkm.pikes.resources.mpqa.2.0/NAF-parsed/20010907_00.16.28-8800.naf";
47  		KAFDocument document = KAFDocument.createFromFile(new File(fileName));
48  		Multimap<Term, Lexeme> lexemeMultimap = lexicon.match(document, document.getTerms());
49  
50  		for (Term term : lexemeMultimap.keys()) {
51  			Collection<Lexeme> lexemes = lexemeMultimap.get(term);
52  			for (Lexeme lexeme : lexemes) {
53  				System.out.println(lexeme);
54  			}
55  		}
56  
57  //        try {
58  //            final CommandLine cmd = CommandLine
59  //                    .parser()
60  //                    .withName("index-subjectivity-lexicon")
61  //                    .withHeader("Processes the original file of the subjectivity lexicon, " //
62  //                            + "producing a TSV file with an indexed version of it that can " //
63  //                            + "be used with the eu.fbk.dkm.pikes.resources.SubjectivityLexicon Java API class.")
64  //                    .withOption("i", "input", "the input file name", "FILE", Type.FILE_EXISTING,
65  //                            true, false, true)
66  //                    .withOption("o", "output", "the output file name", "FILE", Type.FILE, true,
67  //                            false, true) //
68  //                    .withLogger(LoggerFactory.getLogger("eu.fbk")) //
69  //                    .parse(args);
70  //
71  //            final File inputFile = cmd.getOptionValue("i", File.class);
72  //            final File outputFile = cmd.getOptionValue("o", File.class);
73  //
74  //            final eu.fbk.dkm.pikes.resources.Intensities lexicon = index(inputFile.getAbsolutePath());
75  //            lexicon.writeTo(outputFile.getAbsolutePath());
76  //
77  //        } catch (final Throwable ex) {
78  //            CommandLine.fail(ex);
79  //        }
80  	}
81  
82  	public Intensities(final Iterable<Lexeme> lexemes) {
83  		super(lexemes);
84  	}
85  
86  	public static final class Lexeme extends Lexicon.Lexeme {
87  
88  		private final Type type;
89  
90  		public Lexeme(final String id, final Iterable<Token> tokens, final Type type) {
91  			super(id, tokens);
92  			this.type = Preconditions.checkNotNull(type);
93  		}
94  
95  		protected Lexeme(final String id, final Iterable<Token> tokens,
96  						 final Map<String, String> properties) {
97  			// for use with reflection
98  			this(id, tokens, Type.valueOf(properties.get("type").toUpperCase()));
99  		}
100 
101 		@Override
102 		protected Map<String, String> getProperties() {
103 			return ImmutableMap.of("type", this.type.toString());
104 		}
105 
106 		public Type getType() {
107 			return this.type;
108 		}
109 
110 	}
111 
112 	public enum Type {
113 
114 		INTENSIFIER,
115 		DIMINISHER,
116 		MODAL
117 
118 	}
119 
120 }