File size: 8,124 Bytes
c109682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 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
}