An improved algorithm for iOS Chinese character recognition

Source: Internet
Author: User

Brief introduction

The previous article introduced the new addition in the class of the first version of the Chinese character function according to pinyin writing algorithm implementation, today said a modified algorithm.

After the launch of the 3.2 version of the newsletter, we have improved the Chinese character recognition algorithm, and the module has been drawn out to perfect a new application-write PA, has now passed AppStore audit on-line, welcome to download the experience.


go to the chase

In the first version, we split the entire word into several strokes into the database, each stroke record corresponds to several point records, the template and user records are the same, when comparing the template and user data, the corresponding records are compared according to the stroke ID, and then the score is calculated based on the matching results. In the new version, we model all the strokes in the database, and then each word corresponds to several stroke models. After the user writes the submission, analyzes each stroke entered by the user, matches a number of strokes in the Template table, and then looks at the consistency of the template to obtain a score. The following is a detailed procedure

1. Refining the Stroke model

On the internet to find 31 strokes, so 31 strokes one by one input, and then observe its characteristics. Finally, we decided to divide each stroke into several segments, such as a cross-fold hook, and split it into three segments. The specific method is: When writing a stroke, the calculation of the adjacent two points into a straight line and the angle of the horizontal line, when this angle and the first two points into a straight line angle of the difference is greater than a certain value (such as 15 degrees), it is considered that the stroke more than a paragraph. Finally, the stroke ID and the average angle of the segment are stored in the database, so that one stroke corresponds to several segments.

Parse Stroke model-(void) featurepoints {Nsarray *array = Self.gridarray;        if ([Array Count] < 4) {self.gridarray = Self.pointarray;    Array = Self.pointarray;    } Cgpoint Point;    Cgpoint point0;    Cgpoint point1;    Cgpoint Point2;        int tag = 0;    Double angle0 = 0.0;    Double angle1 = 0.0;    Double angle3 = 0.0;            for (int i=0; I<[array count]; i++) {if (i > 0) {point1 = [array[i-1] cgpointvalue];                        Point2 = [array[i] cgpointvalue];            Angle1 = atan2 (point2.y-point1.y, point2.x-point1.x) *180/m_pi;            if (Angle1 < -180) {angle1 + = 360;                } if (i > 1) {point0 = [array[i-2] cgpointvalue];                ANGLE0 = atan2 (point1.y-point0.y, point1.x-point0.x) *180/m_pi;                    if ((Fabs (ANGLE1-ANGLE0) >180 && 360-fabs (ANGLE1-ANGLE0) >36) | | fabs (ANGLE1-ANGLE0) > 36) {               Tag + = 1; } else if (i > 2) {point = [array[i-3] cgpointvalue];                    Angle3 = atan2 (Point0.y-point.y, point0.x-point.x) *180/m_pi; if (Fabs (angle3-angle1) >36 | | (Fabs (angle3-angle1) >180 && 360-fabs (angle3-angle1) >36))                    {tag + = 1;        }}}} [Self.anglearray Addobject:[nsnumber numberwithdouble:angle1]];    [Self.angletagarray Addobject:[nsnumber Numberwithint:tag]; }}

2. Enter the template

Manual input of Chinese characters template, each input a word, according to the comparison of strokes, find out the matching strokes, the stroke ID into the Chinese record, the last of each Chinese character corresponding to a number of strokes.


Input Chinese character template-(void) InsertStandardStroke2: (Charactermodel *) Model {nsmutabledictionary *dict = [Nsmutabledictionary D    ictionary];//records the number of repeated strokes nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];    NSNumber *widthnumber = [Userdefaults objectforkey:wtdefaultscanvasviewwidth];    NSNumber *heightnumber = [Userdefaults objectforkey:wtdefaultscanvasviewheight];    float canvasviewwidth = Widthnumber.floatvalue;        float canvasviewheight = Heightnumber.floatvalue; /**************** Insert Word **************/[self.hanzidb executeupdate:@ "insert into s_character (Chinese, pinyin) values (?,        ?) ", Model.chinese, Model.pinyin]; /**************** get word id**************/fmresultset* set0 = [self.hanzidb executequery:@ "select Max (ID) as Maxcharid fro    M S_character "];    int charid =-1;    if ([Set0 next]) {Charid = [set0 intforcolumn:@ "Maxcharid"];    } else {return;    } Nsarray *strokemodelarray = Model.strokemodelarray; for (int strokeindex=0; strokeindex<strokemodelarray.count;        strokeindex++) {Strokemodel *strokemodel = Strokemodelarray[strokeindex];                [Self clearusertable];        /**************** Insert Stroke **************/cgpoint minpoint = [Model.minpointvalue cgpointvalue];        Cgpoint maxpoint = [Model.maxpointvalue cgpointvalue]; [Self.hanzidb executeupdate:@ "INSERT into U_stroke (MinX, Miny, MaxX, Maxy) VALUES (?,?,?,?)", Minpoint.x, Minpoint.y,                MAXPOINT.X,MAXPOINT.Y];  /**************** gets the id**************/of the stroke nsstring *querystrokeid = [NSString stringwithformat:@ "select Max (ID) maxid        From U_stroke "];        fmresultset* Setstrokeid = [Self.hanzidb Executequery:querystrokeid];        int Strokeid =-1;        if ([Setstrokeid next]) {Strokeid = [Setstrokeid intforcolumn:@ "Maxid"];        } else {return;        }/**************** insertion point and angle **************/nsarray *pointsarray1 = Strokemodel.gridarray; Nsarray *anGleArray1 = Strokemodel.anglearray;                Nsarray *angletagarray1 = Strokemodel.angletagarray;            for (int i=0; i<pointsarray1.count; i++) {nsvalue *pointvalue = pointsarray1[i];            NSNumber *angletagnumber = Angletagarray1[i];            CGFloat angle = [anglearray1[i] floatvalue];                        Cgpoint point = [Pointvalue cgpointvalue]; NSString *SS = [NSString stringwithformat:@ "INSERT into U_angle (Strokeid, pointx, pointy, angle, Angletag) VALUES (%d,%.2f            ,%.2f,%.2f,%d) ", Strokeid,point.x,point.y,angle, Angletagnumber.intvalue];        [Self.hanzidb EXECUTEUPDATE:SS]; }//Refine break each stroke into segments [self.hanzidb executeupdate:@ "INSERT into U_line (Strokeid, Pointcount, Angle, an Gletag) Select Strokeid, COUNT (*), sum (angle)/count (*), Angletag from U_angle Group by Strokeid, Angletag "];<span style = "White-space:pre" ></span>//...}

3. Determine user input

When judging user input, only need to compare the stroke ID of the corresponding Chinese character template, the specific code will not be served (⊙﹏⊙ B is not dare to open)


Ending

Waiting for more than a week, write PA online, the results just on the line, the friends are playing bad ...

There are such things as:



There's still this.





No more talking, go back and change the algorithm.

An improved algorithm for iOS Chinese character recognition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.