Khmer Spelling Checker (part II)

After we separate Khmer words in sentences, I have an idea which implementing a Khmer spelling check in the browser extension. We are going to build a browser extension which embedded with browser input field. It will check any typo when user lost focus of that field then it will suggest a similar word to the user. In our first version we will check typing mistake only and for grammar and other logical error we will implement it later on.

In order to build this extention, we need to implement a server side where it receive senetence from user then it check any mistake and respond back to user. we also implementing extension where it send input value to server and handle response from server and display to user.

Server Side Logic

Here are steps, we use in our server side:

  • recieve request from clients
  • seperate string of sentences in array of sentents
  • check each words in senteces
  • response an json object of sentences with its sugestion


Let's start by setting up a small Flask server.

# -*- coding: utf-8 -*-
import sys
from flask import Flask
from flask_restful import Resource, Api
from resources.home import Home
from resources.spelling_checker import SpellingCheck

app = Flask(__name__)
api = Api(app)

api.add_resource(Home, '/')
api.add_resource(SpellingCheck, '/sentences_checking')

if __name__ == '__main__':

Let's create home page handler

from flask_restful import Resource

class Home(Resource):
  def get(self):
    return {'hello world': 'Welcome to Khmer Spelling checker API'}

Let's create spelling check handler

# -*- coding: utf-8 -*-
from flask_restful import reqparse, Resource
import model.word_segmentation as word_segmentation

parser = reqparse.RequestParser()

class SpellingCheck(Resource):
  def post(self):
    args = parser.parse_args()
    sentences = args['sentences']
    words = word_segmentation.WordSegmentation(sentences)
    return words.get_result(), 201

As you can see in our code about, we use the code in our previous article. However, we customize some logic in it.

class WordSegmentation:
  def check_words(self):
    while(self.startIndex < len(self.text)):
      ch = self.text[self.startIndex]
      ch = ch.encode('utf-8')
      word = ''

      if self.isNumber(ch):
        word = self.parseNumber(self.startIndex).encode('utf-8')
      elif self.isEnglish(ch):
        word = self.parseEnglish(self.startIndex).encode('utf-8')
        word = self.parseTrie(self.startIndex)

      length = len(word.decode('utf-8'))
      result = {}
      if length == 0:
        result["text"] = ch.decode('utf-8')
        result["is_word"] = False
        self.startIndex += 1

      if self.model.searchWord(word) or self.isNumber(ch) or self.isEnglish(ch):
        result["text"] = word.decode('utf-8').encode('utf-8')
        result["is_word"] = True
        result["text"] = word.decode('utf-8').encode('utf-8')
        result["is_word"] = False
        result["suggest_word"] = self.get_suggestion(word.decode('utf-8'))

      self.startIndex += length

Fanally, let's start our server:

FLASK_APP=api_server.py flask run

Now, let's make a simple post request:



What's next?

We are planning to build a Khmer work checking in a browser and we already implemented server side where it receives raw input and send back the validated words with their suggestion. Next, we are going to create a browser extension for sending user input value and displaying the error and suggesting similar words. Stay tune. 😃

All Rights Reserved

Let's register a Viblo Account to get more interesting posts.