Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Commit 0c0c48d

Browse files
Merge pull request #60 from watson-developer-cloud/updates
Add support for IAM and update dependencies
2 parents 5d0de8f + 6681ab1 commit 0c0c48d

14 files changed

+1038
-3133
lines changed

.env.example

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=<username>
2-
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=<password>
3-
CLASSIFIER_ID=<classifier-id>
1+
# Environment variables
2+
CLASSIFIER_ID=
3+
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api
4+
# You need to provide either username and password
5+
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=
6+
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=
7+
# OR IAM API key and URL
8+
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=
9+
NATURAL_LANGUAGE_CLASSIFIER_IAM_URL=

.travis.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
dist: trusty
33
sudo: required
4-
node_js: 6
4+
node_js: 8
55
script:
66
- npm run test
77

@@ -16,12 +16,15 @@ env:
1616
- BX_SPACE=demos
1717
before_deploy: npm install -g bx-blue-green
1818
deploy:
19-
provider: script
19+
- provider: script
20+
skip_cleanup: true
2021
script:
2122
- bx-blue-green-travis
2223
on:
2324
branch: master
2425
repo: watson-developer-cloud/natural-language-classifier-nodejs
26+
- provider: script
2527
skip_cleanup: true
26-
notifications:
27-
email: false
28+
script: npx semantic-release
29+
on:
30+
node: 8

CONTRIBUTING.md

+54-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,64 @@
11
# Questions
22

