001/*
002 * Copyright © 2012, 2013, 2014 Royal Botanic Gardens, Kew.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
005 *
006 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
009 */
010package org.kew.rmf.transformers.authors;
011
012import java.util.ArrayList;
013import java.util.List;
014import java.util.Locale;
015import java.util.regex.Pattern;
016
017import org.apache.commons.lang3.StringUtils;
018import org.kew.rmf.transformers.SqueezeWhitespaceTransformer;
019import org.kew.rmf.transformers.StringShrinkerTransformer;
020import org.kew.rmf.transformers.StripNonAlphanumericCharactersTransformer;
021import org.kew.rmf.transformers.Transformer;
022
023/**
024 * This transformer tries to identify <strong>all</strong> authors (accepts publishing-,
025 * basionym-, ex-, in-) of plant names in a string and returns a string where
026 * each of their surnames are shrunk/cropped to a length
027 * of {@link #setShrinkTo(int)}.
028 */
029/*
030 * For examples of standard usage and corner cases see {@link ShrunkAuthorsTest}
031 */
032public class ShrunkAuthors implements Transformer {
033
034        private DotFDotCleaner dotFDotCleaner = new DotFDotCleaner();
035        private SurnameExtractor surnameExtractor = new SurnameExtractor();
036
037        private StripBasionymAuthorTransformer stripBasionymAuthor =new StripBasionymAuthorTransformer();
038        private StripPublishingAuthorTransformer stripPublishingAuthor = new StripPublishingAuthorTransformer();
039
040        private Pattern inEx = Pattern.compile(StripExAuthorTransformer.EX_MARKER_REGEX + "|" + StripInAuthorTransformer.IN_MARKER_REGEX);
041
042        private SqueezeWhitespaceTransformer shrinkWhitespace = new SqueezeWhitespaceTransformer();
043        private StripNonAlphanumericCharactersTransformer safeStripNonAlphanumerics = new StripNonAlphanumericCharactersTransformer();
044
045        private StringShrinkerTransformer stringShrinker = new StringShrinkerTransformer();
046
047        @Override
048        public String transform(String s) {
049                if (s == null) return null;
050
051                s = dotFDotCleaner.transform(s);
052                s = surnameExtractor.transform(s);
053
054                String pub = stripBasionymAuthor.transform(s);
055                String bas = stripPublishingAuthor.transform(s);
056
057                List<String> surnames = new ArrayList<>();
058                for (String authors : new String[] {bas, pub}) {
059                        authors = inEx.matcher(authors).replaceAll(" ");
060                        authors = shrinkWhitespace.transform(authors);
061                        authors = safeStripNonAlphanumerics.transform(authors);
062                        for (String author : authors.split(" ")) {
063                                // shrink each identified author surname to shrinkTo if set
064                                author = stringShrinker.transform(author);
065                                author = author.trim();
066                                if (author.length() > 0) {
067                                        surnames.add(author);
068                                }
069                        }
070                }
071                return StringUtils.join(surnames, " ").toLowerCase(Locale.ENGLISH);
072        }
073
074        public Integer getShrinkTo() {
075                return stringShrinker.getLength();
076        }
077        public void setShrinkTo(int shrinkTo) {
078                stringShrinker.setLength(shrinkTo);
079        }
080}