package com.TextMining;

/**
 * Created by Sakurmi on 10/29/2015.
 */
public class StemWord {

    public static final char ALEF = '\u0627';
    public static final char BEH = '\u0628';
    public static final char TEH = '\u062A';
    public static final char FEH = '\u0641';
    public static final char KAF = '\u0643';
    public static final char LAM = '\u0644';
    public static final char NOON = '\u0646';
    public static final char HEH = '\u0647';
    public static final char WAW = '\u0648';
    public static final char YEH = '\u064A';


    public static final String prefixes[] = {
            ("" + WAW + LAM + LAM), ("" + WAW + ALEF + LAM), ("" + BEH + ALEF + LAM),
            ("" + KAF + ALEF + LAM), ("" + FEH + ALEF + LAM),
            ("" + ALEF + LAM),("" + LAM + LAM),("" + WAW)};

    public static final String suffixes[] = {
            ("" + WAW + ALEF),
            ("" + HEH + ALEF),
            ("" + ALEF + NOON),
            ("" + ALEF + TEH), ("" + WAW + NOON), ("" + YEH + NOON ),("" + YEH + HEH), ("" + HEH), ("" + YEH)
    };


    public static String Stem(String str)
    {
        String CurrentStr = "";


        if (!str.contains(" ")) {
            CurrentStr = RemovePrefix(str);
            CurrentStr = RemoveSuffix(CurrentStr);
        }
        else
        {
            CurrentStr = StemConnectedWords(str);
        }
        return CurrentStr;
    }

    private static String StemConnectedWords(String str)
    {
        String[] StrArray = str.split(" ");

        String CurrentString = "";
        for(int index= 0 ; index < StrArray.length; ++index)
        {
            StrArray[index] = RemovePrefix(StrArray[index]);
            StrArray[index] = RemoveSuffix(StrArray[index]);

            CurrentString += StrArray[index] + " ";
        }

      //  System.out.println(CurrentString);

        CurrentString = CurrentString.substring(0, CurrentString.length() - 1);

        return CurrentString;
    }

    private static String RemovePrefix(String word)
    {
        String modifiedWord = word;
        boolean isDone = false;

        for(int index= 0 ; index < prefixes.length; ++index)
        {
            if (word.startsWith(prefixes[index]))
                if (prefixes[index].equals(WAW)&& !(word.length() > 3) && !isDone) {
                    isDone = true;
        //            System.out.println(prefixes[index]);
                    modifiedWord = word.substring(word.indexOf(prefixes[index]) + 1);
                }
                else if ((prefixes[index].length() + 2 < word.length()) ) {
                    isDone = true;
          //          System.out.println(prefixes[index]);
                    modifiedWord = word.substring(word.indexOf(prefixes[index]) + prefixes[index].length());
                }
        }


        return modifiedWord;
    }

    private static String RemoveSuffix(String word)
    {
        String modifiedWord = word;

        boolean isDone = false;

        for(int index= 0 ; index < suffixes.length; ++index)
        {
            if (word.endsWith(suffixes[index]) ) {


                if ((suffixes[index].equals("" + ALEF + TEH) || suffixes[index].equals("" + ALEF + NOON)) )
                {
                    if((suffixes[index].length() + 3 < word.length()) && !isDone){

                        isDone = true;
                        modifiedWord = word.substring(0, word.lastIndexOf(suffixes[index]));
                    }
                    else
                        isDone = true;

                }
                else if (suffixes[index].length() + 2 < word.length() && !isDone) {

            //        System.out.println(suffixes[index] + ":" + word.lastIndexOf(suffixes[index]));
                    isDone = true;
                    modifiedWord = word.substring(0, word.lastIndexOf(suffixes[index]));
                }
            }
        }

        if (modifiedWord.endsWith("ؤ") || modifiedWord.endsWith("ئ")) {
            modifiedWord = modifiedWord.replace("ؤ", "ء");
            modifiedWord = modifiedWord.replace("ئ", "ء");
        }

        return modifiedWord;
    }
}

@@ -43,62 +43,245 @@
  by 
-Sakurmi
+Marwa Khan
  on 1
-0/29
+/4
 /201
-5
+6
 .%0D%0A *
-/%0D%0Apublic class StemWord %7B%0D%0A
+ Class Description: This class will stem each word to its root %0D%0A * Note: the utilized stemmer is based on Apache stemmer, but with view changes %0D%0A */%0D%0Apublic class StemWord %7B%0D%0A%0D%0A    // variables Initilization 
 %0D%0A  
@@ -1267,24 +1267,50 @@
     %7D;%0D%0A%0D%0A%0D%0A
+    // class constructer%0D%0A
     public s
@@ -1643,32 +1643,115 @@
 tStr;%0D%0A    %7D%0D%0A%0D%0A
+    //Method to stem a given word %0D%0A    // Input: String %0D%0A    // Output: String %0D%0A
     private stat
@@ -2149,58 +2149,8 @@
  %7D%0D%0A
-%0D%0A      //  System.out.println(CurrentString);%0D%0A%0D%0A
     
@@ -2260,32 +2260,65 @@
 ring;%0D%0A    %7D%0D%0A%0D%0A
+    // Method to remove prefix %0D%0A
     private stat
@@ -2683,68 +2683,8 @@
 e;%0D%0A
-        //            System.out.println(prefixes%5Bindex%5D);%0D%0A
     
@@ -2902,68 +2902,8 @@
 e;%0D%0A
-          //          System.out.println(prefixes%5Bindex%5D);%0D%0A
     
@@ -3038,20 +3038,16 @@
      %7D%0D%0A
-%0D%0A%0D%0A
         
@@ -3073,24 +3073,56 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    // method to remove suffix%0D%0A
     private 
@@ -3921,110 +3921,8 @@
 %0D%0A%0D%0A
-            //        System.out.println(suffixes%5Bindex%5D + %22:%22 + word.lastIndexOf(suffixes%5Bindex%5D));%0D%0A
     

Update StemWord.java