Skip to content

Commit

Permalink
fix(SpeechToTextV1): Fix grammarName and redaction parameters in reco…
Browse files Browse the repository at this point in the history
…gnize websocket methods
  • Loading branch information
Anthony Oliveri committed Jan 18, 2019
1 parent e8e4c9b commit 64b116c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 100 deletions.
61 changes: 2 additions & 59 deletions Source/SpeechToTextV1/SpeechToText+Recognize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@ extension SpeechToText {
- parameter learningOptOut: If `true`, then this request will not be logged for training.
- parameter customerID: Associates a customer ID with all data that is passed over the connection.
By default, no customer ID is associated with the data.
- parameter grammarName: The name of a grammar that is to be used with the recognition request. If you specify a
grammar, you must also use the `language_customization_id` parameter to specify the name of the custom language
model for which the grammar is defined. The service recognizes only strings that are recognized by the specified
grammar; it does not recognize other custom words from the model's words resource. See
[Grammars](https://cloud.ibm.com/docs/services/speech-to-text/output.html).
- parameter redaction: If `true`, the service redacts, or masks, numeric data from final transcripts. The feature
redacts any number that has three or more consecutive digits by replacing each digit with an `X` character. It is
intended to redact sensitive numeric data, such as credit card numbers. By default, the service performs no
redaction.
When you enable redaction, the service automatically enables smart formatting, regardless of whether you
explicitly disable that feature. To ensure maximum security, the service also disables keyword spotting (ignores
the `keywords` and `keywords_threshold` parameters) and returns only a single final transcript (forces the
`max_alternatives` parameter to be `1`).
**Note:** Applies to US English, Japanese, and Korean transcription only.
See [Numeric redaction](https://cloud.ibm.com/docs/services/speech-to-text/output.html#redaction).
- parameter completionHandler: A function executed when the request completes with a successful result or error
*/
public func recognize(
Expand All @@ -70,8 +55,6 @@ extension SpeechToText {
languageCustomizationID: String? = nil,
learningOptOut: Bool? = nil,
customerID: String? = nil,
grammarName: String? = nil,
redaction: Bool? = nil,
completionHandler: @escaping (WatsonResponse<SpeechRecognitionResults>?, WatsonError?) -> Void)
{
do {
Expand All @@ -84,8 +67,6 @@ extension SpeechToText {
languageCustomizationID: languageCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction,
completionHandler: completionHandler
)
} catch {
Expand Down Expand Up @@ -119,21 +100,6 @@ extension SpeechToText {
- parameter learningOptOut: If `true`, then this request will not be logged for training.
- parameter customerID: Associates a customer ID with all data that is passed over the connection.
By default, no customer ID is associated with the data.
- parameter grammarName: The name of a grammar that is to be used with the recognition request. If you specify a
grammar, you must also use the `language_customization_id` parameter to specify the name of the custom language
model for which the grammar is defined. The service recognizes only strings that are recognized by the specified
grammar; it does not recognize other custom words from the model's words resource. See
[Grammars](https://cloud.ibm.com/docs/services/speech-to-text/output.html).
- parameter redaction: If `true`, the service redacts, or masks, numeric data from final transcripts. The feature
redacts any number that has three or more consecutive digits by replacing each digit with an `X` character. It is
intended to redact sensitive numeric data, such as credit card numbers. By default, the service performs no
redaction.
When you enable redaction, the service automatically enables smart formatting, regardless of whether you
explicitly disable that feature. To ensure maximum security, the service also disables keyword spotting (ignores
the `keywords` and `keywords_threshold` parameters) and returns only a single final transcript (forces the
`max_alternatives` parameter to be `1`).
**Note:** Applies to US English, Japanese, and Korean transcription only.
See [Numeric redaction](https://cloud.ibm.com/docs/services/speech-to-text/output.html#redaction).
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter completionHandler: A function executed when the request completes with a successful result or error
*/
Expand All @@ -146,8 +112,6 @@ extension SpeechToText {
acousticCustomizationID: String? = nil,
learningOptOut: Bool? = nil,
customerID: String? = nil,
grammarName: String? = nil,
redaction: Bool? = nil,
headers: [String: String]? = nil,
completionHandler: @escaping (WatsonResponse<SpeechRecognitionResults>?, WatsonError?) -> Void)
{
Expand All @@ -159,9 +123,7 @@ extension SpeechToText {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction
customerID: customerID
)

// set urls
Expand Down Expand Up @@ -228,21 +190,6 @@ extension SpeechToText {
By default, no customer ID is associated with the data.
- parameter compress: Should microphone audio be compressed to Opus format?
(Opus compression reduces latency and bandwidth.)
- parameter grammarName: The name of a grammar that is to be used with the recognition request. If you specify a
grammar, you must also use the `language_customization_id` parameter to specify the name of the custom language
model for which the grammar is defined. The service recognizes only strings that are recognized by the specified
grammar; it does not recognize other custom words from the model's words resource. See
[Grammars](https://cloud.ibm.com/docs/services/speech-to-text/output.html).
- parameter redaction: If `true`, the service redacts, or masks, numeric data from final transcripts. The feature
redacts any number that has three or more consecutive digits by replacing each digit with an `X` character. It is
intended to redact sensitive numeric data, such as credit card numbers. By default, the service performs no
redaction.
When you enable redaction, the service automatically enables smart formatting, regardless of whether you
explicitly disable that feature. To ensure maximum security, the service also disables keyword spotting (ignores
the `keywords` and `keywords_threshold` parameters) and returns only a single final transcript (forces the
`max_alternatives` parameter to be `1`).
**Note:** Applies to US English, Japanese, and Korean transcription only.
See [Numeric redaction](https://cloud.ibm.com/docs/services/speech-to-text/output.html#redaction).
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter completionHandler: A function executed when the request completes with a successful result or error
*/
Expand All @@ -255,8 +202,6 @@ extension SpeechToText {
learningOptOut: Bool? = nil,
customerID: String? = nil,
compress: Bool = true,
grammarName: String? = nil,
redaction: Bool? = nil,
headers: [String: String]? = nil,
completionHandler: @escaping (WatsonResponse<SpeechRecognitionResults>?, WatsonError?) -> Void)
{
Expand Down Expand Up @@ -288,9 +233,7 @@ extension SpeechToText {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction
customerID: customerID
)

// set urls
Expand Down
21 changes: 21 additions & 0 deletions Source/SpeechToTextV1/WebSockets/RecognitionSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ public struct RecognitionSettings: Codable, Equatable {
/// If `true`, then speaker labels will be returned for each timestamp. The default is `false`.
public var speakerLabels: Bool?

/// The name of a grammar that is to be used with the recognition request. If you specify a
/// grammar, you must also use the `language_customization_id` parameter to specify the name of the custom language
/// model for which the grammar is defined. The service recognizes only strings that are recognized by the specified
/// grammar; it does not recognize other custom words from the model's words resource. See
/// [Grammars](https://cloud.ibm.com/docs/services/speech-to-text/output.html).
public var grammarName: String?

/// If `true`, the service redacts, or masks, numeric data from final transcripts. The feature
/// redacts any number that has three or more consecutive digits by replacing each digit with an `X` character. It is
/// intended to redact sensitive numeric data, such as credit card numbers. By default, the service performs no
/// redaction.
/// When you enable redaction, the service automatically enables smart formatting, regardless of whether you
/// explicitly disable that feature. To ensure maximum security, the service also disables keyword spotting (ignores
/// the `keywords` and `keywords_threshold` parameters) and returns only a single final transcript (forces the
/// `max_alternatives` parameter to be `1`).
/// **Note:** Applies to US English, Japanese, and Korean transcription only.
/// See [Numeric redaction](https://cloud.ibm.com/docs/services/speech-to-text/output.html#redaction).
public var redaction: Bool?

/**
Initialize a `RecognitionSettings` object to set the parameters of a Watson Speech to
Text recognition request.
Expand Down Expand Up @@ -116,5 +135,7 @@ public struct RecognitionSettings: Codable, Equatable {
case filterProfanity = "profanity_filter"
case smartFormatting = "smart_formatting"
case speakerLabels = "speaker_labels"
case grammarName = "grammar_name"
case redaction = "redaction"
}
}
28 changes: 6 additions & 22 deletions Source/SpeechToTextV1/WebSockets/SpeechToTextSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ public class SpeechToTextSession {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction
customerID: customerID
)!
var socket = SpeechToTextSocket(
url: url,
Expand Down Expand Up @@ -121,8 +119,6 @@ public class SpeechToTextSession {
private let acousticCustomizationID: String?
private let learningOptOut: Bool?
private let customerID: String?
private let grammarName: String?
private let redaction: Bool?

internal init(
authMethod: AuthenticationMethod,
Expand All @@ -131,9 +127,7 @@ public class SpeechToTextSession {
languageCustomizationID: String? = nil,
acousticCustomizationID: String? = nil,
learningOptOut: Bool? = nil,
customerID: String? = nil,
grammarName: String? = nil,
redaction: Bool? = nil)
customerID: String? = nil)
{
self.authMethod = authMethod
self.model = model
Expand All @@ -142,8 +136,6 @@ public class SpeechToTextSession {
self.acousticCustomizationID = acousticCustomizationID
self.learningOptOut = learningOptOut
self.customerID = customerID
self.grammarName = grammarName
self.redaction = redaction

recorder = SpeechToTextRecorder()
// swiftlint:disable:next force_try
Expand Down Expand Up @@ -187,9 +179,7 @@ public class SpeechToTextSession {
languageCustomizationID: String? = nil,
acousticCustomizationID: String? = nil,
learningOptOut: Bool? = nil,
customerID: String? = nil,
grammarName: String? = nil,
redaction: Bool? = nil)
customerID: String? = nil)
{
let authMethod = BasicAuthentication(username: username, password: password)
self.init(
Expand All @@ -199,9 +189,7 @@ public class SpeechToTextSession {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction)
customerID: customerID)
}

/**
Expand Down Expand Up @@ -237,9 +225,7 @@ public class SpeechToTextSession {
languageCustomizationID: String? = nil,
acousticCustomizationID: String? = nil,
learningOptOut: Bool? = nil,
customerID: String? = nil,
grammarName: String? = nil,
redaction: Bool? = nil)
customerID: String? = nil)
{
let authMethod = IAMAuthentication(apiKey: apiKey, url: iamUrl)
self.init(
Expand All @@ -249,9 +235,7 @@ public class SpeechToTextSession {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction
customerID: customerID
)
}

Expand Down
10 changes: 1 addition & 9 deletions Source/SpeechToTextV1/WebSockets/SpeechToTextSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ internal class SpeechToTextSocket: WebSocketDelegate {
languageCustomizationID: String?,
acousticCustomizationID: String?,
learningOptOut: Bool?,
customerID: String?,
grammarName: String?,
redaction: Bool?) -> URL?
customerID: String?) -> URL?
{
var queryParameters = [URLQueryItem]()
if let model = model {
Expand All @@ -190,12 +188,6 @@ internal class SpeechToTextSocket: WebSocketDelegate {
let value = "customer_id=\(customerID)"
queryParameters.append(URLQueryItem(name: "x-watson-metadata", value: value))
}
if let grammarName = grammarName {
queryParameters.append(URLQueryItem(name: "grammar_name", value: grammarName))
}
if let redaction = redaction {
queryParameters.append(URLQueryItem(name: "redaction", value: redaction.description))
}
var urlComponents = URLComponents(string: url)
if !queryParameters.isEmpty {
urlComponents?.queryItems = queryParameters
Expand Down
14 changes: 4 additions & 10 deletions Tests/SpeechToTextV1Tests/SpeechToTextUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class SpeechToTextUnitTests: XCTestCase {
let acousticCustomizationID = "456"
let learningOptOut = true
let customerID = "Anthony"
let grammarName = "example"
let redaction = true

// API Key authentication
var sttSession = SpeechToTextSession(
Expand All @@ -86,12 +84,10 @@ class SpeechToTextUnitTests: XCTestCase {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction)
customerID: customerID)

var socket = sttSession.socket
var expectedURL = "\(sttSession.websocketsURL)?model=\(model)&base_model_version=\(baseModelVersion)&language_customization_id=\(languageCustomizationID)&acoustic_customization_id=\(acousticCustomizationID)&x-watson-learning-opt-out=\(learningOptOut)&x-watson-metadata=customer_id%3D\(customerID)&grammar_name=\(grammarName)&redaction=\(redaction)"
var expectedURL = "\(sttSession.websocketsURL)?model=\(model)&base_model_version=\(baseModelVersion)&language_customization_id=\(languageCustomizationID)&acoustic_customization_id=\(acousticCustomizationID)&x-watson-learning-opt-out=\(learningOptOut)&x-watson-metadata=customer_id%3D\(customerID)"
XCTAssertEqual(socket.url, URL(string: expectedURL))

// Same as above, but with Basic Auth instead of API Key in the initializer
Expand All @@ -103,12 +99,10 @@ class SpeechToTextUnitTests: XCTestCase {
languageCustomizationID: languageCustomizationID,
acousticCustomizationID: acousticCustomizationID,
learningOptOut: learningOptOut,
customerID: customerID,
grammarName: grammarName,
redaction: redaction)
customerID: customerID)

socket = sttSession.socket
expectedURL = "\(sttSession.websocketsURL)?model=\(model)&base_model_version=\(baseModelVersion)&language_customization_id=\(languageCustomizationID)&acoustic_customization_id=\(acousticCustomizationID)&x-watson-learning-opt-out=\(learningOptOut)&x-watson-metadata=customer_id%3D\(customerID)&grammar_name=\(grammarName)&redaction=\(redaction)"
expectedURL = "\(sttSession.websocketsURL)?model=\(model)&base_model_version=\(baseModelVersion)&language_customization_id=\(languageCustomizationID)&acoustic_customization_id=\(acousticCustomizationID)&x-watson-learning-opt-out=\(learningOptOut)&x-watson-metadata=customer_id%3D\(customerID)"
XCTAssertEqual(socket.url, URL(string: expectedURL))
}
}

0 comments on commit 64b116c

Please sign in to comment.