mdj1412
commit first
c109682
raw
history blame
8.12 kB
// jQuery
// $(document).ready(function() { });
$(function() {
console.log("Start News's Title !");
newsInit();
});
function newsInit() {
// Ticker ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ
// ํ•ด๋‹น class์˜ text ๊ฐ€์ ธ์˜ค๊ธฐ
let ticker = document.querySelector('.goticker .tickerName').textContent;
let date = document.querySelector('.titleDate').textContent;
let title = document.querySelector('.titleName').textContent;
let url = document.querySelector('.NewsURL').textContent;
ticker = ticker;
date = date.substring(6, date.length);
title = title.substring(7, title.length);
url = url.substring(5, url.length);
console.log(ticker);
console.log(date);
console.log(title);
console.log(url);
//////////////////////////////////////////////////////////////////////
// Javascript๋ฅผ ์ด์šฉํ•ด HTML์— ๋™์ ์œผ๋กœ ํƒœ๊ทธ ์ถ”๊ฐ€
// a ํƒœ๊ทธ onclick ์ ์šฉ
const goticker = document.querySelector('.goticker');
let stockURL = '/'.concat(ticker);
goticker.setAttribute('href', stockURL);
// a ํƒœ๊ทธ์— URL ์ ์šฉ
const addURL = document.querySelector('.NewsURL .input-News-URL');
addURL.setAttribute('href', url);
// ๋ชจ๋ธ์—์„œ ์งˆ๋ฌธ ์˜ˆ์‹œ Ticker์— ์•Œ๋งž๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ
const example_value = document.querySelector('#model .text-form #text-input');
example = "Why did " + ticker + "'s stock go down?";
example_value.setAttribute('value', example);
//////////////////////////////////////////////////////////////////////
// NER ๊ด€๋ จ
ents = sendAjax_async('/ner', {'ticker': ticker, 'date': date, 'title': title}, dataType="json", handle=handleReturn);
// ents = {'text': [], 'start_char': [], 'end_char': [], 'label_': [], 'news': []}
console.log(ents);
let news = ents['news'];
let numOfNER = ents['text'].length;
// ๋žœ๋”๋ง html ์š”์†Œ ์ƒ์„ฑ
news_ner = document.querySelector('.entities');
news_ner.innerHTML = '';
for (i=0; i<numOfNER-1; i++) {
start_idx = (i == 0) ? 0 : ents['end_char'][i-1];
end_idx = ents['start_char'][i];
last_idx = ents['end_char'][i];
label = ents['label_'][i];
if (label == 'ORG') { class_name = "entity_org"; }
else if (label == 'PERSON') { class_name = "entity_person"; }
else if (label == 'FAC') { class_name = "entity_fac"; }
else if (label == 'GPE') { class_name = "entity_gpe"; }
else if (label == 'PRODUCT') { class_name = "entity_product"; }
else { console.log("[ Error !!! - New NER label_ ] : ", ents['label_'][i], ents['text'][i]); class_name = "none"; }
news_ner.innerHTML = news_ner.innerHTML + news.substring(start_idx, end_idx);
news_ner.innerHTML = news_ner.innerHTML + '<mark class=' + class_name
+ ' style="margin: 0 0.25em; line-height: 1;">'
+ news.substring(end_idx, last_idx)
+ '<span class="show-label" style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem">'
+ label + '</span></mark>';
}
news_ner.innerHTML = news_ner.innerHTML + news.substring(ents['end_char'][numOfNER-1]);
//////////////////////////////////////////////////////////////////////
// ๋ชจ๋ธ ์ ์šฉ ๋‚ด์šฉ ( Submit )
var sendTitle = title; // Javascript -> Python ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ title
// title์—์„œ & ํ‘œ์‹œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ.
// Title ์—์„œ '&'๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋Š”๋ฐ ๋”ฐ๋กœ ๊ตฌ๋ณ„ํ•ด์•ผ ๋œ๋‹ค.
// andSymbolInTitle ์—์„œ ๊ฐ€์ ธ์˜จ '&' ์œ„์น˜ index๋ฅผ title๊ณผ ํ•ฉ์ณ์ค€๋‹ค.
andSymbolInTitle = [];
let idx = 0;
// title = "asdf&asdf&AS&DF&&";
// sendTitle = title;
while (true) {
idx = sendTitle.indexOf('&', idx);
if (idx == -1) { break; }
sendTitle = sendTitle.substring(0, idx) + sendTitle.substring(idx+1, sendTitle.length);
console.log(sendTitle);
andSymbolInTitle.push(idx + andSymbolInTitle.length);
}
console.log(andSymbolInTitle);
console.log("Last String", sendTitle);
// function ์•ž์— "async"๋ฅผ ๋ถ™์ด๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ ํ”„๋ผ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const translateText = async (text) => {
// ๋ชฉ์  : Flask์— input์„ ๋ณด๋‚ด์ฃผ๊ณ  output์„ ๋ฐ›์•„์˜ค๋Š” ๊ณผ์ •
console.log("Start translateText async");
// "await"๋Š” "async" ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ๋™์ž‘ํ•œ๋‹ค.
// "await" ํ‚ค์›Œ๋“œ๋ฅผ ๋งŒ๋‚˜๋ฉด Promise๊ฐ€ ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
// Promise๊ฐ€ ์ฒ˜๋ฆฌ๋˜๊ธธ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ์—” ์—”์ง„์ด ๋‹ค๋ฅธ์ผ(๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰, ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋“ฑ)์„ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, CPU ๋ฆฌ์†Œ์Šค๊ฐ€ ๋‚ญ๋น„๋˜์ง€ ์•Š๋Š”๋‹ค.
const inferResponse = await fetch(`newsQuestions?ticker=${ticker}&date=${date}&title=${sendTitle}&andSymbolInTitle=${andSymbolInTitle}&questions=${text}`); // Javascript -> Flask(python)
// console.log("inferResponse : ", inferResponse);
const inferJson = await inferResponse.json(); // Flask(python) -> Javascript
// console.log(inferJson);
return inferJson.result['answer'];
};
/* ๋ชจ๋ธ Submit button ๊ด€๋ จ ๋‚ด์šฉ */
// form ํƒœ๊ทธ์˜ class ์ด๋ฆ„
const textForm = document.querySelector('.text-form');
// addEventListener(type, listener)
// addEventListener(type, listener, options)
// addEventListener(type, listener, useCapture)
textForm.addEventListener('submit', async (event) => {
event.preventDefault();
// console.log(event);
// html -> javascript : input ๋ฐ›์•„์™€์„œ output ๋ณด๋‚ด๊ธฐ
const textInput = document.getElementById('text-input');
const textParagraph = document.querySelector('.text-output');
console.log("textInput : ", textInput, textInput.value);
try {
// sendAjax("/inference", {"input_text" : textInput.value}, handleOutput);
// "await"๋Š” "async" ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ๋™์ž‘ํ•œ๋‹ค.
// "await" ํ‚ค์›Œ๋“œ๋ฅผ ๋งŒ๋‚˜๋ฉด Promise๊ฐ€ ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
// Promise๊ฐ€ ์ฒ˜๋ฆฌ๋˜๊ธธ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ์—” ์—”์ง„์ด ๋‹ค๋ฅธ์ผ(๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰, ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋“ฑ)์„ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, CPU ๋ฆฌ์†Œ์Šค๊ฐ€ ๋‚ญ๋น„๋˜์ง€ ์•Š๋Š”๋‹ค.
const answer = await translateText(textInput.value); // Flask์— input์„ ๋ณด๋‚ด์ฃผ๊ณ  output์„ ๋ฐ›์•„์˜ค๋Š” ๊ณผ์ •
console.log("Answer : ", answer);
textParagraph.textContent = answer;
} catch (err) {
console.error(err);
}
});
}
/**
*
* @param {string} url from javascript to flask(python) with route
* @param {dictionary} data from javascript to flask(python) with data
* @param {function} handle ํฐ ์˜๋ฏธ ์—†์Œ
*/
function sendAjax(url, data, handle) {
/*
jQuery.getJSON(url, [, data], [, success])
Load JSON-encoded data from the server using a GET HTTP request.
*/
$.getJSON(url, data,
function(response) {
handle(response.result);
});
}
/**
*
* @param {string} url from javascript to flask(python) with route
* @param {dictionary} data from javascript to flask(python) with data
* @param {string} dataType The type of data that you're expecting back from the server. (ex. "json")
* @param {function} handle ํฐ ์˜๋ฏธ ์—†์Œ
* @returns from flask(python) to javascript with data
*/
function sendAjax_async(url, data, dataType, handle) {
/*
jQuery.ajax(url, [, settings])
jQuery.getJSON => Asynchronous (๋น„๋™๊ธฐ์‹)
Synchronous => ๋™๊ธฐ์‹ : ์ฝ”๋“œ ์ˆœ์„œ๋Œ€๋กœ ์ง„ํ–‰
*/
var search_var;
console.log("Internal : sendAjax async");
$.ajax(url=url, settings={data: data, dataType: dataType, async: false,
success: function(response) {
console.log("Success : ", typeof response);
search_var = handle(response.result); // handle, ํฐ ์˜๋ฏธ ์—†์Œ
}
});
return search_var
}