Sunday, 21 June 2015

Searching Nepali Text

It was late 2007, I was trying to develop the system that could search queries and displays the result from huge Nepali archives. There were two problems:

1. How to get huge amount of Nepali text ?
2. How to make them searchable and search with high relevancy ?

 I developed some state machines (a simple, equivalent regular expression) that can able accumulate news archives from the web dumped of many news sources.  A sample machine  for Himal News Paper is here.


Fig. State machine that accumulates news of Himal Khabar.

The java source representation of the state machine is:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Santa
 */
public class Himal { 
    
    private boolean isNepali(char ch) {
        if (ch >= 0x900 && ch <= 0x97f) {
            return (true);
        } else if (ch == ',' || ch == '?' || ch=='!') {
            return (true);
        } else if (ch == ' ') {
            return (true);
        } else if (ch == '\n') {
            return (true);
        } else {
            return (false);
        }
    }
    
    private StringBuffer filterText(StringBuffer s){
        StringBuffer result = new StringBuffer();
        char ch;
        int state = 0;
        for(int i=0;i<s.length();i++){
            ch = s.charAt(i);
            if(ch==',' || ch=='?' || ch=='เคƒ'){
                if(state != 0){
                    result.append(ch);
                    result.append(" ");
                }
                state = 0;
            }
            else if(ch==' '){
                if(state != 0)
                    result.append(ch);
                state = 0;
            }             
            else{
                state = 1;
                result.append(ch);
            }
        }
        return(result);
    }
    
   private StringBuffer dfaHimal(StringBuffer buffer){
        StringBuffer result = new StringBuffer();       
        String tmp = "";
        int state = 1;
        char ch;
        for(int i=0;i<buffer.length();i++){
            ch = buffer.charAt(i);          
            switch(state){
                case 1:
                    if(ch=='=')
                        state = 2;
                    else
                        state = 1;                   
                    break;
                case 2:
                    if(ch=='"')
                        state = 3;                    
                    else
                        state = 1;
                    break;
                case 3:
                    if(ch=='"'){
                        if(tmp.equals("headline")||tmp.equals("articletext")||tmp.equals("intro"))                                                        
                            state = 4;      
                        else
                            state = 1;
                        tmp = "";
                    }                    
                    else{
                        tmp += ch;
                        state = 3;
                    }
                    break;  
                case 4: 
                    if(isNepali(ch))
                        result.append(ch);                    
                    if(ch=='<')
                        state = 5;
                    else
                        state = 4;
                    break;
                case 5:
                    if(ch=='>'){
                        if(tmp.equals("br")||tmp.equals("/p")||tmp.equals("/P")||tmp.equals("BR")||tmp.equals("/TABLE")){
                            result.append("\r\n44  ");
                            state = 4;
                        }
                        else if(tmp.equals("/div")){
                            result.append("\r\n");
                            state = 1;
                        }
                        else
                            state = 4;
                    }
                    else if(ch=='='){
                        state = 6;
                    }
                    else{
                        tmp += ch;
                        state = 5;
                    }
                    break;
                case 6:
                    if(ch=='"'){
                        tmp = "";
                        state = 7; 
                    }
                    else
                        state = 4;
                    break;
                case 7:
                    if(ch=='"'){
                        if(tmp.equals("headlines"))
                            return(result);
                        else
                            state = 5;                      
                    }
                    else{
                        tmp += ch;
                        state = 7;
                    }
            }
        }
        return(result);
    }
    
    public StringBuffer getNewsText(String file){
        StringBuffer result = new StringBuffer();
        StringBuffer buffer = new StringBuffer();
        FileReadWrite obj = new FileReadWrite();
        buffer = obj.readFile(file);     
        result = dfaHimal(buffer);
        return(result);
    }

}

It was the beginning of the searching system development.


No comments:

Post a Comment