-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf0c26b
commit f7ea822
Showing
7 changed files
with
220 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { spawn } from 'child_process'; | ||
|
||
export const getRuSentiment = async ( | ||
data: Array<{ text: string; textIndex: number }>, | ||
) => { | ||
// Пустые значения не принимает библиотека и крашится | ||
const notEmptyData = data | ||
.map(({ text }) => text) | ||
.filter(text => { | ||
const withoutSpaces = text.replace(/\s/g, ''); | ||
|
||
return withoutSpaces.length > 0; | ||
}); | ||
|
||
let indexOfSentiment = 0; | ||
|
||
const pythonProcess = spawn('python3', [ | ||
'src/lib/ru_social_sentiment/sentiment_coefficient.py', | ||
JSON.stringify(notEmptyData), | ||
]); | ||
|
||
const getSentiments = () => { | ||
return new Promise<{ | ||
dataWithSentiments: { [key: string]: number }; | ||
countOfSentimentCoefficients: number; | ||
}>((resolve, reject) => { | ||
pythonProcess.stdout.on('data', sentiments => { | ||
const result = sentiments.toString(); | ||
|
||
const normalizedSentiments: { [key: string]: number } = {}; | ||
|
||
let countOfSentimentCoefficients = 0; | ||
|
||
if (result !== undefined && result.length > 0) { | ||
const parsedResult = JSON.parse(result.replace(/'/g, '"')); | ||
|
||
data.forEach(({ text, textIndex }) => { | ||
const withoutSpaces = text.replace(/\s/g, ''); | ||
let coefficient = 0; | ||
|
||
if (withoutSpaces.length > 0) { | ||
const sentiment: { [key: string]: number } = | ||
parsedResult[indexOfSentiment]; | ||
|
||
const sentimentKeys = Object.keys(sentiment); | ||
const isNegative = sentimentKeys.includes('negative'); | ||
const isPositive = sentimentKeys.includes('positive'); | ||
|
||
if (isNegative) { | ||
coefficient = -1 * sentiment.negative; | ||
} else if (isPositive) { | ||
coefficient = sentiment.positive; | ||
} else { | ||
const [_, valueSentiment] = Object.entries(sentiment)[0]; | ||
|
||
coefficient = valueSentiment; | ||
} | ||
|
||
indexOfSentiment++; | ||
|
||
countOfSentimentCoefficients = | ||
countOfSentimentCoefficients + coefficient; | ||
} | ||
|
||
normalizedSentiments[textIndex] = coefficient; | ||
}); | ||
} | ||
|
||
resolve({ | ||
dataWithSentiments: normalizedSentiments, | ||
countOfSentimentCoefficients, | ||
}); | ||
|
||
pythonProcess.kill('SIGTERM'); | ||
}); | ||
}); | ||
}; | ||
|
||
const result = await getSentiments(); | ||
|
||
return result; | ||
}; |
17 changes: 17 additions & 0 deletions
17
backend/src/lib/ru_social_sentiment/sentiment_coefficient.py
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,17 @@ | ||
from dostoevsky.tokenization import RegexTokenizer | ||
from dostoevsky.models import FastTextSocialNetworkModel | ||
|
||
import sys | ||
import json | ||
|
||
tokenizer = RegexTokenizer() | ||
|
||
model = FastTextSocialNetworkModel(tokenizer=tokenizer) | ||
|
||
texts = json.loads(sys.argv[1]) | ||
|
||
results = model.predict(texts, k=2) | ||
|
||
print(results) | ||
|
||
sys.stdout.flush() |
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
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