Commit 09d4e3e8 authored by Panagiotis Papadakos's avatar Panagiotis Papadakos
Browse files

[Stemmer] Add english stemmer and stopwords from mitos code.

parent 0fbc00c4
/*
* bias goggles - Measuring the bias of web domains through the eyes of users
*
* FOUNDATION OF RESEARCH AND TECHNOLOGY - HELLAS (FORTH-ICS)
*
* INFORMATION SYSTEMS LABORATORY (ISL)
*
* http://www.ics.forth.gr/isl
*
* LICENCE: TO BE ADDED
*
* Copyright 2019-2020
*
*/
/*
!! Code from mitos web search engine !!
*/
package gr.csd.uoc.hy463.fairness.trec.stemmer;
/**
* @author Panagiotis Papadakos (papadako@ics.forth.gr)
*/
import gr.csd.uoc.hy463.fairness.trec.stemmer.english.EnglishStemmer;
import javax.swing.*;
/*
*
*
*
*/
public class Stemmer {
private Stemmer() {
}
public static String Stem(String string) {
// To lower case
string = string.trim().toLowerCase();
// If emptry return.
if (string.length() == 0) {
return string;
}
// Java uses Unicode
// Modern Greek in unicode like ISO 8859-7 are between 0370 - 03FF
// Basic Latin 0021 - 007E
// !TODO Currently we do not stem any string that does not start with a ascii character
char c = string.charAt(0); // Get the first character of the word
if (c >= (char) 880 && c <= (char) 1023) // If character is between
// 0x0370 - 0x03FF
{
return string;
//GreekStemmer.Singleton().Stem(string); // return the greek
} // stemmer
else if (c >= (char) 33 && c <= (char) 126) // // If character is
// between 0x0021 - 0x007E
{
return EnglishStemmer.Singleton().Stem(string); // return the
} // english stemmer
else {
return string; // In all other cases return string as is
}
}
public static void Initialize() {
EnglishStemmer.Singleton();
}
public static void main(String[] a) {
String w;
Stemmer.Initialize();
do {
w = JOptionPane.showInputDialog("Please input a word");
JOptionPane.showMessageDialog(null, Stemmer.Stem(w), w,
JOptionPane.INFORMATION_MESSAGE);
} while (!w.equals(""));
}
} // class Stemmer
/*
* themis - A fair search engine for scientific articles
*
* Computer Science Department
*
* University of Crete
*
* http://www.csd.uoc.gr
*
* Project for hy463 Information Retrieval Systems course
* Spring Semester 2020
*
* LICENCE: TO BE ADDED
*
* Copyright 2020
*
*/
package gr.csd.uoc.hy463.fairness.trec.stemmer;
/**
* @author Panagiotis Papadakos (papadako@ics.forth.gr)
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class StopWords {
private static final Logger LOGGER = LogManager.getLogger(StopWords.class);
private static HashSet<String> words;
private static StopWords m_stopWords = null;
private static String STOPLIST_PATH = "stopwords.txt";
private BufferedReader br;
private StringBuffer strBuf;
/**
* Words constructor, initializes stoplist
*/
private StopWords() {
words = new HashSet<String>();
try {
br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/stopwords.txt")));
strBuf = new StringBuffer();
while (true) {
int ch = br.read();
if (ch == -1) {
break;
}
strBuf.append((char) ch);
}
StringTokenizer strTok = new StringTokenizer(strBuf.toString());
while (strTok.hasMoreTokens()) {
words.add(strTok.nextToken().toLowerCase());
}
} catch (FileNotFoundException e) {
LOGGER.error("Could not load stopwords file!");
} catch (IOException e) {
LOGGER.error("IOException found!");
}
}
/**
* Initialize stopwords. Add the stopwords from file
*/
public static void Initialize() {
m_stopWords = (m_stopWords == null) ? new StopWords() : m_stopWords;
}
/**
* Checks if a word is a stop word
*
* @param word The term to be checked
*
* @return true if the term is a stop word, false otherwise
*/
public static boolean isStopWord(String word) {
Initialize();
return words.contains(word);
}
/**
* Checks if a word is an operator word
*
* @param word The term to be checked
*
* @return true if the term is an operator word ("and","or","not"), false
* otherwise
*/
/*---------------------------------------------------*/
public static final boolean isOpWord(String word) {
Initialize();
return word.compareToIgnoreCase("and") == 0
|| word.compareToIgnoreCase("or") == 0
|| word.compareToIgnoreCase("not") == 0;
}
}
/*
* themis - A fair search engine for scientific articles
*
* Computer Science Department
*
* University of Crete
*
* http://www.csd.uoc.gr
*
* Project for hy463 Information Retrieval Systems course
* Spring Semester 2020
*
* LICENCE: TO BE ADDED
*
* Copyright 2020
*
*/
/*
!! Code from mitos web search engine !!
*/
package gr.csd.uoc.hy463.fairness.trec.stemmer.english;
/**
* @author Panagiotis Papadakos (papadako@ics.forth.gr)
*/
public class EnglishStemmer {
private static EnglishStemmer m_englishStemmer = null;
private PorterStemmer m_PorterStemmer;
private EnglishStemmer() {
m_PorterStemmer = new PorterStemmer();
}
public static EnglishStemmer Singleton() {
return (m_englishStemmer == null) ? (m_englishStemmer = new EnglishStemmer())
: m_englishStemmer;
}
public String Stem(String _word) {
String word = _word;
m_PorterStemmer.add(word.toCharArray(), word.length());
m_PorterStemmer.stem();
return m_PorterStemmer.toString();
}
} // class EnglishStemmer
/*
* themis - A fair search engine for scientific articles
*
* Computer Science Department
*
* University of Crete
*
* http://www.csd.uoc.gr
*
* Project for hy463 Information Retrieval Systems course
* Spring Semester 2020
*
* LICENCE: TO BE ADDED
*
* Copyright 2020
*
*/
/*
!! Code from mitos web search engine !!
*/
/*
Porter stemmer in Java. The original paper is in
Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
no. 3, pp 130-137,
See also http://www.tartarus.org/~martin/PorterStemmer
History:
Release 1
Bug 1 (reported by Gonzalo Parra 16/10/99) fixed as marked below.
The words 'aed', 'eed', 'oed' leave k at 'a' for step 3, and b[k-1]
is then out outside the bounds of b.
Release 2
Similarly,
Bug 2 (reported by Steve Dyrdahl 22/2/00) fixed as marked below.
'ion' by itself leaves j = -1 in the test for 'ion' in step 5, and
b[j] is then outside the bounds of b.
Release 3
Considerably revised 4/9/00 in the light of many helpful suggestions
from Brian Goetz of Quiotix Corporation (brian@quiotix.com).
Release 4
*/
package gr.csd.uoc.hy463.fairness.trec.stemmer.english;
/**
* @author Panagiotis Papadakos (papadako@ics.forth.gr)
*/
/**
* Stemmer, implementing the Porter Stemming Algorithm
*
* The Stemmer class transforms a word into its root form. The input word can be
* provided a character at time (by calling add()), or at once by calling one of
* the various stem(something) methods.
*/
class PorterStemmer {
private static final int INC = 50;
private char[] b;
private int i, /* offset into b */
i_end, /* offset to end of stemmed word */
j, k;
/* unit of size whereby b is increased */
public PorterStemmer() {
b = new char[INC];
i = 0;
i_end = 0;
}
/**
* Add a character to the word being stemmed. When you are finished adding
* characters, you can call stem(void) to stem the word.
*/
public void add(char ch) {
if (i == b.length) {
char[] new_b = new char[i + INC];
for (int c = 0; c < i; c++) {
new_b[c] = b[c];
}
b = new_b;
}
b[i++] = ch;
}
/**
* Adds wLen characters to the word being stemmed contained in a portion of
* a char[] array. This is like repeated calls of add(char ch), but faster.
*/
public void add(char[] w, int wLen) {
if (i + wLen >= b.length) {
char[] new_b = new char[i + wLen + INC];
for (int c = 0; c < i; c++) {
new_b[c] = b[c];
}
b = new_b;
}
for (int c = 0; c < wLen; c++) {
b[i++] = w[c];
}
}
/**
* After a word has been stemmed, it can be retrieved by toString(), or a
* reference to the internal buffer can be retrieved by getResultBuffer and
* getResultLength (which is generally more efficient.)
*/
public String toString() {
return new String(b, 0, i_end);
}
/**
* Returns the length of the word resulting from the stemming process.
*/
public int getResultLength() {
return i_end;
}
/**
* Returns a reference to a character buffer containing the results of the
* stemming process. You also need to consult getResultLength() to determine
* the length of the result.
*/
public char[] getResultBuffer() {
return b;
}
/* cons(i) is true <=> b[i] is a consonant. */
private final boolean cons(int i) {
switch (b[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return false;
case 'y':
return (i == 0) || !cons(i - 1);
default:
return true;
}
}
/*
* m() measures the number of consonant sequences between 0 and j. if c is a
* consonant sequence and v a vowel sequence, and <..> indicates arbitrary
* presence,
*
* <c><v> gives 0 <c>vc<v> gives 1 <c>vcvc<v> gives 2 <c>vcvcvc<v> gives 3
* ....
*/
private final int m() {
int n = 0;
int i = 0;
while (true) {
if (i > j) {
return n;
}
if (!cons(i)) {
break;
}
i++;
}
i++;
while (true) {
while (true) {
if (i > j) {
return n;
}
if (cons(i)) {
break;
}
i++;
}
i++;
n++;
while (true) {
if (i > j) {
return n;
}
if (!cons(i)) {
break;
}
i++;
}
i++;
}
}
/* vowelinstem() is true <=> 0,...j contains a vowel */
private final boolean vowelinstem() {
int i;
for (i = 0; i <= j; i++) {
if (!cons(i)) {
return true;
}
}
return false;
}
/* doublec(j) is true <=> j,(j-1) contain a double consonant. */
private final boolean doublec(int j) {
if (j < 1) {
return false;
}
if (b[j] != b[j - 1]) {
return false;
}
return cons(j);
}
/*
* cvc(i) is true <=> i-2,i-1,i has the form consonant - vowel - consonant
* and also if the second c is not w,x or y. this is used when trying to
* restore an e at the end of a short word. e.g.
*
* cav(e), lov(e), hop(e), crim(e), but snow, box, tray.
*/
private final boolean cvc(int i) {
if (i < 2 || !cons(i) || cons(i - 1) || !cons(i - 2)) {
return false;
}
{
int ch = b[i];
return ch != 'w' && ch != 'x' && ch != 'y';
}
}
private final boolean ends(String s) {
int l = s.length();
int o = k - l + 1;
if (o < 0) {
return false;
}
for (int i = 0; i < l; i++) {
if (b[o + i] != s.charAt(i)) {
return false;
}
}
j = k - l;
return true;
}
/*
* setto(s) sets (j+1),...k to the characters in the string s, readjusting
* k.
*/
private final void setto(String s) {
int l = s.length();
int o = j + 1;
for (int i = 0; i < l; i++) {
b[o + i] = s.charAt(i);
}
k = j + l;
}
/* r(s) is used further down. */
private final void r(String s) {
if (m() > 0) {
setto(s);
}
}
/*
* step1() gets rid of plurals and -ed or -ing. e.g.
*
* caresses -> caress ponies -> poni ties -> ti caress -> caress cats -> cat
*
* feed -> feed agreed -> agree disabled -> disable
*
* matting -> mat mating -> mate meeting -> meet milling -> mill messing ->
* mess
*
* meetings -> meet
*/
private final void step1() {
if (b[k] == 's') {
if (ends("sses")) {
k -= 2;
} else if (ends("ies")) {
setto("i");
} else if (b[k - 1] != 's') {
k--;
}
}
if (ends("eed")) {
if (m() > 0) {
k--;
}
} else if ((ends("ed") || ends("ing")) && vowelinstem()) {
k = j;
if (ends("at")) {
setto("ate");
} else if (ends("bl")) {
setto("ble");
} else if (ends("iz")) {
setto("ize");
} else if (doublec(k)) {
k--;
{
int ch = b[k];
if (ch == 'l' || ch == 's' || ch == 'z') {
k++;
}
}
} else if (m() == 1 && cvc(k)) {
setto("e");
}
}
}
/* step2() turns terminal y to i when there is another vowel in the stem. */
private final void step2() {
if (ends("y") && vowelinstem()) {
b[k] = 'i';
}
}
/*
* step3() maps double suffices to single ones. so -ization ( = -ize plus
* -ation) maps to -ize etc. note that the string before the suffix must
* give m() > 0.
*/
private final void step3() {
if (k == 0) {
return;
/* For Bug 1 */
}
switch (b[k - 1]) {
case 'a':
if (ends("ational")) {
r("ate");
break;
}
if (ends("tional")) {