Use OLAMISDK to implement a 24-point computing iOS program for voice input numbers.

Source: Internet
Author: User

Use OLAMISDK to implement a 24-point computing iOS program for voice input numbers.
Preface

In current software applications, the input mode is mainly text input, but the language input mode is more and more widely used. This is a 24-point iOS program written using the Olami SDK. It is input through speech.
The following url describes the Olami SDK:
Https://cn.olami.ai/wiki? Mp = sdk & content = sdk/ios/reference.html
The URL details the Olami SDK contains the functions and defined delegation.

App implementation

The following describes how to use the SDK through the program.
This APP can be downloaded in https://github.com/lym-ay/OlamiRecognizerMath24

olamiRecognizer= [[OlamiRecognizer alloc] init];olamiRecognizer.delegate = self;

2. Call the setAuthorization function for authorization

[olamiRecognizer setAuthorization:@"d13bbcbef2a4460dbf19ced850eb5d83"    api:@"asr" appSecret:@"3b08b349c0924a79869153bea334dd86" cusid:OLACUSID];

The parameter descriptions of this function are described in OlamiRecognizer. You can also view the parameters in the online API description.
Https://cn.olami.ai/wiki? Mp = sdk & content = sdk/ios/reference.html
Some parameters must be registered on the development platform of Olami can be obtained, the URL is a https://olami.ai, registration login after creating the application can be seen

3. Set language families

[olamiRecognizer setLocalization:LANGUAGE_SIMPLIFIED_CHINESE];

Before recording a video, you must first set it. Otherwise, no results will be obtained. Currently, only Simplified Chinese (LANGUAGE_SIMPLIFIED_CHINESE) is supported)

4. Start recording
Call the start () API to start recording.

[olamiRecognizer start];

5. obtain and process the text and semantics of the recording.
By calling the stop () function or automatically stopping the recording, you can obtain the text of the recording and the semantic analysis results of the recording.
You can obtain the result by implementing the OlamiRecognizerDelegate onResult function. The result is called back in the form of a json string. You can obtain the desired number by parsing the string. For example, if you say "2345 is" to the microphone, the result is as follows:

{"Data": {"asr": {"result": "24 o'clock on April 9, 2345", "speech_status": 0, "final": true, "status ": 0}, "nli": [{"desc_obj": {"status": 0}, "semantic": [{"app": "math24", "input ": "24: 00 on December 31, 2345", "slots": [{"num_detail": {"recommend_value": "", "type": "number"}, "name ": "number3", "value": "4" },{ "num_detail": {"recommend_value": "", "type": "number"}, "name ": "number4", "value": "5" },{ "num_detail": {"recommend_value": "", "type": "number"}, "name ": "number1", "value": "2" },{ "num_detail": {"recommend_value": "", "type": "number"}, "name ": "number2", "value": "3"}], "modifier": ["play_calculate"], "customer": "58df685e84ae11f0bb7b4893"}], "type ": "math24"}]}, "status": "OK "}

This is a set of rules defined according to the OSL syntax to describe the returned results. Description of this result in https://cn.olami.ai/wiki? Mp=api_nlu&content=api_nlu3.html.

You may wonder how the APP knows what I mean? This involves the OSL Syntax description Language. OLAMI Syntax Language (OSL) is a unique Syntax markup Language developed by the OLAMI platform for natural Language processing, natural Language Interaction (NLI) management system uses OSL to replace complex coding and programming. It is simple to use, easy to learn, flexible and flexible. You can view detailed instructions on this website.
Https://cn.olami.ai/wiki? Mpw.osl&content=osl1.html
Before writing this APP, a set of syntaxes will be compiled according to the requirements of OSL. This syntax can be understood by the Olami server, analyzed in semantics, and the result is the above json string. There are some fields of modules written on the Olami official website, which can be used directly. In https://cn.olami.ai/wiki? The mpw.nli&content=nli1.html URL shows how to use subsequent modules. This 24-point method uses existing modules to write code.

6. Create an application, set and import grammar
First, log on to and register with the olami homepage. Go to this page after login

On this page, you can see my application math24 and the key

Of course, this page must be available only after the application is created. Click "create new application" and go to the following page.

Enter the application name and application description. After the application introduction, you can create the application. Go back to the previous page and you will see the created application.

Click "Enter NLI system" to go to the module page.

Grammar has been built in many fields on the official website. On the module page, click "import" to view the modules in the existing fields.

Select a module to use. For example, if you want to import the "math24" module, select it and click "import ".
 

Go to the math24 module and you will see the example

However, it cannot be used at this time and must be released first. Click "release" at the top of the page to go to the release page.

Click "release ".

The release is successful. Now you can use the 24 module.

Olami also provides the grammar test function. You can click the "test" button to perform a test on the page without having to develop an APP first.

Finally, return to the "My applications" Page and click "Configure NLI module" to associate the applications and modules you have created.

7. Description of the onResult Function
In the entire program, the most important function is the onResult function.

