Changeset 6715


Ignore:
Timestamp:
10/30/15 17:19:52 (9 years ago)
Author:
davor.ostojic@oeaw.ac.at
Message:

cross-mapping
vocabulary-entry instead of invertedMap

Location:
vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/AvailabilityPostProcessor.java

    r6379 r6715  
    1010 */
    1111public class AvailabilityPostProcessor extends PostProcessorsWithVocabularyMap {
    12 
    13         private static Map<String, String> availabilityMap;
    14    
     12       
    1513        private static final Integer MAX_LENGTH = 20;
    1614    private static final String OTHER_VALUE = "Other";
     
    1917    @Override
    2018    public List<String> process(final String value) {
    21         String result = value;
    2219        List<String> resultList = new ArrayList<String>();
    23 
    24         if (getVocabularyMap().containsKey(value)) {
    25             resultList.add(getVocabularyMap().get(value));
    26         } else {
    27             if (result.length() > MAX_LENGTH) {
    28                 resultList.add(OTHER_VALUE);
    29             } else {
    30                 resultList.add(result.trim());
    31             }
    32         }
     20       
     21        resultList.add(normalize(value, value.length() > MAX_LENGTH? OTHER_VALUE : value.trim()));
    3322
    3423        return resultList;
    3524    }
    36    
    37     public Map<String, String> getVocabularyMap(){
    38         if(availabilityMap == null){
    39                 availabilityMap = createControlledVocabularyMap(MetadataImporter.config.getLicenseAvailabilityMapUrl());
    40                  }
    41        
    42                  return availabilityMap;
    43     }
     25
     26
     27        @Override
     28        public String getNormalizationMapURL() {
     29                return MetadataImporter.config.getLicenseAvailabilityMapUrl();
     30        }
    4431}
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/CMDIParserVTDXML.java

    r6413 r6715  
    11package eu.clarin.cmdi.vlo.importer;
     2
     3import java.io.File;
     4import java.io.FileInputStream;
     5import java.io.IOException;
     6import java.net.URI;
     7import java.util.ArrayList;
     8import java.util.Arrays;
     9import java.util.HashMap;
     10import java.util.LinkedList;
     11import java.util.List;
     12import java.util.Map;
     13import java.util.Map.Entry;
     14import java.util.regex.Matcher;
     15import java.util.regex.Pattern;
     16
     17import org.apache.commons.io.IOUtils;
     18import org.slf4j.Logger;
     19import org.slf4j.LoggerFactory;
    220
    321import com.ximpleware.AutoPilot;
     
    826import com.ximpleware.XPathEvalException;
    927import com.ximpleware.XPathParseException;
     28
    1029import eu.clarin.cmdi.vlo.FacetConstants;
    11 import java.io.File;
    12 import java.io.FileInputStream;
    13 import java.io.IOException;
    14 import java.net.URI;
    15 import java.util.ArrayList;
    16 import java.util.List;
    17 import java.util.Map;
    18 import java.util.regex.Matcher;
    19 import java.util.regex.Pattern;
    20 import org.apache.commons.io.IOUtils;
    21 import org.slf4j.Logger;
    22 import org.slf4j.LoggerFactory;
    2330
    2431public class CMDIParserVTDXML implements CMDIDataProcessor {
     
    231238     */
    232239    private void processFacets(CMDIData cmdiData, VTDNav nav, FacetMapping facetMapping) throws VTDException {
    233         List<FacetConfiguration> facetList = facetMapping.getFacets();
     240               
     241        List<FacetConfiguration> facetList = facetMapping.getFacets();         
    234242        for (FacetConfiguration config : facetList) {
    235243            boolean matchedPattern = false;
     
    289297                continue;
    290298            }
    291 
     299           
    292300            final List<String> values = postProcess(config.getName(), value);
    293             insertFacetValues(config.getName(), values, cmdiData, languageCode, allowMultipleValues, config.isCaseInsensitive());
     301           
     302            insertFacetValues(config.getName(), values, cmdiData, languageCode, allowMultipleValues, config.isCaseInsensitive(), true);
     303           
     304            crossMap(config, value, cmdiData, languageCode);
    294305           
    295306            //add also non curated resource type
     
    304315                    derivedValues.addAll(postProcess(derivedFacet, postProcessedValue));
    305316                }
    306                 insertFacetValues(derivedFacet, derivedValues, cmdiData, languageCode, allowMultipleValues, config.isCaseInsensitive());
     317                insertFacetValues(derivedFacet, derivedValues, cmdiData, languageCode, allowMultipleValues, config.isCaseInsensitive(), true);
    307318            }
    308319
     
    330341    }
    331342
    332     private void insertFacetValues(String name, List<String> valueList, CMDIData cmdiData, String languageCode, boolean allowMultipleValues, boolean caseInsensitive) {
     343   
     344    /*
     345         * Add values to facet either they come from MD fields either from cross mapping
     346         * Advantage is given to the values from MD fields. They will be always at the begging of the list and in case
     347         * when facet doesn't allow multiple values and we already had value from cross mapping this value will be overridden
     348         *
     349         */
     350    private void insertFacetValues(String name, List<String> valueList, CMDIData cmdiData, String languageCode, boolean allowMultipleValues, boolean caseInsensitive, boolean comesFromConceptMapping) {
     351       
     352        //keep only values from original concepts, not from cross mappings
     353                if(comesFromConceptMapping && !allowMultipleValues && cmdiData.getSolrDocument().containsKey(name)){
     354                        cmdiData.getSolrDocument().remove(name);
     355                }
     356               
     357                if(!comesFromConceptMapping && !allowMultipleValues && cmdiData.getSolrDocument().containsKey(name))
     358                        return;
     359       
    333360        for (int i = 0; i < valueList.size(); i++) {
    334361            if (!allowMultipleValues && i > 0) {
     
    361388        return resultList;
    362389    }
     390   
     391    private void crossMap(FacetConfiguration config, String extractedValue, CMDIData cmdiData, String languageCode){
     392       
     393        if (postProcessors.containsKey(config.getName())){
     394            PostProcessor processor = postProcessors.get(config.getName());
     395            if(processor instanceof PostProcessorsWithVocabularyMap){
     396               
     397                List<String> facetNames = MetadataImporter.config.getAllFacetFields();
     398               
     399                Map<String, String> crossMap = ((PostProcessorsWithVocabularyMap) processor).getCrossMappings(extractedValue);
     400                for(Entry e: crossMap.entrySet()){
     401                        String toFacet = (String) e.getKey();
     402                        String value = (String) e.getValue();
     403                        for(String facetName: facetNames){
     404                                if(toFacet.toLowerCase().equals(facetName.toLowerCase())){//normalize facet name, map can contain it in any case
     405                                        insertFacetValues(facetName, Arrays.asList(value), cmdiData, languageCode, config.getAllowMultipleValues(), config.isCaseInsensitive(), false);
     406                                }
     407                        }
     408                }
     409            }
     410        }
     411    }
     412   
    363413}
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/LanguageCodePostProcessor.java

    r6379 r6715  
    4343    }
    4444   
    45     @Override
    46         protected Map<String, String> getVocabularyMap() {
    47                 if(languageNameVariantsMap == null){
    48                         languageNameVariantsMap = createControlledVocabularyMap(MetadataImporter.config.getLanguageNameVariantsUrl());
    49                 }
    50                 return languageNameVariantsMap;
     45        @Override
     46        public String getNormalizationMapURL() {
     47                return MetadataImporter.config.getLanguageNameVariantsUrl();
    5148        }
    52    
    5349
    5450    protected String extractLanguageCode(String value) {
     
    5955       
    6056        // map known language name variants to their offical name
    61         if(getVocabularyMap().containsKey(result))
    62             result = getVocabularyMap().get(result);
     57        result = normalize(result);
    6358       
    6459        // input is already ISO 639-3?
     
    9085        return result;
    9186    }
     87
    9288}
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/MetadataImporter.java

    r6502 r6715  
    8989        POST_PROCESSORS.put(FacetConstants.FIELD_CLARIN_PROFILE, new CMDIComponentProfileNamePostProcessor());
    9090        POST_PROCESSORS.put(FacetConstants.FIELD_RESOURCE_CLASS, new ResourceClassPostProcessor());
     91        POST_PROCESSORS.put(FacetConstants.FIELD_PROFILE, new ProfileNamePostProcessor());
    9192    }
    9293
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/NationalProjectPostProcessor.java

    r6509 r6715  
    2727    private final static Logger LOG = LoggerFactory.getLogger(NationalProjectPostProcessor.class);
    2828
    29     private static Map<String, String> nationalProjectMap = null;
    30     private static Map<Pattern, String> nationalProjectRegExpMap = null;
    31 
    3229    /**
    3330     * Returns the national project based on the mapping in
     
    3936    @Override
    4037    public List<String> process(String value) {
    41         String input = value.trim();
    4238        List<String> resultList = new ArrayList<String>();
    43 
    44         if (input != null && getVocabularyMap().containsKey(input)) {
    45             resultList.add(getVocabularyMap().get(input));
    46             return resultList;
    47         }
    48 
    49         for (Pattern pattern : getRegExpMapping().keySet()) {
    50             Matcher matcher = pattern.matcher(input);
    51             if (matcher.find()) {
    52                 resultList.add(getRegExpMapping().get(pattern));
    53                 return resultList;
    54             }
    55         }
    56 
    57         resultList.add("");
     39        resultList.add(normalize(value.trim(), ""));
     40       
    5841        return resultList;
    5942    }
    6043   
    61     @Override
    62         protected Map<String, String> getVocabularyMap() {
    63                 if(nationalProjectMap == null){
    64                         createControlledVocabularyMap(getMappingFileUrl());
    65                 }
    66                
    67                 return nationalProjectMap;
    68         }
    69    
    70     private Map<Pattern, String> getRegExpMapping() {
    71         if (nationalProjectRegExpMap == null) {
    72                 createControlledVocabularyMap(getMappingFileUrl());
    73         }
    74         return nationalProjectRegExpMap;
    75     }
    76    
    77    
    78         protected String getMappingFileUrl() {
    79         String projectsMappingFile = MetadataImporter.config.getNationalProjectMapping();
    8044
    81         if (projectsMappingFile.length() == 0) {
    82             // use the packaged project mapping file
    83             projectsMappingFile = "/nationalProjectsMapping.xml";
    84         }
    85        
    86         return projectsMappingFile;
    87         }
    88        
    8945        @Override
    90         protected Map<String, String> createControlledVocabularyMap(String mapUrl) {
    91                 if(nationalProjectMap == null){
    92                 nationalProjectMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    93         }
    94        
    95         if(nationalProjectRegExpMap == null){
    96                 nationalProjectRegExpMap = new HashMap<Pattern, String>();
    97         }
    98        
    99         VariantsMap map = getMappingFromFile(mapUrl);
    100                
    101         for(Entry<String, List<Variant>> entry: map.getMap().entrySet()){
    102                 for(Variant variant: entry.getValue()){
    103                         if(variant.isRegExp()){
    104                                 nationalProjectRegExpMap.put(Pattern.compile(variant.getValue()), entry.getKey());
    105                         }else{
    106                                 nationalProjectMap.put(variant.getValue(), entry.getKey());
    107                         }                               
    108                 }
    109         }
    110        
    111         return nationalProjectMap;
     46        public String getNormalizationMapURL() {
     47                return MetadataImporter.config.getNationalProjectMapping();
    11248        }
    11349   
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/OrganisationPostProcessor.java

    r6379 r6715  
    2323        String[] splitArray = normalizeInputString(value).split(";");
    2424        for (int i = 0; i < splitArray.length; i++) {
    25             String orgaName = splitArray[i];
    26             if (getVocabularyMap().containsKey(normalizeVariant(orgaName))) {
    27                 splitArray[i] = getVocabularyMap().get(normalizeVariant(orgaName));
    28             }
     25            String normalizedVal = normalize(splitArray[i], null);
     26            if(normalizedVal != null)
     27                splitArray[i] = normalizeVariant(normalizedVal);
    2928        }
    3029       
     
    3231    }
    3332   
    34     @Override
    35         protected Map<String, String> getVocabularyMap() {
    36                 if(organisationNamesMap == null){
    37                         organisationNamesMap = createControlledVocabularyMap(MetadataImporter.config.getOrganisationNamesUrl());
    38                 }
    39                 return organisationNamesMap;
     33        @Override
     34        public String getNormalizationMapURL() {
     35                return MetadataImporter.config.getOrganisationNamesUrl();
    4036        }
    4137   
     
    4844    }
    4945
     46
    5047       
    5148   
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/PostProcessorsWithVocabularyMap.java

    r6616 r6715  
    22
    33import java.io.InputStream;
     4import java.util.List;
    45import java.util.Map;
    56import java.util.Map.Entry;
     
    910
    1011import eu.clarin.cmdi.vlo.pojo.VariantsMap;
     12import eu.clarin.cmdi.vlo.pojo.VocabularyEntry;
    1113import eu.clarin.cmdi.vlo.transformers.VariantsMapMarshaller;
    1214
     
    2325public abstract class PostProcessorsWithVocabularyMap implements PostProcessor{
    2426       
    25          private final static Logger LOG = LoggerFactory.getLogger(PostProcessorsWithVocabularyMap.class);
    26                  
     27         private final static Logger _logger = LoggerFactory.getLogger(PostProcessorsWithVocabularyMap.class);
     28         
     29         private List<VocabularyEntry> map;
     30         
     31         public PostProcessorsWithVocabularyMap(){
     32                 VariantsMap varinatsRawMap = getMappingFromFile(getNormalizationMapURL());
     33                 map = varinatsRawMap.getMap();
     34         }
     35         
     36       
     37         /*
     38          * This method is used for normalization of facet values based on normalization maps.
     39          * In case that value is not in the map, the input value is returned.
     40          *
     41          * @param value - original value from record
     42          * @return normalized value if there is a match otherwise original value
     43          *
     44          */
     45         public String normalize(String value){
     46                 return normalize(value, value);
     47         }
     48         
    2749         
    2850         /*
    29           * returns specific static map and should call createControlledVocabularyMap
     51          * This method is used for normalization of facet values based on normalization maps.
     52          * With second parameter user can specify what to return in case when value is not in normalization map (special case for NationalProject facet).
     53          * 
     54          * @param value - original value from record
     55          * @param fallBackValue - value to be returned in case of no match
     56          * @return normalized value if there is a match otherwise returns what is specified with 2nd parameter
     57          *
    3058          */
    31          protected abstract Map<String, String> getVocabularyMap();
     59         public String normalize(String value, String fallBackValue){
     60                 int ind = map.indexOf(value);
     61                 return (ind != -1)? map.get(ind).getNormalizedValue() : fallBackValue;
     62         }
     63         
     64         public  Map<String, String> getCrossMappings(String value){
     65                 int ind = map.indexOf(value);
     66                 return (ind != -1)? map.get(ind).getCrossMap() : null;
     67         }
     68         
     69         public abstract String getNormalizationMapURL();
    3270         
    3371                 
     
    4078                 try {
    4179                       
    42                         LOG.info("Reading vocabulary file from: {}", mapUrl);
     80                        _logger.info("Reading vocabulary file from: {}", mapUrl);
    4381                    // load records from file
    4482                        // in the future this should be loaded from CLAVAS directly and the file only used as fallback
     
    5290         }
    5391         
    54          // for debug purposes
     92         
     93         
     94         // for debug
    5595         public void printMap(){
    56                  LOG.info("map contains {} entries", getVocabularyMap().size());
    57                  for(Entry<String, String> e: getVocabularyMap().entrySet()){
    58                          byte bytes[] = e.getKey().getBytes();
    59                          StringBuilder sb = new StringBuilder();
    60                             for (byte b : bytes)
    61                                 sb.append(String.format("%02X ", b));
    62                            
    63                          LOG.info("Key <{} {}> will be mapped to <{}>", e.getKey(), sb.toString(), e.getValue());
    64                  }
    65                  
    66                  String s = "TÃŒbingen Curated Resource";
    67                  byte bytes[] = s.getBytes();
    68                  StringBuilder sb = new StringBuilder();
    69              for (byte b : bytes)
    70                  sb.append(String.format("%02X ", b));
    71              LOG.info("{} {}", s, sb.toString());
    72                    
    73                  
     96                 _logger.info("map contains {} entries", map.size());
     97                 for(VocabularyEntry entry: map)
     98                         _logger.info(entry.toString());
     99               
    74100         }
    75          
    76        
    77 
    78 
    79101}
  • vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/ResourceClassPostProcessor.java

    r6400 r6715  
    1010
    1111public class ResourceClassPostProcessor extends PostProcessorsWithVocabularyMap {
    12         private static Map<String, String> resourceTypeMap;
    1312       
    1413        /**
     
    3130        }
    3231               
    33         if (getVocabularyMap().containsKey(value))
    34                 result = getVocabularyMap().get(value);
     32        result = normalize(value);
    3533           
    3634                return Arrays.asList(result);
     
    3836
    3937        @Override
    40         protected Map<String, String> getVocabularyMap() {
    41                 if(resourceTypeMap == null){
    42                         resourceTypeMap = createControlledVocabularyMap(MetadataImporter.config.getResourceClassMapUrl());
    43                  }
    44        
    45                  return resourceTypeMap;
     38        public String getNormalizationMapURL() {
     39                return MetadataImporter.config.getResourceClassMapUrl();
    4640        }
    4741}
Note: See TracChangeset for help on using the changeset viewer.