com.paolodragone.wsn.util.LexicalContexts.java Source code

Java tutorial

Introduction

Here is the source code for com.paolodragone.wsn.util.LexicalContexts.java

Source

/*
 * Copyright Paolo Dragone 2014
 *
 * This file is part of WiktionarySemanticNetwork.
 *
 * WiktionarySemanticNetwork is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WiktionarySemanticNetwork is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WiktionarySemanticNetwork.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.paolodragone.wsn.util;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

import static com.paolodragone.util.DStrings.filterNotEmpty;
import static com.paolodragone.util.DStrings.toLowerCaseAll;
import static com.paolodragone.util.DStrings.trimAll;

/**
 * @author Paolo Dragone
 */
public class LexicalContexts {

    private static final Set<String> invalidLexicalContextsParts = ImmutableSet.of("obsolete", "dated", "informal",
            "archaic", "slang", "rare");

    public static boolean isValidLexicalContext(String lexicalContext) {
        Set<String> lexicalContextParts = getLexicalContextParts(lexicalContext);
        for (String lexicalContextPart : lexicalContextParts) {
            if (!invalidLexicalContextsParts.contains(lexicalContextPart)) {
                return true;
            }
        }
        return lexicalContextParts.stream().noneMatch(invalidLexicalContextsParts::contains);
    }

    public static Set<String> getLexicalContextParts(String lexicalContext) {
        return filterNotEmpty(trimAll(toLowerCaseAll(Sets.newHashSet(lexicalContext.split(";")).stream())))
                .collect(Collectors.toSet());
    }

    public static void filterValidLexicalContextParts(Set<String> lexicalContextParts) {
        Iterator<String> lexicalContextPartsIterator = lexicalContextParts.iterator();
        while (lexicalContextPartsIterator.hasNext()) {
            String lexicalContextPart = lexicalContextPartsIterator.next();
            if (invalidLexicalContextsParts.contains(lexicalContextPart)) {
                lexicalContextPartsIterator.remove();
            }
        }
    }

    private LexicalContexts() {
    }
}