Jean-Francois Leveque

Ajout des recommandations. Problème de packaging Spring. J'ai une NPE, je m'en occupe demain.

...@@ -25,6 +25,11 @@ ...@@ -25,6 +25,11 @@
25 <artifactId>spring-boot-starter-test</artifactId> 25 <artifactId>spring-boot-starter-test</artifactId>
26 <scope>test</scope> 26 <scope>test</scope>
27 </dependency> 27 </dependency>
28 + <dependency>
29 + <groupId>org.apache.commons</groupId>
30 + <artifactId>commons-csv</artifactId>
31 + <version>1.3</version>
32 + </dependency>
28 </dependencies> 33 </dependencies>
29 34
30 <build> 35 <build>
......
1 +package org.legrog.recommendation.postprocess;
2 +
3 +import org.apache.commons.csv.CSVFormat;
4 +import org.apache.commons.csv.CSVRecord;
5 +import org.slf4j.Logger;
6 +import org.slf4j.LoggerFactory;
7 +import org.springframework.beans.factory.annotation.Value;
8 +import org.springframework.boot.CommandLineRunner;
9 +import org.springframework.boot.SpringApplication;
10 +import org.springframework.boot.autoconfigure.SpringBootApplication;
11 +import org.springframework.context.annotation.Bean;
12 +
13 +import java.io.*;
14 +import java.util.HashMap;
15 +import java.util.HashSet;
16 +import java.util.Properties;
17 +import java.util.Set;
18 +
19 +@SpringBootApplication
20 +public class PostprocessingApplication {
21 +
22 + Logger logger = LoggerFactory.getLogger(getClass());
23 +
24 + @Value("${parameters.filename}")
25 + String parametersFilename;
26 + @Value("${data.dir}")
27 + String dataDir;
28 + @Value("${collectionSample.filename}")
29 + String collectionSampleFilename;
30 + @Value("${ratingSample.filename}")
31 + String ratingSampleFilename;
32 + String sampleFilename;
33 + Properties properties;
34 + Set<Long> sampleItemIds;
35 + Set<Long> sampleUserIds;
36 + HashMap<Long, Set<Long>> sampleItemUserIds;
37 +
38 + public static void main(String[] args) {
39 + SpringApplication.run(PostprocessingApplication.class, args);
40 + }
41 +
42 + @Bean
43 + public CommandLineRunner postprocess() {
44 + return (args) -> this.run();
45 + }
46 +
47 + public void run() {
48 + loadParametersProperties();
49 + loadSampleFilename();
50 + analyzeSample();
51 +
52 + }
53 +
54 + void analyzeSample() {
55 +
56 + Reader in = null;
57 + try {
58 + sampleItemIds = new HashSet<>();
59 + sampleUserIds = new HashSet<>();
60 + sampleItemUserIds = new HashMap<>();
61 + //
62 + in = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(dataDir + sampleFilename));
63 + Iterable<CSVRecord> records = CSVFormat.TDF.withFirstRecordAsHeader().parse(in);
64 + for (CSVRecord record : records) {
65 + Long itemId = Long.parseLong(record.get("itemId"));
66 + Long userId = Long.parseLong(record.get("userId"));
67 + sampleItemIds.add(itemId);
68 + sampleUserIds.add(userId);
69 + if (!sampleItemUserIds.containsKey(itemId)) {
70 + Set<Long> set = sampleItemUserIds.get(itemId);
71 +// NPE !!!
72 + set.add(userId);
73 + sampleItemUserIds.put(itemId, set);
74 + } else {
75 + Set<Long> set = new HashSet<>();
76 + set.add(userId);
77 + sampleItemUserIds.put(itemId, set);
78 + }
79 + }
80 + logger.trace("Nombre d'objets recommandables {}", sampleItemIds.size());
81 + logger.trace("Taille de la matrice item-user {}", sampleItemIds.size() * sampleUserIds.size());
82 +
83 + int sampleCoupleCount = 0;
84 + for (Long itemId : sampleItemIds) {
85 + sampleCoupleCount += sampleItemUserIds.get(itemId).size();
86 + }
87 +
88 + logger.trace("Nombre de couples item-user dans l'échantillon {}", sampleCoupleCount);
89 +
90 + } catch (IOException e) {
91 + e.printStackTrace();
92 + }
93 +
94 + }
95 +
96 + void loadSampleFilename() {
97 + if (!properties.containsKey("ratings")) {
98 + return;
99 + } else {
100 + logger.trace("ratings {}", properties.getProperty("ratings"));
101 + if (Boolean.parseBoolean(properties.getProperty("ratings"))) {
102 + sampleFilename = ratingSampleFilename;
103 + } else {
104 + sampleFilename = collectionSampleFilename;
105 + }
106 +
107 + logger.trace("sampleFilename {}", sampleFilename);
108 + }
109 + }
110 +
111 + void loadParametersProperties() {
112 +
113 + Properties properties = new Properties();
114 + InputStream in = null;
115 + try {
116 + in = this.getClass().getClassLoader().getResourceAsStream(dataDir + parametersFilename);
117 +// in = new FileInputStream(dataDir + parametersFilename);
118 + properties.load(in);
119 + in.close();
120 + } catch (FileNotFoundException e) {
121 + e.printStackTrace();
122 + } catch (IOException e) {
123 + e.printStackTrace();
124 + }
125 +
126 + this.properties = properties;
127 + }
128 +
129 +}
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<configuration>
3 +
4 + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5 + <!-- encoders are assigned the type
6 + ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
7 + <encoder>
8 + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
9 + </encoder>
10 + </appender>
11 +
12 + <logger name="org.legrog" level="DEBUG"/>
13 + <logger name="org.legrog.recommendation.postprocess" level="TRACE"/>
14 +
15 + <root level="warn">
16 + <appender-ref ref="STDOUT" />
17 + </root>
18 +</configuration>
...\ No newline at end of file ...\ No newline at end of file
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
19 <module>grog-entities</module> 19 <module>grog-entities</module>
20 <module>grog-webapp</module> 20 <module>grog-webapp</module>
21 <module>grog-db-generator</module> 21 <module>grog-db-generator</module>
22 + <module>grog-recommendation</module>
22 </modules> 23 </modules>
23 24
24 <properties> 25 <properties>
......