Advertisement
rachmadi

Stemming

Dec 23rd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.77 KB | None | 0 0
  1. package com.example.kamusindonesia;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.Locale;
  8. import java.util.Scanner;
  9.  
  10. import android.content.Context;
  11. import android.util.Log;
  12.  
  13. public class Stemming {
  14.  
  15.     Word originalword = new Word();
  16.     String word = originalword.getWord();
  17.     String stemword, bstemword = "";
  18.     String suffix = "";
  19.     String decodeword = "";
  20.     String cword = "";
  21.     String word1 = "";
  22.     Scanner sc;
  23.     int checked = 0;
  24.     boolean found;
  25.     public static int root=1;
  26.    
  27.     public void setStemWord(String word){
  28.         this.stemword = word;
  29.     }
  30.    
  31.     public String getStemWord(){
  32.         return stemword;
  33.     }
  34.    
  35.     public String stemWord(Context context, String word) throws IOException{
  36.         word = word.toLowerCase();
  37.         if (!word.trim().equals("")){
  38.             stemword = word;
  39.             found = checkWord(context, word);
  40.             if(found==false && (stemword.length()>4)){
  41.                 root = 0;
  42.                 step2();
  43.                 found=checkWord(context, stemword);
  44.                 if(found==false){
  45.                     step3();
  46.                     found=checkWord(context, stemword);
  47.                     if(found==false){
  48.                         int i=0;
  49.                         while (found==false && i<3){
  50.                             step4();
  51.                             found=checkWord(context, stemword);
  52.                             i++;
  53.                             System.out.println("step 4.I ke : "+i);
  54.                         }
  55.                         if(found==false){
  56.                             if((suffix=="an")&&(stemword.endsWith("k"))){
  57.                                 stemword=stemword.substring(0,stemword.length()-1);
  58. //                              suffix="kan";
  59.                                 i = 0;
  60.                                 while (found==false && i<3){
  61.                                     step4();
  62.                                     found=checkWord(context, stemword);
  63.                                     i++;
  64.                                     System.out.println("step 4.II ke : "+i);
  65.                                 }
  66.                                 if(found==false){
  67.                                     System.out.println("Stemword 2: "+stemword);
  68.                                     System.out.println("step 5.I");
  69.                                     step5();
  70.                                     checkWord(context, stemword);
  71.                                 }
  72.                             }
  73.                             else {
  74.                                 stemword=bstemword;
  75.                                 System.out.println("Stemword 3: "+stemword);
  76.                                 i=0;
  77.                                 while (found==false && i<3){
  78.                                     System.out.println("Stemword 4: "+stemword);
  79.                                     step4();
  80.                                     found=checkWord(context, stemword);
  81.                                     i++;
  82.                                     System.out.println("step 4.IV ke : "+i);
  83.                                 }
  84.                                 if(found==false){
  85.                                     System.out.println("Stemword 5: "+stemword);
  86.                                     System.out.println("step 5.II");
  87.                                     step5();
  88.                                     checkWord(context, stemword);
  89.                                 }
  90.                             }
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.         return stemword;
  97.     }
  98.  
  99.     public boolean checkWord(Context context, String w) {
  100.         CSearch c = new CSearch();
  101.         found = c.search(context, w);
  102.         return found;
  103.     }
  104.    
  105.     public void step2(){
  106.         System.out.println("Step 2");
  107.         System.out.println("Stemword di step 2 : "+stemword);
  108.         if (stemword.endsWith("kah")){
  109.             stemword=stemword.substring(0, stemword.length()-3);
  110.             System.out.println("Akhiran kah : "+stemword);
  111.         }
  112.        
  113.         else if (stemword.endsWith("lah")){
  114.             stemword=stemword.substring(0, stemword.length()-3);
  115.             System.out.println("Akhiran lah : "+stemword);
  116.         }
  117.        
  118.         else if (stemword.endsWith("tah")){
  119.             stemword=stemword.substring(0, stemword.length()-3);
  120.         }
  121.        
  122.         else if (stemword.endsWith("pun")){
  123.             stemword=stemword.substring(0, stemword.length()-3);
  124.         }
  125.        
  126.         if (stemword.endsWith("ku")){
  127.             if(!stemword.endsWith("aku")){
  128.                 stemword=stemword.substring(0,stemword.length()-2);
  129.             }
  130.         }
  131.        
  132.         else if (stemword.endsWith("mu")){
  133.             stemword=stemword.substring(0,stemword.length()-2);
  134.         }
  135.        
  136.         else if (stemword.endsWith("nya")){
  137.             stemword=stemword.substring(0,stemword.length()-3);
  138.         }
  139.         setStemWord(stemword);
  140.         System.out.println("Stemword di akhir Step 2: "+stemword);
  141.     }
  142.    
  143.     public void step3(){
  144.         System.out.println("Step 3");
  145.         bstemword = stemword;
  146.        
  147.         if (stemword.endsWith("i")){
  148.             stemword=stemword.substring(0,stemword.length()-1);
  149.             suffix = "i";
  150.         }
  151.  
  152.         else if (stemword.endsWith("an")){
  153.             stemword=stemword.substring(0,stemword.length()-2);
  154.             suffix = "an";
  155.         }
  156.        
  157.         setStemWord(stemword);
  158.         System.out.println("Stemword di akhir Step 3: "+stemword);
  159.         decodeword = stemword;
  160.     }
  161.  
  162.     public void step4(){
  163.  
  164.         if((stemword.length()<5)){return;}
  165.         System.out.println("Step 4");
  166.         String word2=stemword.substring(2);
  167.         String word3=stemword.substring(3);
  168.         String word4=stemword.substring(4);
  169.         String word5=stemword.substring(5);
  170.         String prefix=word2.substring(0,1);
  171.         String prefix2=word2.substring(1,2);
  172.         String prefix3=word2.substring(2,3);
  173.         String vowel="aiueo";
  174.         String consonant="bcdfghjklmnpqrstvwxyz";
  175.         String consonantr="bcdfghjklmnpqstvwxyz";
  176.         String consonantrl="bcdfghjkmnpqstvwxyz";
  177.         String consonantrwylmn="bcdfghjkpqstvxz";
  178.         String lrwy="lrwy";
  179.         String bfv="bfv";
  180.         String rl="rl";
  181.         String cdjz="cdjz";
  182.         String ghq="ghq";
  183.         String wy="wy";
  184.          
  185.         if (stemword.startsWith("di")){
  186.             stemword=word2;
  187.         }
  188.         else if (stemword.startsWith("ke")){
  189.             stemword=word2;
  190.         }
  191.         else if (stemword.startsWith("se")){
  192.             stemword=word2;
  193.         }
  194.         else if (stemword.startsWith("be")){
  195.             if (word2.startsWith("r")&&
  196.                     vowel.contains(prefix2)){
  197.                 stemword=word3;
  198. //              tvresult.setText(stemword); //berV
  199.             }
  200.            
  201.             else if(word2.startsWith("r")&&
  202.                     consonantr.contains(prefix2)&&
  203.                     !(word5.startsWith("er"))){
  204.                 stemword=word3;
  205. //              tvresult.setText(stemword); //berCAP
  206.             }
  207.            
  208.             else if(word2.startsWith("r")&&
  209.                     consonantr.contains(prefix2)&&
  210.                     (word5.startsWith("er")) &&
  211.                     vowel.contains(word2.substring(5, 6))){
  212.                 stemword=word3;
  213. //              tvresult.setText(stemword); //berCAerV
  214.             }
  215.             else if(word2.startsWith("l")){
  216.                 stemword=word3;
  217. //              tvresult.setText(stemword); //belajar
  218.             }
  219.             else if(consonantrl.contains(prefix)&&
  220.                     (word3.startsWith("er")) &&
  221.                     consonant.contains(word2.substring(3, 4))){
  222.                 stemword=word2;
  223. //              tvresult.setText(stemword); //berC1erC2
  224.             }  
  225.            
  226.         }
  227.        
  228.         else if(stemword.startsWith("te")){
  229.             if (word2.startsWith("r")&&
  230.                     vowel.contains(prefix2)){
  231.                 stemword=word3;
  232. //              tvresult.setText(stemword);//terV
  233.             }
  234.            
  235.             else if(word2.startsWith("r")&&
  236.                     consonantr.contains(prefix2)&&
  237.                     (word4.startsWith("er")) &&
  238.                     vowel.contains(word2.substring(4, 5))){
  239.                 stemword=word3;
  240. //              tvresult.setText(stemword);//terCerV
  241.             }
  242.            
  243.             else if(word2.startsWith("r")&&
  244.                     consonantr.contains(prefix2)&&
  245.                     !(word4.startsWith("er"))){
  246.                 stemword=word3;
  247. //              tvresult.setText(stemword); //terCP
  248.             }
  249.            
  250.             else if(consonantr.contains(prefix)&&
  251.                     (word3.startsWith("er")) &&
  252.                     consonant.contains(word2.substring(3, 4))){
  253.                 stemword=word2;
  254. //              tvresult.setText(word1); //teC1erC2
  255.             }
  256.            
  257.         }
  258.        
  259.         else if(stemword.startsWith("me")){
  260.             if(lrwy.contains(prefix)&&
  261.             vowel.contains(prefix2)){
  262.                 stemword=word2;
  263. //              tvresult.setText(stemword);//me{l|r|w|y}V
  264.                 System.out.println("me{l|r|w|y}V");
  265.             }
  266.            
  267.             else if(word2.startsWith("m")&&
  268.                     (bfv.contains(prefix2))){
  269.                 stemword=word3;
  270. //              tvresult.setText(stemword);
  271.                 System.out.println("me{l|r|w|y}V");
  272.             }
  273.            
  274.             else if (word2.startsWith("mpe")&&
  275.                     (rl.contains(word2.substring(3, 4)))){
  276.                 stemword=word3;
  277. //              tvresult.setText(stemword);
  278.             }
  279.            
  280.             else if(word2.startsWith("n")&&
  281.                     (cdjz.contains(prefix2))) {
  282.                 stemword=word3;
  283. //              tvresult.setText(stemword);
  284.             }
  285.            
  286.             else if(word2.startsWith("n")&&
  287.                     (vowel.contains(prefix2))){
  288.                 stemword=word2;
  289. //              tvresult.setText(stemword);
  290.             }
  291.            
  292.             else if (word2.startsWith("m")&&
  293.                     ((prefix2.contains("r")&&
  294.                             (vowel.contains(prefix3)))) ||
  295.                             (vowel.contains(prefix2))) {
  296.                 stemword=word3;
  297. //              tvresult.setText(stemword);
  298.             }
  299.            
  300.             else if(word2.startsWith("ng")&&
  301.                     (ghq.contains(prefix3))){
  302.                 stemword=word4;
  303. //              tvresult.setText(stemword);
  304.             }
  305.            
  306.             else if(word2.startsWith("ng")&&
  307.                     (vowel.contains(prefix3))){
  308.                 stemword=word4;
  309. //              tvresult.setText(stemword);
  310.             }
  311.            
  312.             else if(word2.startsWith("ny")&&
  313.                     (vowel.contains(prefix3))){
  314.                 stemword=word4;
  315. //              tvresult.setText(stemword);
  316.             }
  317.            
  318.             else if(word2.startsWith("mp")&&
  319.                     (vowel.contains(prefix3))){
  320.                 stemword=word3;
  321. //              tvresult.setText(stemword);
  322.             }
  323.         }
  324.        
  325.         else if(stemword.startsWith("pe")){
  326.             if(wy.contains(prefix)&&
  327.             vowel.contains(prefix2)){
  328.                 stemword=word2;
  329. //              tvresult.setText(stemword);//pe{w|y}V
  330.             }
  331.            
  332.             else if(word2.startsWith("r")&&
  333.                     vowel.contains(prefix2)){
  334.                 stemword=word3;
  335. //              tvresult.setText(stemword);
  336.             }      
  337.            
  338.             else if(word2.startsWith("r")&&
  339.                     consonantr.contains(prefix2)&&
  340.                     !(word5.startsWith("er"))){
  341.                 stemword=word3;
  342. //              tvresult.setText(stemword); //perCAP
  343.             }
  344.            
  345.             else if(word2.startsWith("r")&&
  346.                     consonantr.contains(prefix2)&&
  347.                     (word5.startsWith("er")) &&
  348.                     vowel.contains(word2.substring(5, 6))){
  349.                 stemword=word3;
  350. //              tvresult.setText(stemword); //perCAerV
  351.             }
  352.            
  353.             else if(word2.startsWith("m")&&
  354.                     (bfv.contains(prefix2))){
  355.                 stemword=word3;
  356. //              tvresult.setText(stemword);
  357.             }
  358.            
  359.             else if (word2.startsWith("m")&&
  360.                     ((prefix2.contains("r")&&
  361.                             (vowel.contains(prefix3)))) ||
  362.                             (vowel.contains(prefix2))) {
  363.                 stemword=word3;
  364. //              tvresult.setText(stemword);
  365.             }
  366.            
  367.             else if(word2.startsWith("n")&&
  368.                     (cdjz.contains(prefix2))) {
  369.                 stemword=word3;
  370. //              tvresult.setText(stemword);
  371.             }
  372.            
  373.             else if(word2.startsWith("n")&&
  374.                     (vowel.contains(prefix2))){
  375.                 stemword=word2;
  376. //              tvresult.setText(stemword);
  377.             }
  378.            
  379.             else if(word2.startsWith("ng")&&
  380.                     (ghq.contains(prefix3))){
  381.                 stemword=word4;
  382. //              tvresult.setText(stemword);
  383.             }
  384.            
  385.             else if(word2.startsWith("ng")&&
  386.                     (vowel.contains(prefix3))){
  387.                 stemword=word4;
  388. //              tvresult.setText(stemword);
  389.             }
  390.            
  391.             else if(word2.startsWith("ny")&&
  392.                     (vowel.contains(prefix3))){
  393.                 stemword="s"+word4;
  394. //              tvresult.setText(stemword);
  395.             }
  396.            
  397.             else if(word2.startsWith("l")){
  398.                 stemword=word2;
  399. //              tvresult.setText(stemword); //
  400.             }
  401.            
  402.             else if(word2.startsWith("r")&&
  403.                     consonantr.contains(prefix2)&&
  404.                     (word4.startsWith("er")) &&
  405.                     vowel.contains(word2.substring(4, 5))){
  406.                 stemword=word3;
  407. //              tvresult.setText(stemword);//perCerV
  408.             }
  409.            
  410.             else if((consonantr.contains(prefix2)&&
  411.                     (consonantrwylmn.contains(prefix2)))&&
  412.                     !(word4.startsWith("er"))){
  413.                 stemword=word2;
  414. //              tvresult.setText(stemword); //perCP
  415.             }
  416.            
  417.         }
  418.         setStemWord(stemword);
  419.         System.out.println("Stemword di akhir Step 4: "+stemword); 
  420.     }
  421.  
  422.     public void step5(){
  423.         stemword = decodeword;
  424.         System.out.println("Step 5");
  425.         System.out.println("Stemword Step 5 :"+stemword);
  426.         if((stemword.length()<5)){return;}
  427. //      stemword=etword.getText().toString();
  428.        
  429.         String word2=stemword.substring(2);
  430.         String word3=stemword.substring(3);
  431.         String word4=stemword.substring(4);
  432.         String prefix2=word2.substring(1,2);
  433.         String prefix3=word2.substring(2,3);
  434.         String vowel="aiueo";
  435.    
  436.          
  437.        
  438.         if (stemword.startsWith("be")){
  439.             if (word2.startsWith("r")&&
  440.                     vowel.contains(prefix2)){
  441.                 stemword=word2;
  442. //              tvresult.setText(stemword); //berV
  443.             }
  444.         }  
  445.            
  446.         else if(stemword.startsWith("te")){
  447.             if (word2.startsWith("r")&&
  448.                     vowel.contains(prefix2)){
  449.                 stemword=word2;
  450. //              tvresult.setText(stemword);//terV
  451.             }
  452.            
  453.         }
  454.        
  455.         else if(stemword.startsWith("me")){
  456.            
  457.             if(word2.startsWith("n")&&
  458.                     (vowel.contains(prefix2))){
  459.                 stemword=word3;
  460.                 stemword="t"+stemword;
  461. //              tvresult.setText("t"+stemword);
  462.             }
  463.            
  464.             else if (word2.startsWith("m")&&
  465.                     ((prefix2.contains("r")&&
  466.                             (vowel.contains(prefix3)))) ||
  467.                             (vowel.contains(prefix2))) {
  468.                 stemword=word3;
  469.                 stemword="p"+stemword;
  470. //              tvresult.setText("p"+stemword);
  471.             }
  472.            
  473.             else if(word2.startsWith("ng")&&
  474.                     (vowel.contains(prefix3))){
  475.                 stemword=word4;
  476.                 stemword="k"+stemword;
  477. //              tvresult.setText("k"+stemword);
  478.             }
  479.         }
  480.        
  481.         else if(stemword.startsWith("pe")){
  482.             if(word2.startsWith("r")&&
  483.                     vowel.contains(prefix2)){
  484.                 stemword=word3;
  485.                 stemword="r"+stemword;
  486. //              tvresult.setText("r"+stemword);
  487.             }      
  488.            
  489.            
  490.             else if(word2.startsWith("n")&&
  491.                     (vowel.contains(prefix2))){
  492.                 stemword=word3;
  493.                 stemword="t"+stemword;
  494. //              tvresult.setText("t"+stemword);
  495.             }
  496.            
  497.             else if (word2.startsWith("m")&&
  498.                     ((prefix2.contains("r")&&
  499.                             (vowel.contains(prefix3)))) ||
  500.                             (vowel.contains(prefix2))) {
  501.                 stemword=word3;
  502.                 stemword="p"+stemword;
  503. //              tvresult.setText("p"+stemword);
  504.             }
  505.            
  506.            
  507.             else if(word2.startsWith("ng")&&
  508.                     (vowel.contains(prefix3))){
  509.                 stemword=word4;
  510.                 stemword="k"+stemword;
  511. //              tvresult.setText("k"+stemword);
  512.             }
  513.                    
  514.         }
  515.         setStemWord(stemword);
  516.         System.out.println("Stemword di akhir Step 5: "+stemword);
  517.     }
  518.  
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement