iOS - Machine Learning "Three"

Preface:

  To undertake the above, the last time just to find some information on the theory and codes, time recently wrote a demo, a collapse. . .

  Simply put this under the demo it, or want to be a critic of the automatic recognition model judge.

First, the construction of the training data

  1, I was prepared for such data is

[ 
    { 
        " Text " : " The movie looks beautiful " , " label " : " praise " 
    }, 
    { 
        " text " : " sucks " , " label " : " Poor " 
    }, 
    { 
        " text " : " like a general, not bad but also not good " , " label " : " Average " 
    },

    However, this data can not be directly used for training, the ML iOS currently does not support Chinese, so I turn the Chinese into hexadecimal, praise Poor assessment translated into strings 0,1 distinguished.

  2, the code

//1.读取JSON文件
    NSMutableArray *textList = [[NSMutableArray alloc] init];
    NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MLData" ofType:@"json"]];
    NSString *str  =[[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
    str = [self removeSpaceAndNewline:str];
//    NSLog(@"%@",str);
    
    NSError *error;
    NSArray *dataPathList = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&error];
     IF (dataPathList.count == 0 ) { 
        NSLog ( @ " JSON parsing failed " );
         return ; 
    } 
    
    // 2. JSON file processing Chinese characters coded into 64 
    [dataPathList enumerateObjectsUsingBlock: ^ (NSDictionary * obj, NSUInteger IDX, BOOL * _Nonnull STOP) { 
        NSString * moviceContent obj = [ @ " text " ]; // critic content 
        moviceContent = [Self hexStringFromString: moviceContent]; 
        
        NSString * moviceType obj = [ @ " label " ]; // s User type
        NSString *movieTypeNum = @"0";
        if ([moviceType isEqualToString:@"好评"]) {
            movieTypeNum = @"2";
        } else if ([moviceType isEqualToString:@"中评"]) {
            movieTypeNum = @"1";
        } else if ([moviceType isEqualToString:@"差评"]) {
            movieTypeNum = @"0";
        }
        NSDictionary *dict = @{@"text":moviceContent,@"label":movieTypeNum};
        [textList addObject:dict];
    }];
    
    //3.导出JSON文件
    NSData *whirtData =[NSJSONSerialization dataWithJSONObject:textList options:NSJSONWritingPrettyPrinted error:0];
    [whirtData writeToFile:@"/Users/sunjiaqi/Desktop/appsTrain.json" atomically:YES];
    NSLog(@"文件生成成功");
- (NSString *)removeSpaceAndNewline:(NSString *)str {
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"        " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"    " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"    " withString:@""];
    return temp;
}

- (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

  Note: There is a pit this point, my JSON file inside a lot more line breaks and spaces, has led to read out the data inside, toss a long time.

JSON files handled well after so long:

[
  {
    "label" : "2",
    "text" : "e8bf99e983a8e794b5e5bdb1e79c9fe5a5bde79c8b"
  },
  {
    "label" : "0",
    "text" : "e5a4aae78382e4ba86"
  },
  {
    "label" : "1",
    "text" : "e4b880e888ace888acefbc8ce4b88de7ae97e5b7aee4b99fe4b88de7ae97e5a5bd"
  },

  Then you can take this data to generate a model of the train.

Second, generate a model

Open playgroud, directly on the code runs

the Cocoa Import 
Import CreateMLUI 
Import CreateML 

var STR = " the Hello, Playground " 

// the let MLImageClassifierBuilder Builder = ()
 // builder.showInLiveView () 




// training source address of 
the let Data = the try MLDataTable (contentsOf: the URL (fileURLWithPath: " / the Users / sunjiaqi / Desktop / appsTrain.json " )) 

// import training data source 
the let sentimentClassifier = the try MLTextClassifier (trainingData: data, TextColumn: " text " , 
labelColumn: " label " ) 

//Accuracy evaluation model
 // the let evaluationMetrics = sentimentClassifier.evaluation (ON: Data, TextColumn: "text", labelColumn: "label")
 // the let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100
 // Print ( "evaluationAccuracy: \ (evaluationAccuracy) ") 


// export the model 
the let the Metadata = MLModelMetadata (author: " life Warriors " , 
                               shortDescription: " this judgment is a critic of the model " , 
                               Version: " 1.0 " )
 the try sentimentClassifier.write (to: the URL of (fileURLWithPath : "/ Users / sunjiaqi / Desktop / export model /SentimentClassifier.mlmodel " ), 
                              the Metadata: the Metadata)

  Note: This is the final model, and import it into demo which you can use.

 

Third, using the model

  1, into the model, like directly dragged

  2, the model build tools

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface SentimentClassifierModel : NSObject


+ (NSString *)judgeMoviceContentWith:(NSString *)content;

@end

NS_ASSUME_NONNULL_END

 

#import "SentimentClassifierModel.h"
#import "SentimentClassifier.h"

@implementation SentimentClassifierModel

+ (SentimentClassifier *)model {
    auto bundle = [NSBundle bundleForClass:SentimentClassifier.class];
    auto mlmodelcURL = [bundle URLForResource:@"SentimentClassifier" withExtension:@"mlmodelc"];
    if (mlmodelcURL) {
        return [SentimentClassifier new];
    }

    auto modelPath = [bundle pathForResource:@"SentimentClassifier" ofType:@"mlmodel"];
    if (!modelPath) return nil;

    auto modelURL = [NSURL fileURLWithPath:modelPath];
    mlmodelcURL = [MLModel compileModelAtURL:modelURL error:nil];
    if (!mlmodelcURL) return nil;

    auto model = [[SentimentClassifier alloc] initWithContentsOfURL:mlmodelcURL error:nil];
    return model;
}

+ (NSString *)judgeMoviceContentWith:(NSString *)content {
    NSString judgeResult = * @ " unrecognized " ; // 0- Poor, in the assessment 1-, 2- Good, 3-identifying failed 
    
    Auto Model = [Model Self]; 
    
    // process Content, turn determines the model to hexadecimal 
    = Content [Self hexStringFromString: Content]; 
    Auto Result = [Model predictionFromText: Content error: nil]; 
    NSLog ( @ " Result:% @ " , result.label);
     IF ([result.label of isEqual: @ " 0 " ] ) { 
        judgeResult = @ " Poor " ; 
    } the else  IF ([result.label of isEqual:@"1"]) {
        judgeResult = @"中评";
    } else if ([result.label isEqual:@"2"]) {
        judgeResult = @"好评";
    }
    return judgeResult;
}

+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

  Note: Note that here, a point is first passed in turn to hexadecimal critic again determined.

 

IV Summary

  1, the training data to a source inside the test is accurate, the accuracy of the other data bit touching, after all, there is no algorithm, test data is also small, optimized point a lot.

  2, consider the word to optimize the accuracy of the model, the model before the judge to make a judgment of the critic, looking for similarities between the text.

  3, the model just to give you a judgment as to the condition which is characterized by clear, this is related to the accuracy of the model.

 

Guess you like

Origin www.cnblogs.com/qiyiyifan/p/12357565.html