-(Void) onResult :( NSData *) result {NSError * error; _ weak typeof (self) weakSelf = self; NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: result options: Unknown error: & error]; if (error) {NSLog (@ "error is % @", error. localizedDescription);} else {NSString * jsonStr = [[NSString alloc] initWithData: result encoding: NSUTF8StringEncoding]; NSLog (@ "jsonStr is % @", jsonStr); N SString * OK = [dic objectForKey: @ "status"]; if ([OK isEqualToString: @ "OK"]) {NSDictionary * dicData = [dic objectForKey: @ "data"]; NSDictionary * asr = [dicData objectForKey: @ "asr"]; if (asr) {// if asr is not empty, description: it is currently the speech input [weakSelf processASR: asr];} NSDictionary * nli = [[dicData objectForKey: @ "nli"] objectAtIndex: 0]; NSDictionary * desc = [nli objectForKey: @ "desc_obj"]; int status = [[desc objectForKey: @ "st Atus "] intValue]; if (status! = 0) {// 0 indicates that the status is normal, and the non-zero status is abnormal NSString * result = [desc objectForKey: @ "result"]; dispatch_async (dispatch_get_main_queue (), ^ {_ resultTextView. text = result ;}) ;}else {NSDictionary * semantic = [[nli objectForKey: @ "semantic"] objectAtIndex: 0]; [weakSelf processSemantic: semantic];} else {dispatch_async (dispatch_get_main_queue (), ^ {_ resultTextView. text = @ "Please name the number of 4 in 10 ";});}}}

This function is used to process the passed results.

In this function, three functions are called to process three important nodes in the josn format.

-(Void) processASR :( NSDictionary *) asrDic {NSString * result = [asrDic objectForKey: @ "result"]; if (result. length = 0) {// if the result is null, the warning box UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @ "no voice is received. Please enter it again! "Message: nil preferredStyle: Regular]; [self presentViewController: alertController animated: YES completion: ^ {interval time = dispatch_time (DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC); dispatch_after (time, dispatch_get_main_queue (), ^ {[alertController dismissViewControllerAnimated: YES completion: nil] ;}] ;}else {dispatch_async (dispatch_get_main_queue (), ^ {NSString * str = [result stringByReplacingOccurrencesOfString: @ "" withString: @ ""]; // remove the space in the middle of the character _ inputTextView. text = str ;});}}

This is used to process the ASR node and obtain the speech recognition result. If there is no result, a dialog box is displayed. The ASR recognized text is displayed in the first TextView.

- (void)processSemantic:(NSDictionary*)semanticDic {    NSArray *slot = [semanticDic objectForKey:@"slots"];    [_slotValue removeAllObjects];    if (slot.count != 0) {        for (NSDictionary *dic in slot) {            NSString* val = [dic objectForKey:@"value"];            [_slotValue addObject:val];        }    }    NSArray *modify = [semanticDic objectForKey:@"modifier"];    if (modify.count != 0) {        for (NSString *s in modify) {            [self processModify:s];        }    }}

This is used to process Semantic nodes. This node contains the slot value and the modifier value. The slot in the OSL syntax description language can be understood as a variable in the semantics. It is used to transmit and extract information, and is the source of data processed by code. For the 24-point program, it is the source of the number of 4 for calculation. The slot value can refer to the https://cn.olami.ai/wiki? Mpw.osl&content=osl_slot.html, which is described in detail here. In the 24-point program, the number we want to calculate is obtained from here.

-(Void) processModify :( NSString *) str {if ([str isEqualToString: @ "play_want"] | [str isEqualToString: @ "play_want_ask"] | [str isEqualToString: @ "needmore"] | [str isEqualToString: @ "needmore_ask"]) {// requires the user to enter the dispatch_async (dispatch_get_main_queue (), ^ {_ resultTextView. text = @ "Please specify the number of four in 10" ;}) ;}else if ([str isinclutostring: @ "rules"]) {dispatch_async (dispatch_get_main_queue (), ^ {_ resultTextView. text = @ "the result of the four numeric operations is twenty-four" ;}) ;}else if ([str isEqualToString: @ "play_calculate"]) {NSString * str = [[Math24 shareInstance] calculate: _ slotValue]; dispatch_async (dispatch_get_main_queue (), ^ {_ resultTextView. text = str ;});} else if ([str isEqualToString: @ "attention"]) {dispatch_async (dispatch_get_main_queue (), ^ {_ resultTextView. text = @ "the four numbers must be less than 10, and cannot exceed 10 ";});}}

This is used to process the speech and semantic results. This function is mainly used to process the modifier node in the json string. In the OSL syntax description language, the modifier syntax description rule is a built-in information transfer mechanism other than slot, which is generally used to represent the semantic purpose, it can also be understood as a semantic annotation method, so that application developers can learn the corresponding intent represented by grammar. For more information, see
Https://cn.olami.ai/wiki? Mpw.osl&content=osl_regex.html #11. Through modifier, we can know what the program intends? For example, whether you want to ask questions or calculate the results.

As shown in the code above, we defined 7 modifiers at 24 o'clock, which can be guessed literally. These can be customized in the OSL syntax, and then obtained through the Josn string for processing in the program. This is a judgment mechanism for our program to process.

Download Resources

You can download it from the csdn download channel.
Http://download.csdn.net/detail/dfman1978/9840447

Github
Https://github.com/lym-ay/OlamiRecognizerMath24

In addition, there are several articles on developing programs using the Olami SDK.

This is a program for listening to books.
Http://blog.csdn.net/ls0609/article/details/71519203

This is a weather program.
Http://blog.csdn.net/zhangxy0605/article/details/71601604

This is a calendar demo developed based on the OLAMI platform.
Http://blog.csdn.net/xinfinityx/article/details/72840977

Related Article

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.