-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generic Conversation requests (#397)
* Add initializer and toJSON for Conversation models * Add entities, intents, and output to Conversation message request * Add generic Conversation message endpoint * Add tests for generic Conversation message endpoint
- Loading branch information
1 parent
97a7176
commit 1407448
Showing
10 changed files
with
488 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright IBM Corporation 2016 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
**/ | ||
|
||
import Foundation | ||
import Freddy | ||
|
||
/** A term from the request that was identified as an entity. */ | ||
public struct Entity: JSONEncodable, JSONDecodable { | ||
|
||
/// The name of the recognized entity. | ||
public let entity: String? | ||
|
||
/// The location where the entity value begins and ends in the input text. | ||
public let location: EntityLocation? | ||
|
||
/// The term in the input text that was recognized. | ||
public let value: String? | ||
|
||
/** | ||
Create an `Entity`. | ||
- parameter entity: The name of the recognized entity. | ||
- parameter location: The location where the entity value begins and ends in the input text. | ||
- parameter value: The term in the input text that was recognized. | ||
*/ | ||
public init(entity: String?, location: EntityLocation?, value: String?) { | ||
self.entity = entity | ||
self.location = location | ||
self.value = value | ||
} | ||
|
||
/// Used internally to initialize an `Entity` model from JSON. | ||
public init(json: JSON) throws { | ||
entity = try? json.string("entity") | ||
location = try? json.decode("location") | ||
value = try? json.string("value") | ||
} | ||
|
||
/// Used internally to serialize an `Entity` model to JSON. | ||
public func toJSON() -> JSON { | ||
var json = [String: JSON]() | ||
if let entity = entity { | ||
json["entity"] = .String(entity) | ||
} | ||
if let location = location { | ||
json["location"] = location.toJSON() | ||
} | ||
if let value = value { | ||
json["value"] = .String(value) | ||
} | ||
return JSON.Dictionary(json) | ||
} | ||
} | ||
|
||
/** The location where an entity value begins and ends in the input text. */ | ||
public struct EntityLocation: JSONEncodable, JSONDecodable { | ||
|
||
/// The zero-based character offset that indicates | ||
/// where an entity value begins in the input text. | ||
public let startIndex: Int | ||
|
||
/// The zero-based character offset that indicates | ||
/// where an entity value ends in the input text. | ||
public let endIndex: Int | ||
|
||
/** | ||
Create an `EntityLocation`. | ||
- parameter startIndex: The zero-based character offset that | ||
indicates where an entity value begins in the input text. | ||
- parameter endIndex: The zero-based character offset that | ||
indicates where an entity value ends in the input text. | ||
*/ | ||
public init(startIndex: Int, endIndex: Int) { | ||
self.startIndex = startIndex | ||
self.endIndex = endIndex | ||
} | ||
|
||
/// Used internally to initialize an `EntityLocation` model from JSON. | ||
public init(json: JSON) throws { | ||
let indices = try json.arrayOf(type: Swift.Int) | ||
startIndex = indices[0] | ||
endIndex = indices[1] | ||
} | ||
|
||
/// Used internally to serialize an `EntityLocation` model to JSON. | ||
public func toJSON() -> JSON { | ||
let json = [JSON.Int(startIndex), JSON.Int(endIndex)] | ||
return JSON.Array(json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright IBM Corporation 2016 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
**/ | ||
|
||
import Foundation | ||
import Freddy | ||
|
||
/** A term from the request that was identified as an intent. */ | ||
public struct Intent: JSONEncodable, JSONDecodable { | ||
|
||
/// The name of the recognized intent. | ||
public let intent: String? | ||
|
||
/// The confidence score of the intent, between 0 and 1. | ||
public let confidence: Double? | ||
|
||
/** | ||
Create an `Intent`. | ||
- parameter intent: The name of the recognized intent. | ||
- parameter confidence: The confidence score of the intent, between 0 and 1. | ||
*/ | ||
init(intent: String?, confidence: Double?) { | ||
self.intent = intent | ||
self.confidence = confidence | ||
} | ||
|
||
/// Used internally to initialize an `Intent` model from JSON. | ||
public init(json: JSON) throws { | ||
intent = try? json.string("intent") | ||
confidence = try? json.double("confidence") | ||
} | ||
|
||
/// Used internally to serialize an `Intent` model to JSON. | ||
public func toJSON() -> JSON { | ||
var json = [String: JSON]() | ||
if let intent = intent { | ||
json["intent"] = .String(intent) | ||
} | ||
if let confidence = confidence { | ||
json["confidence"] = .Double(confidence) | ||
} | ||
return JSON.Dictionary(json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.