3-
If you are having difficulties running the app or have a question about the service, please ask a question on [dW Answers](https://developer.ibm.com/answers/questions/ask/?topics=watson) or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson).
3+
If you are having problems using the APIs or have a question about the IBM
4+
Watson Services, please ask a question on
5+
[dW Answers](https://developer.ibm.com/answers/questions/ask/?topics=watson)
6+
or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson).
7+
8+
# Code
9+
10+
* Our style guide is based on [Google's](https://google.github.io/styleguide/jsguide.html), most of it is automaticaly enforced (and can be automatically applied with `npm run autofix`)
11+
* Commits should follow the [Angular commit message guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines). This is because our release tool uses this format for determining release versions and generating changelogs. To make this easier, we recommend using the [Commitizen CLI](https://github.com/commitizen/cz-cli) with the `cz-conventional-changelog` adapter.
412

513
# Issues
614

7-
If you encounter an issue with this sample app, you are welcome to submit a [bug report](https://github.com/watson-developer-cloud/natural-language-classifier-nodejs/issues). Before that, please search for similar issues. It's possible somebody has encountered this issue already.
15+
If you encounter an issue with the Node.js library, you are welcome to submit
16+
a [bug report](https://github.com/watson-developer-cloud/natural-language-classifier-nodejs/issues).
17+
Before that, please search for similar issues. It's possible somebody has
18+
already encountered this issue.
819

920
# Pull Requests
1021

11-
If you want to contribute to the repository, here's a quick guide:
22+
If you want to contribute to the repository, follow these steps:
1223

1324
1. Fork the repo.
14-
1. develop your code changes: `npm install -d`
15-
1. Test your changes: `npm test`
16-
1. Commit your changes
17-
1. Push to your fork and submit a pull request
25+
2. Develop and test your code changes: `npm install -d && npm test`.
26+
3. Travis-CI will run the tests for all services once your changes are merged.
27+
4. Add a test for your changes. Only refactoring and documentation changes require no new tests.
28+
5. Make the test pass.
29+
6. Commit your changes.
30+
7. Push to your fork and submit a pull request.
31+
32+
# Developer's Certificate of Origin 1.1
33+
34+
By making a contribution to this project, I certify that:
35+
36+
(a) The contribution was created in whole or in part by me and I
37+
have the right to submit it under the open source license
38+
indicated in the file; or
39+
40+
(b) The contribution is based upon previous work that, to the best
41+
of my knowledge, is covered under an appropriate open source
42+
license and I have the right under that license to submit that
43+
work with modifications, whether created in whole or in part
44+
by me, under the same open source license (unless I am
45+
permitted to submit under a different license), as indicated
46+
in the file; or
47+
48+
(c) The contribution was provided directly to me by some other
49+
person who certified (a), (b) or (c) and I have not modified
50+
it.
51+
52+
(d) I understand and agree that this project and the contribution
53+
are public and that a record of the contribution (including all
54+
personal information I submit with it, including my sign-off) is
55+
maintained indefinitely and may be redistributed consistent with
56+
this project or the open source license(s) involved.
57+
58+
## Tests
59+
60+
Ideally, we'd like to see both unit and innervation tests on each method.
61+
(Unit tests do not actually connect to the Watson service, integration tests do.)
62+
63+
Out of the box, `npm test` runs linting and unit tests, but skips the integration tests,
64+
because they require credentials.

README.md

+88-78
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,123 @@
1-
# Natural Language Classifier Demo [![Build Status](https://travis-ci.org/watson-developer-cloud/natural-language-classifier-nodejs.svg?branch=master)](http://travis-ci.org/watson-developer-cloud/natural-language-classifier-nodejs)
2-
3-
The IBM Watson&trade; Natural Language Classifier service applies deep learning techniques to make predictions about the best predefined classes for short sentences or phrases. The classes can trigger a corresponding action in an application, such as directing a request to a location or person, or answering a question. After training, the service returns information for texts that it hasn't seen before. The response includes the name of the top classes and confidence values.
1+
<h1 align="center" style="border-bottom: none;">🚀 Natural Language Classifier Sample Application</h1>
2+
<h3 align="center">This Node.js app demonstrates some of the Natural Language Classifier service features.</h3>
3+
<p align="center">
4+
<a href="http://travis-ci.org/watson-developer-cloud/natural-language-classifier">
5+
<img alt="Travis" src="https://travis-ci.org/watson-developer-cloud/natural-language-classifier.svg?branch=master">
6+
</a>
7+
<a href="#badge">
8+
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
9+
</a>
10+
</p>
11+
</p>
12+
13+
The IBM Watson&trade; Natural Language Classifier service applies deep learning techniques to make predictions about the best predefined classes for short sentences or phrases. The classes can trigger a corresponding action in an application, such as directing a request to a location or person, or answering a question. After training, the service returns information for texts that it hasn't seen before. The response includes the name of the top classes and confidence values.
414

515
![demo](public/demo.gif)
616

7-
## Getting started
8-
9-
1. You need a Bluemix account. If you don't have one, [sign up][sign_up]. Experimental Watson Services are free to use.
10-
11-
2. Download and install the [Cloud-foundry CLI][cloud_foundry] tool if you haven't already.
12-
13-
3. Edit the `manifest.yml` file and change `<application-name>` to something unique. The name you use determines the URL of your application. For example, `<application-name>.mybluemix.net`.
14-
15-
```yaml
16-
applications:
17-
- services:
18-
- my-service-instance
19-
name: <application-name>
20-
command: npm start
21-
path: .
22-
memory: 512M
23-
```
17+
You can view a [demo](https://natural-language-classifier-demo.ng.bluemix.net/) of this app.
2418

25-
4. Connect to Bluemix with the command line tool.
2619

27-
```sh
28-
cf api https://api.ng.bluemix.net
29-
cf login
30-
```
20+
## Prerequisites
3121

32-
5. Create and retrieve service keys to access the [Natural Language Classifier][nlc_docs] service:
22+
1. Sign up for an [IBM Cloud account](https://console.bluemix.net/registration/).
23+
1. Download the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview).
24+
1. Create an instance of the Natural Language Classifier service and get your credentials:
25+
- Go to the [Natural Language Classifier](https://console.bluemix.net/catalog/services/natural-language-classifier) page in the IBM Cloud Catalog.
26+
- Log in to your IBM Cloud account.
27+
- Click **Create**.
28+
- Click **Show** to view the service credentials.
29+
- Copy the `apikey` value, or copy the `username` and `password` values if your service instance doesn't provide an `apikey`.
30+
- Copy the `url` value.
3331

34-
```none
35-
cf create-service natural_language_classifier standard my-nlc-service
36-
cf create-service-key my-nlc-service myKey
37-
cf service-key my-nlc-service myKey
38-
```
32+
## Configuring the application
3933

40-
6. The Natural Language Classifier service must be trained before you can successfully use this application. The training data is provided in the file `training/weather_data_train.csv`.
41-
Train a classifier by using the following command:
34+
1. The Natural Language Classifier service must be trained before you can successfully use this application. The training data is provided in the file `training/weather_data_train.csv`.
35+
If you have `username` and `password` credentials, train a classifier by using the following command:
4236

4337
```none
4438
curl -i -u "<username>":"<password>" \
4539
-F training_data=@training/weather_data_train.csv \
4640
-F training_metadata="{\"language\":\"en\",\"name\":\"TutorialClassifier\"}" \
47-
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers"
41+
"<url>/v1/classifiers"
4842
```
49-
Copy `<username>` and `<password>` from step 5
43+
Make sure to replace `<username>`, `<password>` and `<url>`.
44+
If you have `apikey` credentials, use the word "apikey" as your username and your `apikey` as the password.
45+
After running the command, copy the value for `classifier_id`.
5046

51-
7. Create a `.env` file in the root directory by copying the sample `.env.example` file using the following command:
47+
2. In the application folder, copy the *.env.example* file and create a file called *.env*
5248

53-
```none
54-
cp .env.example .env
55-
```
56-
You will update the `.env` with the information you retrieved in steps 5 and 6
49+
```
50+
cp .env.example .env
51+
```
5752
58-
The `.env` file will look something like the following:
53+
7. Open the *.env* file and add the service credentials that you obtained in the previous step.
5954
60-
```none
61-
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=
62-
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=
63-
CLASSIFIER_ID=
64-
```
55+
Example *.env* file that configures the `apikey` and `url` for a Natural Language Classifier service instance hosted in the US East region:
6556
66-
8. Install the dependencies you application need:
57+
```
58+
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=X4rbi8vwZmKpXfowaS3GAsA7vdy17Qh7km5D6EzKLHL2
59+
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api
60+
```
6761
68-
```none
69-
npm install
70-
```
62+
- If your service instance uses `username` and `password` credentials, add the `NATURAL_LANGUAGE_CLASSIFIER_USERNAME` and `NATURAL_LANGUAGE_CLASSIFIER_PASSWORD` variables to the *.env* file.
7163
72-
9. Start the application locally:
64+
Example *.env* file that configures the `username`, `password`, and `url` for a Natural Language Classifier service instance hosted in the Sydney region:
7365
74-
```none
75-
npm start
76-
```
66+
```
67+
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=522be-7b41-ab44-dec3-g1eab2ha73c6
68+
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=A4Z5BdGENrwu8
69+
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway-syd.watsonplatform.net/natural-language-classifier/api
70+
```
7771
78-
10. Point your browser to [http://localhost:3000](http://localhost:3000).
72+
8. Add the `CLASSIFIER_ID` to the previous properties
7973
80-
11. **Optional:** Push the application to Bluemix:
74+
```
75+
CLASSIFIER_ID=522be-7b41-ab44-dec3-g1eab2ha73c6
76+
```
8177
82-
```none
83-
cf push
84-
```
78+
## Running locally
8579
86-
After completing the steps above, you are ready to test your application. Start a browser and enter the URL of your application.
80+
1. Install the dependencies
8781
88-
<your application name>.mybluemix.net
82+
```
83+
npm install
84+
```
8985
86+
1. Run the application
9087
91-
For more details about developing applications that use Watson Developer Cloud services in Bluemix, see [Getting started with Watson Developer Cloud and Bluemix][getting_started].
88+
```
89+
npm start
90+
```
9291
92+
1. View the application in a browser at `localhost:3000`
9393
94-
## Troubleshooting
94+
## Deploying to IBM Cloud as a Cloud Foundry Application
9595
96-
* The main source of troubleshooting and recovery information is the Bluemix log. To view the log, run the following command:
96+
1. Login to IBM Cloud with the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview)
9797
98-
```sh
99-
cf logs <application-name> --recent
100-
```
98+
```
99+
ibmcloud login
100+
```
101+
102+
1. Target a Cloud Foundry organization and space.
101103
102-
* For more details about the service, see the [documentation][nlc_docs] for the Natural Language Classifier.
104+
```
105+
ibmcloud target --cf
106+
```
103107
108+
1. Edit the *manifest.yml* file. Change the **name** field to something unique.
109+
For example, `- name: my-app-name`.
110+
1. Deploy the application
104111
105-
----
112+
```
113+
ibmcloud app push
114+
```
106115
107-
### Directory structure
116+
1. View the application online at the app URL.
117+
For example: https://my-app-name.mybluemix.net
118+
119+
120+
## Directory structure
108121
109122
```none
110123
.
@@ -125,17 +138,14 @@ For more details about developing applications that use Watson Developer Cloud s
125138

126139
## License
127140

128-
This sample code is licensed under Apache 2.0.
141+
This sample code is licensed under Apache 2.0.
142+
Full license text is available in [LICENSE](LICENSE).
129143

130144
## Contributing
131145

132-
See [CONTRIBUTING](.github/CONTRIBUTING.md).
146+
See [CONTRIBUTING](CONTRIBUTING.md).
133147

134148
## Open Source @ IBM
135-
Find more open source projects on the [IBM Github Page](http://ibm.github.io/)
136-
137-
[cloud_foundry]: https://github.com/cloudfoundry/cli
138-
[getting_started]: https://console.bluemix.net/docs/services/watson/index.html#about
139-
[nlc_docs]: https://console.bluemix.net/docs/services/natural-language-classifier/getting-started.html
140-
[sign_up]: https://console.bluemix.net/registration/?target=%2Fcatalog%2Fservices%2Fnatural-language-classifier%2F
141149

150+
Find more open source projects on the
151+
[IBM Github Page](http://ibm.github.io/).

app.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@ const NaturalLanguageClassifierV1 = require('watson-developer-cloud/natural-lang
2222
// Bootstrap application settings
2323
require('./config/express')(app);
2424

25-
const classifier = new NaturalLanguageClassifierV1({
26-
// If unspecified here, the NATURAL_LANGUAGE_CLASSIFIER_USERNAME and
27-
// NATURAL_LANGUAGE_CLASSIFIER_PASSWORD env properties will be checked
28-
// After that, the SDK will fall back to the bluemix-provided VCAP_SERVICES environment property
29-
// username: '<username>',
30-
// password: '<password>',
31-
});
25+
// Create the service wrapper
3226

33-
app.get('/', (req, res) => {
34-
res.render('index', {
35-
bluemixAnalytics: !!process.env.BLUEMIX_ANALYTICS,
27+
let classifier;
28+
29+
if (process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY && process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY !== '') {
30+
classifier = new NaturalLanguageClassifierV1({
31+
url: process.env.NATURAL_LANGUAGE_CLASSIFIER_URL || '<service-url>',
32+
iam_apikey: process.env.NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY || '<iam_apikey>',
33+
iam_url: process.env.ASSISTANT_IAM_URL || 'https://iam.bluemix.net/identity/token',
3634
});
35+
} else {
36+
classifier = new NaturalLanguageClassifierV1({
37+
url: process.env.NATURAL_LANGUAGE_CLASSIFIER_URL || '<service-url>',
38+
username: process.env.NATURAL_LANGUAGE_CLASSIFIER_USERNAME || '<username>',
39+
password: process.env.NATURAL_LANGUAGE_CLASSIFIER_PASSWORD || '<password>',
40+
});
41+
}
42+
43+
app.get('/', (req, res) => {
44+
res.render('index');
3745
});
3846

3947
/**

0 commit comments

Comments
 (0)