All Downloads are FREE. Search and download functionalities are using the official Maven repository.

smile.nlp.NGram Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2025 Haifeng Li. All rights reserved.
 *
 * Smile 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.
 *
 * Smile 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 Smile. If not, see .
 */
package smile.nlp;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * An n-gram is a contiguous sequence of n words from a given sequence of text.
 * An n-gram of size 1 is referred to as an unigram; size 2 is a bigram;
 * size 3 is a trigram.
 *
 * @author Haifeng Li
 */
public class NGram {

    /**
     * Immutable word sequences.
     */
    public final String[] words;

    /**
     * Constructor.
     * @param words the n-gram word sequence.
     */
    public NGram(String[] words) {
        this.words = words;
    }

    @Override
    public String toString() {
        return Arrays.stream(words).collect(Collectors.joining(", ", "[", "]"));
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(words);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof NGram other)) {
            return false;
        }

        return Arrays.equals(words, other.words);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy