-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Categories and attributes config assistance
- Loading branch information
goulven
authored and
goulven
committed
Dec 2, 2024
1 parent
4a08a2e
commit d1316b5
Showing
5 changed files
with
254 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
api/src/main/java/org/open4goods/api/model/AttributesStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.open4goods.api.model; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.open4goods.commons.model.product.ProductAttribute; | ||
|
||
public class AttributesStats { | ||
|
||
/** | ||
* Number of hits for this attribute | ||
*/ | ||
private Integer hits = 0; | ||
|
||
/** | ||
* The values, with associated popularity | ||
*/ | ||
private Map<String,Integer> values = new LinkedHashMap<>(); | ||
|
||
public Map<String, Integer> getValues() { | ||
return values; | ||
} | ||
|
||
public void setValues(Map<String, Integer> values) { | ||
this.values = values; | ||
} | ||
|
||
/** | ||
* Increments the stats | ||
* @param key the key for the attribute | ||
* @param value the ProductAttribute object | ||
*/ | ||
public void process(String key, ProductAttribute value) { | ||
hits++; | ||
|
||
// Incrementing stats by value (value.getValue()) | ||
// Assuming value.getValue() returns a string that represents the attribute value. | ||
if (value != null && value.getValue() != null) { | ||
values.compute(value.getValue(), (k, v) -> v == null ? 1 : v + 1); | ||
} | ||
} | ||
|
||
public Integer getHits() { | ||
return hits; | ||
} | ||
|
||
public void setHits(Integer hits) { | ||
this.hits = hits; | ||
} | ||
|
||
/** | ||
* Sorts the values map by integer value in descending order. | ||
*/ | ||
public void sort() { | ||
values = values.entrySet() | ||
.stream() | ||
.sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue())) // Descending order | ||
.collect(LinkedHashMap::new, // Collect into a LinkedHashMap to maintain order | ||
(map, entry) -> map.put(entry.getKey(), entry.getValue()), | ||
LinkedHashMap::putAll); | ||
} | ||
|
||
|
||
|
||
|
||
} |
108 changes: 108 additions & 0 deletions
108
api/src/main/java/org/open4goods/api/model/VerticalAttributesStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.open4goods.api.model; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.open4goods.commons.model.product.ProductAttribute; | ||
|
||
/** | ||
* Repesents attributes stats for a vertical | ||
*/ | ||
public class VerticalAttributesStats { | ||
|
||
private Integer totalItems = 0; | ||
|
||
/** | ||
* Stats by attribute name | ||
*/ | ||
private Map<String, AttributesStats> stats = new LinkedHashMap<>(); | ||
|
||
public Integer getTotalItems() { | ||
return totalItems; | ||
} | ||
|
||
public void setTotalItems(Integer totalItems) { | ||
this.totalItems = totalItems; | ||
} | ||
|
||
public Map<String, AttributesStats> getStats() { | ||
return stats; | ||
} | ||
|
||
public void setStats(Map<String, AttributesStats> stats) { | ||
this.stats = stats; | ||
} | ||
|
||
/** | ||
* Increments the stats with provided attributes | ||
* @param all | ||
*/ | ||
public void process(Map<String, ProductAttribute> attrs) { | ||
totalItems++; | ||
|
||
for (Entry<String, ProductAttribute> attrEntry : attrs.entrySet()) { | ||
AttributesStats as = stats.get(attrEntry.getKey()); | ||
|
||
if (null == as) { | ||
as = new AttributesStats(); | ||
} | ||
|
||
as.process(attrEntry.getKey(), attrEntry.getValue()); | ||
stats.put(attrEntry.getKey(), as); | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Sort the data for better restitution | ||
*/ | ||
public void sort() { | ||
|
||
// Sorting the attribute names | ||
stats = stats.entrySet() | ||
.stream() | ||
.sorted((e1, e2) -> Integer.compare(e2.getValue().getHits(), e1.getValue().getHits())) // Descending order | ||
.collect(LinkedHashMap::new, // Collect into a LinkedHashMap to maintain order | ||
(map, entry) -> map.put(entry.getKey(), entry.getValue()), | ||
LinkedHashMap::putAll); | ||
|
||
// Sorting the attributes values frequency | ||
stats.values().forEach(e -> { | ||
e.sort(); | ||
}); | ||
|
||
|
||
} | ||
|
||
/** | ||
* Cleaning by evicting dummy / noisy values | ||
*/ | ||
public void clean() { | ||
// Removing items where hits = number of attributes (means 1 to 1, like descriptif, title,gtin...) | ||
Set<String> toRemove = stats.entrySet().stream().filter(e->e.getValue().getHits() == e.getValue().getValues().size() ).map(e->e.getKey()) | ||
.collect(Collectors.toSet()); | ||
|
||
|
||
// Removing if only low keys | ||
stats.entrySet().stream().forEach(e-> { | ||
Integer max = e.getValue().getValues().values().stream().max(Integer::compare).orElse(0); | ||
// TODO(p3,conf) : From conf | ||
if (max < 5) { | ||
toRemove.add(e.getKey()); | ||
} | ||
}); | ||
|
||
toRemove.forEach(e-> { | ||
stats.remove(e); | ||
}); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters