Skip to content

Commit

Permalink
working build
Browse files Browse the repository at this point in the history
  • Loading branch information
leonnoel committed Jul 1, 2021
0 parents commit 8a6b798
Show file tree
Hide file tree
Showing 9 changed files with 3,115 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Install

`npm install`

---

# Things to add

- Create a config folder with a `.env` file and add the following as `key = value`
- PORT = 2121 (can be any port example: 3000)
- MS_KEY = `your Microsoft Face Endpoint`
- MS_REGION = `your Microsoft Face Key`

---

# Run

`npm start`
62 changes: 62 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const express = require("express");
const app = express();

require("dotenv").config({ path: "./config/.env" });

// pull in the required packages.
const {
TextAnalyticsClient,
AzureKeyCredential,
} = require("@azure/ai-text-analytics");

const endpoint = process.env["ENDPOINT"] || "<cognitive services endpoint>";
const apiKey = process.env["TEXT_ANALYTICS_API_KEY"] || "<api key>";

//Server Setup
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

//Routes
app.get("/", (req, res) => {
res.render("index.ejs");
});

app.post("/", async (req, res) => {
try {
const documents = [String(req.body.sendText)];

console.log("=== Analyze Sentiment Sample ===");

const client = new TextAnalyticsClient(
endpoint,
new AzureKeyCredential(apiKey)
);

const results = await client.analyzeSentiment(documents);

for (let i = 0; i < results.length; i++) {
const result = results[i];
console.log(`- Document ${result.id}`);
if (!result.error) {
console.log(`\tDocument text: ${documents[i]}`);
console.log(`\tOverall Sentiment: ${result.sentiment}`);
console.log("\tSentiment confidence scores: ", result.confidenceScores);
console.log("\tSentences");
for (const { sentiment, confidenceScores, text } of result.sentences) {
console.log(`\t- Sentence text: ${text}`);
console.log(`\t Sentence sentiment: ${sentiment}`);
console.log("\t Confidence scores:", confidenceScores);
}
} else {
console.error(` Error: ${result.error}`);
}
}
res.render("result.ejs", { result: results });
} catch (err) {
console.log(err);
}
});

app.listen(process.env.PORT || 8000);
Loading

0 comments on commit 8a6b798

Please sign in to comment.