qna_maker 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ee955956af27b2b001676c32496cd94f4c89db0ee3bd7c5d3717c22e955209e
4
- data.tar.gz: e664c1ac7c731bbf5aa0c6fd9c81603674a489824fa1cdd475d123f6deade505
3
+ metadata.gz: 29492e8b4bfac09cf1e52269c608dba416a723e2d6738e5b0b41377a21798935
4
+ data.tar.gz: 7789f79de95d5996c3809a2b34ebb134ac9097451e3e19166d4d7bb63e425686
5
5
  SHA512:
6
- metadata.gz: 6ee21db904abbf80f6eb34edb3d9da0ffc19e4f7bb47e5ff846c137b874b7286857b63fae99913aaac388830d411e317ad54daf64aecf66c4f2de7823661b469
7
- data.tar.gz: 1c0785b5c633dc95f3c32aa4af42bd42a9f53885a2b22c0473b661c58bf8d44bfb14783dab92027c61118405c58f95b7d5ec65c5bae9b43f232e99213efff977
6
+ metadata.gz: 4d1f018e0be2926afc882e26a099b04edcce677d85588bbbea8d722365bf1ab0d25d56c4230c03c48ba72e6cce802ceb39d9c6f69046ac26d3676504c51678fc
7
+ data.tar.gz: 808612670bdbcdf03972db62f1f082d1e78f575889a4e85c55985f70e71069c0604b86f1ff90323bdd9ce4cf89fb4a939d2ff16e86cf764bb14500c4d0a00a66
data/README.md CHANGED
@@ -1,11 +1,15 @@
1
1
  # QnAMaker
2
2
 
