Skip to content

Estimate the phonetic distance between Chinese words and get similar sounding candidate words.

License

Notifications You must be signed in to change notification settings

kmh4321/MAX-Chinese-Phonetic-Similarity-Estimator

 
 

Repository files navigation

Build Status Website Status

IBM Developer Model Asset Exchange: Chinese Phonetic Similarity Estimator

This repository contains code to instantiate and deploy a Chinese Phonetic Similarity Estimator. The model provides a phonetic algorithm for indexing Chinese characters by sound. Given two Chinese words of the same length, the model determines the distances between the two words and also returns a few candidate words which are close to the given word(s). The code complies with the phonetic principles of Mandarin Chinese as guided by the Romanization defined in ISO 7098:2015.

The model is based on the DimSim model. The code in this repository deploys the model as a web service in a Docker container. This repository was developed as part of the IBM Developer Model Asset Exchange.

Model Metadata

Domain Application Industry Framework Training Data Input Data Format
NLP Text Clustering/Phonetics Social Media Python N/A Chinese Text (utf-8 encoded)

References

Licenses

Component License Link
This repository Apache 2.0 LICENSE
Model Weights N/A N/A
Model Code (3rd party) Apache 2.0 LICENSE
Test assets N/A N/A

Pre-requisites:

  • docker: The Docker command-line interface. Follow the installation instructions for your system.
  • The minimum recommended resources for this model is 4 GB Memory and 4 CPUs.

Steps

  1. Run using PyPi
  2. Deploy from Docker Hub
  3. Deploy on Kubernetes
  4. Run Locally

Run using PyPi

Installing the library

Dependencies:

  • pypinyin: used for translating Chinese characters into their correponding pinyins.

There are two ways to install this library:

  • Install from PyPi
pip install dimsim
  • Download the source code by cloning the source repo and compile it yourself.
git clone [email protected]:System-T/DimSim.git

cd DimSim/

pip install -e .

How to use the library

Once you have the package installed you can use it for the two functions as shown below.

  • Computing phonetic distance of two Chinese phrases. The optional argument pinyin (False by default) can be used to provide a pinyin string list directly. See example usage below.
import dimsim

dist = dimsim.get_distance("大侠","大虾")
0.0002380952380952381

dist = dimsim.get_distance("大侠","大人")
25.001417183349876

dist = dimsim.get_distance(['da4','xia2'],['da4','xia1']], pinyin=True)
0.0002380952380952381

dist = dimsim.get_distance(['da4','xia2'],['da4','ren2']], pinyin=True)
25.001417183349876

  • Return top-k phonetically similar phrases of a given Chinese phrase. Two parameters:
  • mode controls the character type of the returned Chinese phrases, where 'simplified' represents simplified Chinese and 'traditional' represents traditional Chinese.
  • theta controls the size of search space for the candidate phrases.
import dimsim

candidates = dimsim.get_candidates("大侠", mode="simplified", theta=1)
['打下', '大虾', '大侠']

candidates = dimsim.get_candidates("粉丝", mode="traditional", theta=1)
['門市', '分時', '焚屍', '粉飾', '粉絲']

Deploy from Docker Hub

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 codait/max-chinese-phonetic-similarity-estimator

This will pull a pre-built image from Docker Hub (or use an existing image if already cached locally) and run it. If you'd rather checkout and build the model locally you can follow the run locally steps below.

Deploy on Kubernetes

You can also deploy the model on Kubernetes using the latest docker image on Docker Hub.

On your Kubernetes cluster, run the following commands:

$ kubectl apply -f https://github.com/IBM/MAX-Chinese-Phonetic-Similarity-Estimator/raw/master/max-chinese-phonetic-similarity-estimator.yaml

The model will be available internally at port 5000, but can also be accessed externally through the NodePort.

Run Locally

  1. Build the Model
  2. Deploy the Model
  3. Use the Model
  4. Development
  5. Cleanup

1. Build the Model

Clone this repository locally. In a terminal, run the following command:

$ git clone https://github.com/IBM/MAX-Chinese-Phonetic-Similarity-Estimator.git

Change directory into the repository base folder:

$ cd MAX-Chinese-Phonetic-Similarity-Estimator

To build the docker image locally, run:

$ docker build -t max-chinese-phonetic-similarity-estimator .

All required model assets will be downloaded during the build process. Note that currently this docker image is CPU only (we will add support for GPU images later).

2. Deploy the Model

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 max-chinese-phonetic-similarity-estimator

3. Use the Model

The API server automatically generates an interactive Swagger documentation page. Go to http://localhost:5000 to load it. From there you can explore the API and also create test requests.

Use the model/predict endpoint to pass the input to the model. The input has one required field - first_word. The other inputs are optional. Providing a second_word would return distance between the first_word and second_word, in addition to the closest candidate words to both of them.

Other optional arguments are: theta - indicates the distance threshold for candidate words and controls the size of search space for the candidate words. Higher theta returns more candidate words. Default is 1. mode - indicates the output type of the Chinese characters - traditional or simplified. Default is simplified.

INSERT SWAGGER UI SCREENSHOT HERE

You can also test it on the command line, for example:

$ curl -X POST "http://localhost:5000/model/predict?first_word=%E5%A4%A7%E8%99%BE&second_word=%E5%A4%A7%E4%BE%A0&mode=simplified&theta=1" -H  "accept: application/json"

You should see a JSON response like that below:

{
  "status": "ok",
  "predictions": [
    {
      "distance": "0.0002380952380952381",
      "candidates": [
        [
          "打下",
          "大虾",
          "大侠"
        ],
        [
          "打下",
          "大虾",
          "大侠"
        ]
      ]
    }
  ]
}

This means the given words had a distance of 0.00024 between them, and they can be inferred to be very close. The candidate words contains list of candidate words.

4. Development

To run the Flask API app in debug mode, edit config.py to set DEBUG = True under the application settings. You will then need to rebuild the docker image (see step 1).

5. Cleanup

To stop the Docker container, type CTRL + C in your terminal.

About

Estimate the phonetic distance between Chinese words and get similar sounding candidate words.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 98.2%
  • Dockerfile 1.8%