Project

General

Profile

1
package eu.dnetlib.pace.distance.algo;
2

    
3
import eu.dnetlib.pace.distance.ConfigurableDistanceAlgo;
4
import eu.dnetlib.pace.distance.DistanceAlgo;
5
import eu.dnetlib.pace.model.Field;
6

    
7
import java.net.MalformedURLException;
8
import java.net.URL;
9
import java.util.Map;
10

    
11
public class UrlMatcher extends ConfigurableDistanceAlgo implements DistanceAlgo {
12

    
13
    public UrlMatcher(Map<String, String> params, double weight) {
14
        super(params, weight);
15
    }
16

    
17
    @Override
18
    public double distance(Field a, Field b) {
19

    
20
        final URL urlA = asUrl(getFirstValue(a));
21
        final URL urlB = asUrl(getFirstValue(b));
22

    
23
        return urlA.getHost().equalsIgnoreCase(urlB.getHost()) ? 1.0 : 0.0;
24
    }
25

    
26
    private URL asUrl(final String value) {
27
        try {
28
            return new URL(value);
29
        } catch (MalformedURLException e) {
30
            // should not happen as checked by pace typing
31
            throw new IllegalStateException("invalid URL: " + value);
32
        }
33
    }
34

    
35
    @Override
36
    public double getWeight() {
37
        return super.getWeigth();
38
    }
39

    
40
}
(20-20/21)