Skip to content
This repository has been archived by the owner on Nov 28, 2018. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Feb 7, 2017
0 parents commit 5952c0c
Show file tree
Hide file tree
Showing 14 changed files with 27,763 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.idea
node_modules
npm-debug.log
Text2Emotion.log*
26 changes: 26 additions & 0 deletions README.md
@@ -0,0 +1,26 @@
# Text2Emotion

> 分析一句话的情绪值
## 介绍

Demo:https://api.prprpr.me/emotion?text=我今天很开心

以讯飞分词接口和大连理工的情感词汇本体库为基础,分析一句话的情绪值。

## 使用

https://api.prprpr.me/emotion?text={一句话}

## 搭建

需要环境:Node.js MongoDB

`emos.json`导入 MongoDB:
```
mongoimport -d emotion -c emos --file emos.json
```

## LICENSE

MIT © [DIYgod](http://github.com/DIYgod)
3 changes: 3 additions & 0 deletions config.js
@@ -0,0 +1,3 @@
module.exports = {
xfkey: 'o1W418m6b4I674f3W064UCaxpwgkFdRYYJDn7LeM'
};
27,466 changes: 27,466 additions & 0 deletions emos.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions index.js
@@ -0,0 +1,10 @@
var express = require('express');
var logger = require('./tools/logger');
require('./tools/mongodb');

logger.info(`🍻 Text2Emotion start! Cheers!`);

var app = express();
app.all('*', require('./routes/all'));
app.get('/', require('./routes/get'));
app.listen(1210);
Empty file added logs/.gitkeep
Empty file.
60 changes: 60 additions & 0 deletions logs/Weibo2RSS.log
@@ -0,0 +1,60 @@
[2016-11-02 02:11:52.712] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-02 02:11:54.037] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 02:11:54.224] [INFO] Weibo2RSS - Set redis: Weibo2RSS2025479687
[2016-11-02 02:11:54.230] [INFO] console - Reply: OK
[2016-11-02 02:12:04.398] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687.com form origin, IP: ::ffff:127.0.0.1
[2016-11-02 02:12:04.531] [INFO] Weibo2RSS - Set redis: Weibo2RSS2025479687.com
[2016-11-02 02:12:04.533] [INFO] console - Reply: OK
[2016-11-02 02:12:05.577] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687.com form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:13:28.544] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:13:29.635] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:16:17.515] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687.com form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:22:10.306] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 02:22:10.551] [INFO] Weibo2RSS - Set redis: Weibo2RSS3306934123
[2016-11-02 02:22:10.581] [INFO] console - Reply: OK
[2016-11-02 02:23:04.844] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-02 02:23:06.994] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:23:09.012] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:23:10.324] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:24:01.702] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:24:03.561] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:24:04.381] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:24:05.069] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:26:39.344] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:27:56.116] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 02:27:56.311] [INFO] Weibo2RSS - Set redis: Weibo2RSS3306934123
[2016-11-02 02:27:56.313] [INFO] console - Reply: OK
[2016-11-02 02:32:31.321] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-02 02:32:37.076] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:32:38.267] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:32:39.210] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:32:39.998] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 02:36:29.393] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-02 02:36:30.437] [INFO] Weibo2RSS - Weibo2RSS uid 3306934123 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 02:36:30.616] [INFO] Weibo2RSS - Set redis: Weibo2RSS3306934123
[2016-11-02 02:36:30.621] [INFO] console - Reply: OK
[2016-11-02 02:50:29.872] [INFO] Weibo2RSS - Weibo2RSS uid 3225569200 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 02:50:30.098] [INFO] Weibo2RSS - Set redis: Weibo2RSS3225569200
[2016-11-02 02:50:30.117] [INFO] console - Reply: OK
[2016-11-02 12:56:59.695] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-02 12:57:24.422] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 12:57:25.309] [INFO] Weibo2RSS - Set redis: Weibo2RSS2025479687
[2016-11-02 12:57:25.317] [INFO] console - Reply: OK
[2016-11-02 12:57:39.525] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 12:57:39.669] [INFO] Weibo2RSS - Set redis: Weibo2RSS2025479687
[2016-11-02 12:57:39.670] [INFO] console - Reply: OK
[2016-11-02 12:57:44.074] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form origin, IP: ::ffff:127.0.0.1
[2016-11-02 12:57:44.198] [INFO] Weibo2RSS - Set redis: Weibo2RSS2025479687
[2016-11-02 12:57:44.199] [INFO] console - Reply: OK
[2016-11-02 12:57:48.910] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form redis, IP: ::ffff:127.0.0.1
[2016-11-02 12:57:51.140] [INFO] Weibo2RSS - Weibo2RSS uid 2025479687 form redis, IP: ::ffff:127.0.0.1
[2016-11-04 17:02:52.750] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-04 17:03:01.004] [INFO] Weibo2RSS - Weibo2RSS uid 3225569200 form origin, IP: ::ffff:127.0.0.1
[2016-11-04 17:03:01.450] [ERROR] Weibo2RSS - Bilibilib Error: getting cid [ReferenceError: redis is not defined]
ReferenceError: redis is not defined
at /Users/DIYgod/Code/Project/Weibo2RSS/routes/get.js:89:13
at process._tickCallback (node.js:401:9)
[2016-11-04 17:03:40.199] [INFO] Weibo2RSS - 🍻 Weibo2RSS start! Cheers!
[2016-11-04 17:03:41.769] [INFO] Weibo2RSS - Weibo2RSS uid 3225569200 form origin, IP: ::ffff:127.0.0.1
[2016-11-04 17:03:45.509] [INFO] Weibo2RSS - Weibo2RSS uid 3225569200 form origin, IP: ::ffff:127.0.0.1
[2016-11-04 17:03:48.466] [INFO] Weibo2RSS - Weibo2RSS uid 3225569200 form origin, IP: ::ffff:127.0.0.1
11 changes: 11 additions & 0 deletions models/emotion.js
@@ -0,0 +1,11 @@
var mongoose = require('../tools/mongodb');
var emotionSchema = new mongoose.Schema({
word: String,
type: String,
emotion: String,
strength: Number,
polarity: Number
});
var emotion = mongoose.model('emo', emotionSchema);