3
- ![Gem](https://img.shields.io/gem/dv/qna_maker/stable.svg)
3
+ [![Gem](https://img.shields.io/gem/v/qna_maker.svg)](https://rubygems.org/gems/qna_maker)
4
+ [![Gem](https://img.shields.io/gem/dt/qna_maker.svg)](https://rubygems.org/gems/qna_maker)
4
5
  [![Build Status](https://travis-ci.org/Birdie0/qna_maker.svg?branch=master)](https://travis-ci.org/Birdie0/qna_maker)
5
- [![Maintainability](https://api.codeclimate.com/v1/badges/96bf3ebcfe21cd1b9359/maintainability)](https://codeclimate.com/github/Birdie0/qna_maker/maintainability)
6
+ [![Inline docs](https://inch-ci.org/github/Birdie0/qna_maker.svg?branch=master&style=shields)](https://inch-ci.org/github/Birdie0/qna_maker)
7
+ [![Maintainability](https://codeclimate.com/github/Birdie0/qna_maker/badges/gpa.svg)](https://codeclimate.com/github/Birdie0/qna_maker/maintainability)
6
8
 
7
9
  API wrapper for [QnA Maker](https://qnamaker.ai)
8
10
 
11
+ ## [QnA API docs](https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe)
12
+
9
13
  ## Installation
10
14
 
11
15
  Add this line to your application's Gemfile:
@@ -53,7 +57,7 @@ Gem is under active development! It isn't finished yet!
53
57
  * [x] **PATCH** - Train Knowledge Base
54
58
  * [x] **PATCH** - Update Alterations
55
59
  * [x] **PATCH** - Update Knowledge Base
56
- * [ ] Documentation
60
+ * [x] Documentation
57
61
  * [ ] Tests
58
62
 
59
63
  ## Development
data/bin/setup CHANGED
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env bash
2
+
2
3
  set -euo pipefail
3
4
  IFS=$'\n\t'
4
5
  set -vx
5
6
 
6
7
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -18,11 +18,30 @@ require 'qna_maker/endpoints/update_alterations'
18
18
  require 'http'
19
19
 
20
20
  module QnAMaker
21
+ #
22
+ # Client instance
23
+ #
21
24
  class Client
22
25
  BASE_URL = 'https://westus.api.cognitive.microsoft.com/QnAMaker/v2.0/knowledgebases'.freeze
23
26
 
24
- attr_reader :knowledgebase_id, :subscription_key, :data_extraction_results
27
+ # @!attribute [r] knowledgebase_id
28
+ # @return [String]
29
+ attr_reader :knowledgebase_id
30
+ # @!attribute [r] subscription_key
31
+ # @return [String]
32
+ attr_reader :subscription_key
33
+ # @!attribute [r] data_extraction_results
34
+ # @return [Array<String>]
35
+ attr_reader :data_extraction_results
25
36
 
37
+ #
38
+ # <Description>
39
+ #
40
+ # @param [String] knowledgebase_id this should be get from QnAMaker portal
41
+ # @param [String] subscription_key QnAMaker::Client provides access to
42
+ # this API. Found in your QnAMaker Service accounts (https://qnamaker.ai
43
+ # @param [Array<Hash{String => String, String => String, String => String}>] data_extraction_results ata extraction results.
44
+ #
26
45
  def initialize(knowledgebase_id, subscription_key, data_extraction_results = [])
27
46
  @knowledgebase_id = knowledgebase_id
28
47
  @subscription_key = subscription_key
@@ -1,7 +1,21 @@
1
1
  module QnAMaker
2
+ #
3
+ # Alteration class
4
+ #
2
5
  class Alteration
3
- attr_reader :word, :alterations
6
+ # @!attribute [r] word
7
+ # @return [String] word of alteration
8
+ attr_reader :word
9
+ # @!attribute [r] alterations
10
+ # @return [Array<Alteration>] list of alterations for word
11
+ attr_reader :alterations
4
12
 
13
+ #
14
+ # Creates an object for alteration.
15
+ #
16
+ # @param [String] word
17
+ # @param [Array<Alteration>] alterations list of alterations
18
+ #
5
19
  def initialize(word, alterations)
6
20
  @word = word
7
21
  @alterations = alterations
@@ -1,7 +1,17 @@
1
1
  module QnAMaker
2
+ #
3
+ # Answer class
4
+ #
2
5
  class Answer
3
6
  attr_reader :answer, :questions, :score
4
7
 
8
+ #
9
+ # Creates an object for storing data about answer.
10
+ #
11
+ # @param [String] answer text
12
+ # @param [Array<String>] questions list of questions for asked question
13
+ # @param [Float] score probability that answer suits asked question
14
+ #
5
15
  def initialize(answer, questions, score)
6
16
  @answer = answer
7
17
  @questions = questions
@@ -1,5 +1,16 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Creates a new knowledge base.
5
+ #
6
+ # @param [String] name friendly name for the knowledge base (Required)
7
+ # @param [Array<Array(String, String)>] qna_pairs list of question and answer pairs to be added to the knowledge base.
8
+ # Max 1000 Q-A pairs per request.
9
+ # @param [Array<String>] urls list of URLs to be processed and indexed in the knowledge base.
10
+ # In case of existing URL, it will be fetched again and KB will be updated with new data. Max 5 urls per request.
11
+ #
12
+ # @return [Client] client object
13
+ #
3
14
  def create_kb(name, qna_pairs = [], urls = [])
4
15
  response = @http.post(
5
16
  "#{BASE_URL}/create",
@@ -12,7 +23,7 @@ module QnAMaker
12
23
 
13
24
  case response.code
14
25
  when 201
15
- QnA.new(
26
+ QnAMaker::Client.new(
16
27
  response.parse['kbId'],
17
28
  @subscription_key,
18
29
  response.parse['dataExtractionResults']
@@ -26,6 +37,21 @@ module QnAMaker
26
37
  end
27
38
  end
28
39
 
40
+ #
41
+ # Creates a new knowledge base.
42
+ #
43
+ # @param [String] name friendly name for the knowledge base (Required)
44
+ # @param [String] subscription_key Subscription key which provides access to
45
+ # this API. Found in your QnAMaker Service accounts (https://qnamaker.ai)
46
+ # @param [Array<Array(String, String)>] qna_pairs list of question and
47
+ # answer pairs to be added to the knowledge base. Max 1000 Q-A pairs per
48
+ # request.
49
+ # @param [Array<String>] urls list of URLs to be processed and indexed in
50
+ # the knowledge base. In case of existing URL, it will be fetched again
51
+ # and KB will be updated with new data. Max 5 urls per request.
52
+ #
53
+ # @return [Client] client object
54
+ #
29
55
  def self.create_kb(name, subscription_key, qna_pairs = [], urls = [])
30
56
  response = HTTP.headers('Ocp-Apim-Subscription-Key' => subscription_key).post(
31
57
  "#{BASE_URL}/create",
@@ -38,7 +64,7 @@ module QnAMaker
38
64
 
39
65
  case response.code
40
66
  when 201
41
- QnA.new(
67
+ QnAMaker::Client.new(
42
68
  response.parse['kbId'],
43
69
  subscription_key,
44
70
  response.parse['dataExtractionResults']
@@ -1,5 +1,10 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Deletes the current knowledge base and all data associated with it.
5
+ #
6
+ # @return [nil] on success
7
+ #
3
8
  def delete_kb
4
9
  response = @http.delete(
5
10
  "#{BASE_URL}/#{knowledgebase_id}"
@@ -23,6 +28,15 @@ module QnAMaker
23
28
  end
24
29
  end
25
30
 
31
+ #
32
+ # Deletes the specified knowledge base and all data associated with it.
33
+ #
34
+ # @param [String] knowledgebase_id knowledge base identity
35
+ # @param [String] subscription_key Subscription key which provides access to
36
+ # this API. Found in your QnAMaker Service accounts (https://qnamaker.ai)
37
+ #
38
+ # @return [nil] on success
39
+ #
26
40
  def self.delete_kb(knowledgebase_id, subscription_key)
27
41
  response = HTTP.headers('Ocp-Apim-Subscription-Key' => subscription_key).delete(
28
42
  "#{BASE_URL}/#{knowledgebase_id}"
@@ -1,5 +1,11 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Downloads all word alterations (synonyms) that have been automatically
5
+ # mined or added by the user.
6
+ #
7
+ # @return [Array<Alteration>] list of alterations
8
+ #
3
9
  def download_alterations
4
10
  response = @http.get(
5
11
  "#{BASE_URL}/#{@knowledgebase_id}/downloadAlterations"
@@ -1,5 +1,10 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Downloads all the data associated with the specified knowledge base
5
+ #
6
+ # @return [String] SAS url (valid for 30 mins) to tsv file in blob storage
7
+ #
3
8
  def download_kb
4
9
  response = @http.get(
5
10
  "#{BASE_URL}/#{@knowledgebase_id}"
@@ -1,5 +1,16 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Returns the list of answers for the given question sorted in descending
5
+ # order of ranking score.
6
+ #
7
+ # @param [String] question user question to be queried against your
8
+ # knowledge base.
9
+ # @param [Integer] top number of ranked results you want in the output.
10
+ #
11
+ # @return [Array<Answer>] list of answers for the user query sorted in
12
+ # decreasing order of ranking score.
13
+ #
3
14
  def generate_answer(question, top = 1)
4
15
  response = @http.post(
5
16
  "#{BASE_URL}/#{@knowledgebase_id}/generateAnswer",
@@ -1,5 +1,10 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Publish all unpublished in the knowledgebase to the prod endpoint
5
+ #
6
+ # @return [nil] on success
7
+ #
3
8
  def publish_kb
4
9
  response = @http.put(
5
10
  "#{BASE_URL}/#{@knowledgebase_id}"
@@ -1,5 +1,16 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # The developer of the knowledge base service can use this API to submit
5
+ # user feedback for tuning question-answer matching. QnA Maker uses active
6
+ # learning to learn from the user utterances that come on a published
7
+ # Knowledge base service.
8
+ #
9
+ # @param [Array<Array(String, String, String, String)>] feedback_records
10
+ # \[user_id, user_question, kb_question, kb_answer]
11
+ #
12
+ # @return [nil] on success
13
+ #
3
14
  def train_kb(feedback_records = [])
4
15
  feedback_records = feedback_records.map do |record|
5
16
  { userId: record[0],
@@ -1,9 +1,18 @@
1
1
  module QnAMaker
2
2
  class Client
3
+ #
4
+ # Replaces word alterations (synonyms) for the KB with the give records.
5
+ #
6
+ # @param [Array(String, Array<String>)] add word alterations to be added
7
+ # @param [Array(String, Array<String>)] delete word alterations to be removed
8
+ #
9
+ # @return [nil] on success
10
+ #
3
11
  def update_alterations(add = [], delete = [])
4
12
  response = @http.patch(
5
13
  "#{BASE_URL}/#{@knowledgebase_id}/updateAlterations",
6
- json: { add: add, delete: delete }
14
+ json: { add: add.map {|i| {word: i[0], alterations: i[1]} },
15
+ delete: delete.map {|i| {word: i[0], alterations: i[1]} } }
7
16
  )
8
17
 
9
18
  case response.code
@@ -1,6 +1,15 @@
1
1
  module QnAMaker
2
2
  class Client
3
- def update_kb(add = [], delete = [], add_urls = [])
3
+ #
4
+ # Add or delete QnA Pairs and / or URLs to an existing knowledge base.
5
+ #
6
+ # @param [Array<Array(String, String)>] add \[question, answer\] data to be added to the knowledge base.
7
+ # @param [Array<Array(String, String)>] delete \[question, answer\] data to be removed to the knowledge base.
8
+ # @param [Array<String>] add_urls list of URLs to be processed and indexed in the knowledge base.
9
+ #
10
+ # @return [nil] on success
11
+ #
12
+ def update_kb(add: [], delete: [], add_urls: [])
4
13
  response = @http.patch(
5
14
  "#{BASE_URL}/#{@knowledgebase_id}",
6
15
  json: {
@@ -1,28 +1,31 @@
1
1
  module QnAMaker
2
- class BadArgumentError < StandardError
2
+ class Error < StandardError
3
3
  end
4
4
 
5
- class NotFoundError < StandardError
5
+ class BadArgumentError < Error
6
6
  end
7
7
 
8
- class UnauthorizedError < StandardError
8
+ class NotFoundError < Error
9
9
  end
10
10
 
11
- class QuotaExceededError < StandardError
11
+ class UnauthorizedError < Error
12
12
  end
13
13
 
14
- class OperationTimeOutError < StandardError
14
+ class QuotaExceededError < Error
15
15
  end
16
16
 
17
- class RateLimitExceededError < StandardError
17
+ class OperationTimeOutError < Error
18
18
  end
19
19
 
20
- class ConflictError < StandardError
20
+ class RateLimitExceededError < Error
21
21
  end
22
22
 
23
- class ForbiddenError < StandardError
23
+ class ConflictError < Error
24
24
  end
25
25
 
26
- class UnknownError < StandardError
26
+ class ForbiddenError < Error
27
+ end
28
+
29
+ class UnknownError < Error
27
30
  end
28
31
  end
@@ -2,7 +2,15 @@ require 'htmlentities'
2
2
 
3
3
  CODER = HTMLEntities.new
4
4
 
5
+ #
6
+ # Monkey-patched String method for fixing responses.
7
+ #
5
8
  class String
9
+ #
10
+ # Fixes &lt;, &amp; and other &'s characters, replaces crlf to lf newlines.
11
+ #
12
+ # @return [String] same text with correctly displayed &xxxx; characters and lf newlines.
13
+ #
6
14
  def normalize
7
15
  CODER.decode(self).gsub(/\r\n?/, "\n")
8
16
  end
@@ -1,3 +1,3 @@
1
1
  module QnAMaker
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qna_maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Birdie0
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-11 00:00:00.000000000 Z
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlentities