Skip to content

Commit

Permalink
feat: Visual Semantics metric #54
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomrebelo committed Jul 25, 2023
1 parent 02942f5 commit d1f59b5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/@evoposter/evaluator/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import * as Legibility from "./metrics/Legibility.mjs";
import * as GridAppropriateSize from "./metrics/GridAppropriateSize.mjs";
import * as LayoutSemantics from "./metrics/LayoutSemantics.mjs";
import * as VisualSemantics from "./metrics/VisualSemantics.mjs";

export const info = () => {
console.log ("Evaluator working");
Expand All @@ -18,5 +19,6 @@ export const info = () => {
export const legibility = Legibility.compute;
export const gridAppropriateSize = GridAppropriateSize.compute;
export const layoutSemantics = LayoutSemantics.compute;
export const visualSemantics = VisualSemantics.compute;

export { info as default };
1 change: 0 additions & 1 deletion src/@evoposter/evaluator/src/metrics/LayoutSemantics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/

import {arrMean, arrSum} from "../utils.js";
import {sumArr} from "../../../../client/utils.js";


let dist = null;
Expand Down
102 changes: 102 additions & 0 deletions src/@evoposter/evaluator/src/metrics/VisualSemantics.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Visual Semantics
*
* Measure the appropriate of the visual features
* based on the importance of the content
* it considers that most important parts of content
* are those have more emotions related
*
* return the mean of difference between all textboxes,
* a value between 1 (good) and 0 (bad)
*
*
* Sérgio M. Rebelo
* CDV lab. (CMS, CISUC, Portugal)
* srebelo[at]dei.uc.pt
*
* v1.0.0 November 2023
*/
import {arrMax, arrMean, arrMin, map} from "../utils.js";

// by tradition, use more that a method to emphasize the text is considered typecrime
// this method enables turn on/off this, using the param allowMultiple
export const compute = (textboxes, dist, allowMultiple = false) => {
const fontWeight = 1-checkDifferenceVariableFeature(textboxes.map((b) => b["weight"]), dist.map((e) => e[3]));
const fontStretch = 1-checkDifferenceVariableFeature(textboxes.map((b) => b["font-stretch"]), dist.map((e) => e[3]));

console.log (`fontWeight=`, fontWeight);
console.log (`fontStretch=`, fontStretch);

// type design


// way of combine and only checks one
return [fontWeight, fontStretch];
}

const checkDifferenceVariableFeature = (currentFeatures, dist) => {

// max feature range
const maxFeature = arrMax(currentFeatures);
const minFeature = arrMin(currentFeatures);
const range = Math.abs(maxFeature - minFeature);

// semantic data range
const maxSemantic = arrMax(dist);
const minSemantic = arrMin(dist);

// create target feature values
const target = dist.map((e) => {
// The most neutral sentence are the most regular
let v = map(e, minSemantic, maxSemantic, 0, range);
v = v > range ? range : v;
v = v < 0 ? 0 : v;
return v;
});

// distance to target
const current = [];
for (let i in currentFeatures) {
let w = currentFeatures[i];
let currentDistance = Math.abs(w - minFeature);
let dif = Math.abs(currentDistance - target[i]);
current.push(dif);
}

return arrMean(current)/range;
}


// check if the important part of text are typeset in a bolder typeface
const checkWeightFeature = (current, dist) => {

// semantic data range
const maxSemantic = arrMax(dist);
const minSemantic = arrMin(dist);

// max feature range
const maxFeature = arrMax(current);
const minFeature = arrMin(current);

// create target feature values
const probs = dist.map((e) => {
let v = e[3]/maxSemantic * maxFeature;
if (e[3] === minSemantic) {
v = minFeature;
}
return v;
});

// calculate distance
let distance = [];
for (let i in current) {
let w = current[i]
let value = Math.abs(w-probs[i]);
distance.push(value);
}

return arrMean(distance)/Math.abs(maxFeature-minFeature);
}


export { compute as default };
8 changes: 8 additions & 0 deletions src/@evoposter/evaluator/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export const arrMean = (arr) => {
export const arrSum = (arr) => {
return arr.reduce((partialSum, a) => partialSum + a, 0);
}

export const arrMax = (arr) => {
return Math.max(...arr);
}

export const arrMin = (arr) => {
return Math.min(...arr);
}
3 changes: 3 additions & 0 deletions src/client/controllers/Poster.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ class Poster {
evaluate = async (dist) => {
this.phenotype = await this.draw();
const layoutSemantics = evaluator.layoutSemantics(this.genotype["grid"]["rows"]["l"], dist, `RELATIVE`, this.genotype["size"]);
const visualSemantics = evaluator.visualSemantics(this.genotype["textboxes"], dist);

console.log (`visualSemantics`, visualSemantics);

this.fitness = layoutSemantics;

Expand Down
4 changes: 2 additions & 2 deletions src/public/app.js

Large diffs are not rendered by default.

0 comments on commit d1f59b5

Please sign in to comment.