module.exports = emotion;
28 changes: 28 additions & 0 deletions package.json
@@ -0,0 +1,28 @@
{
"name": "Text2Emotion",
"version": "0.0.1",
"description": "分析一句话的情绪值",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/DIYgod/Text2Emotion.git"
},
"keywords": [
"Emotion"
],
"author": "DIYgod",
"license": "MIT",
"bugs": {
"url": "https://github.com/DIYgod/Text2Emotion/issues"
},
"homepage": "https://github.com/DIYgod/Text2Emotion#readme",
"devDependencies": {},
"dependencies": {
"express": "^4.14.0",
"log4js": "^0.6.38",
"mongoose": "^4.1.9",
"node-fetch": "^1.6.3",
"redis": "^2.6.3",
"request": "^2.79.0"
}
}
12 changes: 12 additions & 0 deletions routes/all.js
@@ -0,0 +1,12 @@
module.exports = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

if (req.method == 'OPTIONS') {
res.send(200);
}
else {
next();
}
};
85 changes: 85 additions & 0 deletions routes/get.js
@@ -0,0 +1,85 @@
var url = require('url');
var fetch = require('node-fetch');
var logger = require('../tools/logger');
var danmaku = require('../models/emotion');
var redis = require('../tools/redis');
var xfkey = require('../config').xfkey;

