Skip to content

Commit

Permalink
Add smart formatting parameter for swift-2.3 (#527)
Browse files Browse the repository at this point in the history
* Add smart formatting parameter

* Removed an extra empty line
  • Loading branch information
glennrfisher authored and sccheng committed Nov 9, 2016
1 parent 38a6660 commit 082b3b8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Source/SpeechToTextV1/Models/RecognitionSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ public struct RecognitionSettings: JSONEncodable {
/// If `true`, then profanity will be censored from the service's output, obscuring each
/// occurrence with a set of asterisks. The default is `true`.
public var filterProfanity: Bool?


/// Indicates whether dates, times, series of digits and numbers, phone numbers, currency values,
/// and Internet addresses are to be converted into more readable, conventional representations
/// in the final transcript of a recognition request. If true, smart formatting is performed;
/// if false (the default), no formatting is performed. Applies to US English transcription only.
public var smartFormatting: Bool?

/**
Initialize a `RecognitionSettings` object to set the parameters of a Watson Speech to
Text recognition request.
Expand Down Expand Up @@ -128,6 +134,9 @@ public struct RecognitionSettings: JSONEncodable {
if let filterProfanity = filterProfanity {
json["profanity_filter"] = .Bool(filterProfanity)
}
if let smartFormatting = smartFormatting {
json["smart_formatting"] = .Bool(smartFormatting)
}
return .Dictionary(json)
}
}
Expand Down
Binary file not shown.
44 changes: 44 additions & 0 deletions Source/SpeechToTextV1/Tests/SpeechToTextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class SpeechToTextTests: XCTestCase {
settings.wordConfidence = true
settings.timestamps = true
settings.filterProfanity = false
settings.smartFormatting = true

speechToText.recognize(file, settings: settings, model: "en-US_BroadbandModel", learningOptOut: true, failure: failWithError) { results in
self.validateSTTResults(results.results, settings: settings)
Expand Down Expand Up @@ -263,6 +264,7 @@ class SpeechToTextTests: XCTestCase {
settings.wordConfidence = true
settings.timestamps = true
settings.filterProfanity = false
settings.smartFormatting = true

speechToText.recognize(audio, settings: settings, model: "en-US_BroadbandModel", learningOptOut: true, failure: failWithError) { results in
self.validateSTTResults(results.results, settings: settings)
Expand All @@ -275,6 +277,48 @@ class SpeechToTextTests: XCTestCase {
}
waitForExpectations()
}

// MARK: - Transcribe Data with Smart Formatting

func testTranscribeStockAnnouncementCustomWAV() {
transcribeDataCustomForNumbers("StockAnnouncement", withExtension: "wav", format: .WAV, substring: "$152.37")
}

func transcribeDataCustomForNumbers(
filename: String,
withExtension: String,
format: AudioMediaType,
substring: String)
{
let description = "Transcribe an audio file with smart formatting."
let expectation = expectationWithDescription(description)

let bundle = NSBundle(forClass: self.dynamicType)
guard let file = bundle.URLForResource(filename, withExtension: withExtension) else {
XCTFail("Unable to locate \(filename).\(withExtension).")
return
}

guard let audio = NSData(contentsOfURL: file) else {
XCTFail("Unable to read \(filename).\(withExtension).")
return
}

var settings = RecognitionSettings(contentType: format)
settings.smartFormatting = true

speechToText.recognize(audio, settings: settings, model: "en-US_BroadbandModel", learningOptOut: true, failure: failWithError) { results in
self.validateSTTResults(results.results, settings: settings)
if results.results.last?.final == true {
let transcript = results.results.last?.alternatives.last?.transcript
XCTAssertNotNil(transcript)
XCTAssertGreaterThan(transcript!.characters.count, 0)
XCTAssertTrue(transcript!.containsString(substring))
expectation.fulfill()
}
}
waitForExpectations()
}

// MARK: - Transcribe Streaming

Expand Down
8 changes: 6 additions & 2 deletions WatsonDeveloperCloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@
7AC97FC61D8308690046124C /* opus_multistream.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A9EC81A1D79B76100507725 /* opus_multistream.h */; settings = {ATTRIBUTES = (Public, ); }; };
7AC97FC71D8308690046124C /* opus_types.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A9EC81B1D79B76100507725 /* opus_types.h */; settings = {ATTRIBUTES = (Public, ); }; };
7AC97FC81D830A020046124C /* os_types.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A9EC8151D79B76100507725 /* os_types.h */; settings = {ATTRIBUTES = (Public, ); }; };
7AF31BFB1D7A414D0040D944 /* SpeechToTextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AAA9A251CEE3026002C9564 /* SpeechToTextTests.swift */; };
7AFD74411D4F8A1600666173 /* Entity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AFD74401D4F8A1600666173 /* Entity.swift */; };
7AFD74431D4F8A3800666173 /* Intent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AFD74421D4F8A3800666173 /* Intent.swift */; };
7AFD74451D4F8A5200666173 /* OutputData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AFD74441D4F8A5200666173 /* OutputData.swift */; };
Expand Down Expand Up @@ -374,6 +373,8 @@
B1AAD3161D0F13FA002DAB84 /* car.png in Resources */ = {isa = PBXBuildFile; fileRef = B1AAD3141D0F13F5002DAB84 /* car.png */; };
B1C4DFD21D134E37004CC4B0 /* arsArticle.html in Resources */ = {isa = PBXBuildFile; fileRef = B1C4DFCF1D134DB4004CC4B0 /* arsArticle.html */; };
BE1733921D1FE60C003E8966 /* thomas.png in Resources */ = {isa = PBXBuildFile; fileRef = BE1733901D1FE557003E8966 /* thomas.png */; };
CEBA070D1DCCC971004E80A0 /* StockAnnouncement.wav in Resources */ = {isa = PBXBuildFile; fileRef = CEBA070B1DCCC95A004E80A0 /* StockAnnouncement.wav */; };
CEE38B291DCBCF6C0041FAE4 /* SpeechToTextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AAA9A251CEE3026002C9564 /* SpeechToTextTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -995,6 +996,7 @@
B1F21DF71CE2461800393146 /* ToneAnalyzerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToneAnalyzerTests.swift; sourceTree = "<group>"; };
B1F21DF81CE2461800393146 /* ToneAnalyzer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToneAnalyzer.swift; sourceTree = "<group>"; };
BE1733901D1FE557003E8966 /* thomas.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = thomas.png; sourceTree = "<group>"; };
CEBA070B1DCCC95A004E80A0 /* StockAnnouncement.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = StockAnnouncement.wav; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -1856,6 +1858,7 @@
7AAA9A211CEE3026002C9564 /* Audio Files */ = {
isa = PBXGroup;
children = (
CEBA070B1DCCC95A004E80A0 /* StockAnnouncement.wav */,
7AAA9A221CEE3026002C9564 /* SpeechSample.flac */,
7AAA9A231CEE3026002C9564 /* SpeechSample.ogg */,
7AAA9A241CEE3026002C9564 /* SpeechSample.wav */,
Expand Down Expand Up @@ -3303,6 +3306,7 @@
files = (
7AAAF4731CEE9FBA00B74848 /* SpeechSample.flac in Resources */,
7AAAF4741CEE9FBA00B74848 /* SpeechSample.ogg in Resources */,
CEBA070D1DCCC971004E80A0 /* StockAnnouncement.wav in Resources */,
7AAAF4751CEE9FBA00B74848 /* SpeechSample.wav in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -3664,7 +3668,7 @@
buildActionMask = 2147483647;
files = (
7A6C7B491D8C960300CD97FA /* Credentials.swift in Sources */,
7AF31BFB1D7A414D0040D944 /* SpeechToTextTests.swift in Sources */,
CEE38B291DCBCF6C0041FAE4 /* SpeechToTextTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 082b3b8

Please sign in to comment.