-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISRDataHelper.m
135 lines (108 loc) · 4.63 KB
/
ISRDataHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// ISRDataHander.m
// MSC
//
// Created by ypzhao on 12-11-19.
// Copyright (c) 2012年 iflytek. All rights reserved.
//
#import "ISRDataHelper.h"
@implementation ISRDataHelper
/**
解析命令词返回的结果
****/
+ (NSString*)stringFromAsr:(NSString*)params;
{
NSMutableString * resultString = [[NSMutableString alloc]init];
NSString *inputString = nil;
NSArray *array = [params componentsSeparatedByString:@"\n"];
for (int index = 0; index < array.count; index++)
{
NSRange range;
NSString *line = [array objectAtIndex:index];
NSRange idRange = [line rangeOfString:@"id="];
NSRange nameRange = [line rangeOfString:@"name="];
NSRange confidenceRange = [line rangeOfString:@"confidence="];
NSRange grammarRange = [line rangeOfString:@" grammar="];
NSRange inputRange = [line rangeOfString:@"input="];
if (confidenceRange.length == 0 || grammarRange.length == 0 || inputRange.length == 0 )
{
continue;
}
//check nomatch
if (idRange.length!=0) {
NSUInteger idPosX = idRange.location + idRange.length;
NSUInteger idLength = nameRange.location - idPosX;
range = NSMakeRange(idPosX,idLength);
NSString *idValue = [[line substringWithRange:range]
stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet] ];
if ([idValue isEqualToString:@"nomatch"]) {
return @"";
}
}
//Get Confidence Value
NSUInteger confidencePosX = confidenceRange.location + confidenceRange.length;
NSUInteger confidenceLength = grammarRange.location - confidencePosX;
range = NSMakeRange(confidencePosX,confidenceLength);
NSString *score = [line substringWithRange:range];
NSUInteger inputStringPosX = inputRange.location + inputRange.length;
NSUInteger inputStringLength = line.length - inputStringPosX;
range = NSMakeRange(inputStringPosX , inputStringLength);
inputString = [line substringWithRange:range];
[resultString appendFormat:@"%@ 置信度%@\n",inputString, score];
}
return resultString;
}
/**
解析听写json格式的数据
params例如:
{"sn":1,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"w":"白日","sc":0}]},{"bg":0,"cw":[{"w":"依山","sc":0}]},{"bg":0,"cw":[{"w":"尽","sc":0}]},{"bg":0,"cw":[{"w":"黄河入海流","sc":0}]},{"bg":0,"cw":[{"w":"。","sc":0}]}]}
****/
+ (NSString *)stringFromJson:(NSString*)params
{
if (params == NULL) {
return nil;
}
NSMutableString *tempStr = [[NSMutableString alloc] init];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData: //返回的格式必须为utf8的,否则发生未知错误
[params dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
if (resultDic!= nil) {
NSArray *wordArray = [resultDic objectForKey:@"ws"];
for (int i = 0; i < [wordArray count]; i++) {
NSDictionary *wsDic = [wordArray objectAtIndex: i];
NSArray *cwArray = [wsDic objectForKey:@"cw"];
for (int j = 0; j < [cwArray count]; j++) {
NSDictionary *wDic = [cwArray objectAtIndex:j];
NSString *str = [wDic objectForKey:@"w"];
[tempStr appendString: str];
}
}
}
return tempStr;
}
/**
解析语法识别返回的结果
****/
+ (NSString *)stringFromABNFJson:(NSString*)params
{
if (params == NULL) {
return nil;
}
NSMutableString *tempStr = [[NSMutableString alloc] init];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:
[params dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSArray *wordArray = [resultDic objectForKey:@"ws"];
for (int i = 0; i < [wordArray count]; i++) {
NSDictionary *wsDic = [wordArray objectAtIndex: i];
NSArray *cwArray = [wsDic objectForKey:@"cw"];
for (int j = 0; j < [cwArray count]; j++) {
NSDictionary *wDic = [cwArray objectAtIndex:j];
NSString *str = [wDic objectForKey:@"w"];
// NSString *score = [wDic objectForKey:@"sc"];
[tempStr appendString: str];
// [tempStr appendFormat:@" 置信度:%@",score];
// [tempStr appendString: @"\n"];
}
}
return tempStr;
}
@end