module.exports = function (req, res) {
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;

var query = url.parse(req.url,true).query;
var text = query.text;

redis.client.get(`Text2Emotion${text}`, function(err, reply) {
if (reply) {
logger.info(`Text2Emotion text ${text} form redis, IP: ${ip}`);
res.send(reply);
}
else {
logger.info(`Text2Emotion id ${text} form origin, IP: ${ip}`);

fetch(`http://ltpapi.voicecloud.cn/analysis/?api_key=${xfkey}&text=${encodeURIComponent(text)}&pattern=pos&format=json`).then(
response => response.json()
).then((data) => {
var words = [];
for (let i = 0; i < data[0].length; i++) {
words = words.concat(data[0][i]);
}
var pureWords = [];
var emotionNum = 0;
var last = 0;
for (let i = 0; i < words.length; i++) {
pureWords.push(words[i].cont);
if (words[i].pos !== 'wp' && !/[a-zA-Z0-9]/.test(words[i].cont)) {
last = i;

danmaku.find({word: words[i].cont}, function (err, data) {
if (err) {
logger.error(err);
}
if (data.length) {
if (data[0].polarity === 1) {
emotionNum += data[0].strength;
}
else if (data[0].polarity === 2) {
emotionNum -= data[0].strength;
}
if (i === last) {
var emotion = {
code: 1,
emotion: emotionNum,
words: pureWords
};
var sendEmotion = JSON.stringify(emotion);
res.send(sendEmotion);

redis.set(`Text2Emotion${text}`, sendEmotion);
}
}
else {
if (i === last) {
var emotion = {
code: 1,
emotion: emotionNum,
words: pureWords
};
var sendEmotion = JSON.stringify(emotion);
res.send(sendEmotion);

redis.set(`Text2Emotion${text}`, sendEmotion);
}
}
})
}
}
}
).catch(
e => logger.error("Text2Emotion Error: get xfapi", e)
);
}
});
};
20 changes: 20 additions & 0 deletions tools/logger.js
@@ -0,0 +1,20 @@
var log4js = require('log4js');
log4js.configure({
appenders: [
{
type: "file",
filename: 'logs/Text2Emotion.log',
maxLogSize: 20480,
backups: 3,
category: [ 'Text2Emotion','console' ]
},
{
type: "console"
}
],
replaceConsole: true
});
var logger = log4js.getLogger('Text2Emotion');
logger.setLevel('INFO');

module.exports = logger;
11 changes: 11 additions & 0 deletions tools/mongodb.js
@@ -0,0 +1,11 @@
var mongoose = require('mongoose');
var mongodbUrl;
if (process.env.MONGODB_USERNAME && process.env.MONGODB_PASSWORD && process.env.MONGODB_PORT_27017_TCP_ADDR && process.env.MONGODB_PORT_27017_TCP_PORT && process.env.MONGODB_INSTANCE_NAME) {
mongodbUrl = 'mongodb://' + process.env.MONGODB_USERNAME + ':' + process.env.MONGODB_PASSWORD + '@' + process.env.MONGODB_PORT_27017_TCP_ADDR + ':' + process.env.MONGODB_PORT_27017_TCP_PORT + '/' + process.env.MONGODB_INSTANCE_NAME;
}
else {
mongodbUrl = 'mongodb://127.0.0.1:27017/emotion';
}
mongoose.connect(mongodbUrl);

module.exports = mongoose;
27 changes: 27 additions & 0 deletions tools/redis.js
@@ -0,0 +1,27 @@
var logger = require('./logger');
var redis = require("redis");
var client;
if (process.env.REDIS_PORT_6379_TCP_ADDR && process.env.REDIS_PORT_6379_TCP_PORT && process.env.REDIS_PASSWORD) {
client = redis.createClient({
host: process.env.REDIS_PORT_6379_TCP_ADDR,
port: process.env.REDIS_PORT_6379_TCP_PORT,
password: process.env.REDIS_PASSWORD
});
}
else {
client = redis.createClient();
}


client.on("error", function (err) {
logger.error('Redis Error ' + err);
});

module.exports = {
set: function (key, value) {
client.set(key, value, redis.print);
// client.expire(key, 300);
logger.info('Set redis: ' + key);
},
client: client
};

0 comments on commit 5952c0c

Please sign in to comment.