Spaces:
Runtime error
Runtime error
// 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 | |
} | |