1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.genericevents.commands; |
---|
16 | |
---|
17 | import java.io.BufferedReader; |
---|
18 | import java.io.File; |
---|
19 | import java.io.FileInputStream; |
---|
20 | import java.io.FileNotFoundException; |
---|
21 | import java.io.IOException; |
---|
22 | import java.io.InputStreamReader; |
---|
23 | import java.text.DateFormat; |
---|
24 | import java.text.ParseException; |
---|
25 | import java.text.SimpleDateFormat; |
---|
26 | import java.util.ArrayList; |
---|
27 | import java.util.HashMap; |
---|
28 | import java.util.List; |
---|
29 | import java.util.Map; |
---|
30 | |
---|
31 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
32 | import de.ugoe.cs.autoquest.eventcore.StringEventType; |
---|
33 | import de.ugoe.cs.autoquest.plugin.genericevents.eventCore.GenericEventTarget; |
---|
34 | import de.ugoe.cs.autoquest.plugin.genericevents.eventCore.GenericEventTargetSpec; |
---|
35 | import de.ugoe.cs.util.console.Console; |
---|
36 | |
---|
37 | /** |
---|
38 | * <p> |
---|
39 | * TODO comment |
---|
40 | * </p> |
---|
41 | * |
---|
42 | * @author Patrick Harms |
---|
43 | */ |
---|
44 | public class SogouQDataFileParser { |
---|
45 | |
---|
46 | /** */ |
---|
47 | private static DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss"); |
---|
48 | |
---|
49 | /** |
---|
50 | * |
---|
51 | */ |
---|
52 | Map<String, List<Event>> parseFile(File file, |
---|
53 | boolean hasTimestamp, |
---|
54 | boolean ignoreQuery, |
---|
55 | boolean compareDomainOnly) |
---|
56 | { |
---|
57 | Console.println("reading file " + file); |
---|
58 | Map<String, List<Event>> userSessions = new HashMap<>(); |
---|
59 | try { |
---|
60 | BufferedReader reader = new BufferedReader |
---|
61 | (new InputStreamReader(new FileInputStream(file), "GB2312")); |
---|
62 | |
---|
63 | String line = null; |
---|
64 | long lastTimeStamp = 0; |
---|
65 | |
---|
66 | int timestampIndex = 0; |
---|
67 | int userIdIndex = 1; |
---|
68 | |
---|
69 | if (!hasTimestamp) { |
---|
70 | timestampIndex = -1; |
---|
71 | userIdIndex = 0; |
---|
72 | } |
---|
73 | |
---|
74 | do { |
---|
75 | line = reader.readLine(); |
---|
76 | if (line != null) { |
---|
77 | String[] elements = line.split("\t"); |
---|
78 | |
---|
79 | String userId = elements[userIdIndex].intern(); |
---|
80 | |
---|
81 | if (hasTimestamp) { |
---|
82 | try { |
---|
83 | long timestamp = TIME_FORMAT.parse(elements[timestampIndex]).getTime(); |
---|
84 | if (timestamp > lastTimeStamp) { |
---|
85 | lastTimeStamp = timestamp; |
---|
86 | } |
---|
87 | else { |
---|
88 | lastTimeStamp++; |
---|
89 | } |
---|
90 | } |
---|
91 | catch (ParseException e) { |
---|
92 | // just ignore this and count next |
---|
93 | lastTimeStamp++; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | StringBuffer query = new StringBuffer(); |
---|
98 | |
---|
99 | for (int i = userIdIndex + 1; i < elements.length - 2; i++) { |
---|
100 | query.append(elements[i]); |
---|
101 | } |
---|
102 | |
---|
103 | String queryStr = query.toString().intern(); |
---|
104 | |
---|
105 | String selectedResultPage = |
---|
106 | elements[elements.length - 2].split(" ")[0].intern(); |
---|
107 | String selectedResultIndex = |
---|
108 | elements[elements.length - 2].split(" ")[1].intern(); |
---|
109 | String selectedResult = elements[elements.length - 1]; |
---|
110 | |
---|
111 | String fullSelectedResult = selectedResult; |
---|
112 | |
---|
113 | if (compareDomainOnly) { |
---|
114 | int index = selectedResult.indexOf("://"); |
---|
115 | // ensure with the second condition, that we do not match something in the |
---|
116 | // url query |
---|
117 | if ((index >= 0) && (index < 15)) { |
---|
118 | selectedResult = selectedResult.substring(index + 3); |
---|
119 | } |
---|
120 | |
---|
121 | index = selectedResult.indexOf("/"); |
---|
122 | if (index > 0) { |
---|
123 | selectedResult = selectedResult.substring(0, index); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | selectedResult = selectedResult.intern(); |
---|
128 | |
---|
129 | Event event; |
---|
130 | |
---|
131 | GenericEventTargetSpec spec = new GenericEventTargetSpec(selectedResult, null); |
---|
132 | |
---|
133 | if (!ignoreQuery) { |
---|
134 | event = new Event(new StringEventType("query for " + queryStr), |
---|
135 | new GenericEventTarget(spec, null)); |
---|
136 | } |
---|
137 | else { |
---|
138 | event = new Event(new StringEventType("query"), |
---|
139 | new GenericEventTarget(spec, null)); |
---|
140 | } |
---|
141 | |
---|
142 | event.setTimestamp(lastTimeStamp); |
---|
143 | event.setParameter("userId".intern(), userId); |
---|
144 | event.setParameter("query".intern(), queryStr); |
---|
145 | event.setParameter("selectedResultPage".intern(), selectedResultPage); |
---|
146 | event.setParameter("selectedResultIndex".intern(), selectedResultIndex); |
---|
147 | event.setParameter("selectedResult".intern(), fullSelectedResult); |
---|
148 | |
---|
149 | List<Event> session = userSessions.get(userId); |
---|
150 | |
---|
151 | if (session == null) { |
---|
152 | session = new ArrayList<>(); |
---|
153 | userSessions.put(userId, session); |
---|
154 | } |
---|
155 | |
---|
156 | session.add(event); |
---|
157 | } |
---|
158 | } |
---|
159 | while (line != null); |
---|
160 | |
---|
161 | reader.close(); |
---|
162 | } |
---|
163 | catch (FileNotFoundException e) { |
---|
164 | Console.printerrln("could not read " + file); |
---|
165 | Console.logException(e); |
---|
166 | return null; |
---|
167 | } |
---|
168 | catch (IOException e) { |
---|
169 | Console.printerrln("problem while reading a line from " + file); |
---|
170 | Console.logException(e); |
---|
171 | return null; |
---|
172 | } |
---|
173 | |
---|
174 | return userSessions; |
---|
175 | } |
---|
176 | } |
---|