From d7fc4c3835272719a3a886d179b0cc50ac356156 Mon Sep 17 00:00:00 2001 From: Yu-Xiang Hong Date: Wed, 10 May 2023 22:54:10 +0800 Subject: [PATCH] Do some line adjustments --- legal_judgment_prediction/tools/accuracy.py | 2 +- .../tools/dataset/JsonFromFiles.py | 2 +- legal_judgment_prediction/tools/eval.py | 2 +- legal_judgment_prediction/tools/fact_preprocess.py | 2 +- legal_judgment_prediction/tools/formatter/Basic.py | 3 +-- legal_judgment_prediction/tools/formatter/Bert.py | 2 +- legal_judgment_prediction/tools/model/Bert.py | 2 +- .../tools/model/BertEncoder.py | 2 +- legal_judgment_prediction/tools/model/Predictor.py | 2 +- legal_judgment_prediction/tools/output/functions.py | 2 +- .../tools/output/initialize.py | 2 +- line_bot/Function.py | 4 +--- line_bot/message.py | 13 +++++++++---- line_bot/new.py | 3 ++- line_bot/rich_menu.py | 7 ++++--- line_bot/scraper/law_scraper.py | 7 +++++-- line_bot/scraper/today_news_scraper.py | 7 ++++--- line_bot/scraper/yahoo_scraping.py | 13 +++++++++---- main.py | 2 +- 19 files changed, 46 insertions(+), 33 deletions(-) diff --git a/legal_judgment_prediction/tools/accuracy.py b/legal_judgment_prediction/tools/accuracy.py index d901ac8..98a8220 100644 --- a/legal_judgment_prediction/tools/accuracy.py +++ b/legal_judgment_prediction/tools/accuracy.py @@ -140,4 +140,4 @@ def multi_label_accuracy(outputs, label, config, result=None): result[i]['FP'] += int(((1 - labels1) * outputs1).sum()) result[i]['TN'] += int(((1 - labels1) * (1 - outputs1)).sum()) - return result + return result \ No newline at end of file diff --git a/legal_judgment_prediction/tools/dataset/JsonFromFiles.py b/legal_judgment_prediction/tools/dataset/JsonFromFiles.py index 740a2b2..58b9ac8 100644 --- a/legal_judgment_prediction/tools/dataset/JsonFromFiles.py +++ b/legal_judgment_prediction/tools/dataset/JsonFromFiles.py @@ -39,4 +39,4 @@ def __len__(self): if self.reduce: return int(self.reduce_ratio * len(self.data)) - return len(self.data) + return len(self.data) \ No newline at end of file diff --git a/legal_judgment_prediction/tools/eval.py b/legal_judgment_prediction/tools/eval.py index 8e71ff2..7fdbc81 100644 --- a/legal_judgment_prediction/tools/eval.py +++ b/legal_judgment_prediction/tools/eval.py @@ -64,4 +64,4 @@ def eval_one(model, dataset, epoch, writer, config, gpu_list, output_function, t if task == 'valid': writer.add_scalar(config.get('output', 'model_name') + '_eval_epoch', float(total_loss) / (step + 1), epoch) - model.train() + model.train() \ No newline at end of file diff --git a/legal_judgment_prediction/tools/fact_preprocess.py b/legal_judgment_prediction/tools/fact_preprocess.py index c41629a..451370b 100644 --- a/legal_judgment_prediction/tools/fact_preprocess.py +++ b/legal_judgment_prediction/tools/fact_preprocess.py @@ -183,4 +183,4 @@ def check_odd_chars(string): process('test') logger.info('test data processing is complete.') - logger.info('Task completed.') + logger.info('Task completed.') \ No newline at end of file diff --git a/legal_judgment_prediction/tools/formatter/Basic.py b/legal_judgment_prediction/tools/formatter/Basic.py index 100f028..6234af7 100644 --- a/legal_judgment_prediction/tools/formatter/Basic.py +++ b/legal_judgment_prediction/tools/formatter/Basic.py @@ -4,5 +4,4 @@ def __init__(self, config, mode, *args, **params): self.mode = mode def process(self, data, config, mode, *args, **params): - return data - + return data \ No newline at end of file diff --git a/legal_judgment_prediction/tools/formatter/Bert.py b/legal_judgment_prediction/tools/formatter/Bert.py index 53f75a5..0e021c2 100644 --- a/legal_judgment_prediction/tools/formatter/Bert.py +++ b/legal_judgment_prediction/tools/formatter/Bert.py @@ -129,4 +129,4 @@ def process(self, data, config, mode, *args, **kwargs): article_source = torch.LongTensor(article_source) article = torch.LongTensor(article) - return {'text': input, 'accuse': charge, 'article_source': article_source, 'article': article} + return {'text': input, 'accuse': charge, 'article_source': article_source, 'article': article} \ No newline at end of file diff --git a/legal_judgment_prediction/tools/model/Bert.py b/legal_judgment_prediction/tools/model/Bert.py index 61853ce..fa7f640 100644 --- a/legal_judgment_prediction/tools/model/Bert.py +++ b/legal_judgment_prediction/tools/model/Bert.py @@ -53,4 +53,4 @@ def forward(self, data, config, gpu_list, acc_result, mode): for name in ['accuse', 'article_source', 'article']: acc_result[name] = self.accuracy_function[name](result[name], data[name], config, acc_result[name]) - return {'loss': loss, 'acc_result': acc_result, 'output': result} + return {'loss': loss, 'acc_result': acc_result, 'output': result} \ No newline at end of file diff --git a/legal_judgment_prediction/tools/model/BertEncoder.py b/legal_judgment_prediction/tools/model/BertEncoder.py index 2be8a85..427d960 100644 --- a/legal_judgment_prediction/tools/model/BertEncoder.py +++ b/legal_judgment_prediction/tools/model/BertEncoder.py @@ -12,4 +12,4 @@ def __init__(self, config, gpu_list, *args, **params): def forward(self, x): _, y = self.bert(x, output_all_encoded_layers=False) - return y + return y \ No newline at end of file diff --git a/legal_judgment_prediction/tools/model/Predictor.py b/legal_judgment_prediction/tools/model/Predictor.py index 60577f8..a373b42 100644 --- a/legal_judgment_prediction/tools/model/Predictor.py +++ b/legal_judgment_prediction/tools/model/Predictor.py @@ -25,4 +25,4 @@ def forward(self, h): article_source = article_source.view(batch, -1, 2) article = article.view(batch, -1, 2) - return {'accuse': charge, 'article': article, 'article_source': article_source} + return {'accuse': charge, 'article': article, 'article_source': article_source} \ No newline at end of file diff --git a/legal_judgment_prediction/tools/output/functions.py b/legal_judgment_prediction/tools/output/functions.py index a1d35c6..6ce892e 100644 --- a/legal_judgment_prediction/tools/output/functions.py +++ b/legal_judgment_prediction/tools/output/functions.py @@ -32,4 +32,4 @@ def ljp_output_function(data, config, *args, **params): for name_ in ['mip', 'mir', 'mif', 'map', 'mar', 'maf']: result[name].append(temp[name][name_]) - return json.dumps(result, sort_keys=True) + return json.dumps(result, sort_keys=True) \ No newline at end of file diff --git a/legal_judgment_prediction/tools/output/initialize.py b/legal_judgment_prediction/tools/output/initialize.py index a459310..f050926 100644 --- a/legal_judgment_prediction/tools/output/initialize.py +++ b/legal_judgment_prediction/tools/output/initialize.py @@ -20,4 +20,4 @@ def init_output_function(config, *args, **params): else: information = 'There is no model called %s.' % function_name logger.error(information) - raise AttributeError + raise AttributeError \ No newline at end of file diff --git a/line_bot/Function.py b/line_bot/Function.py index cb3a75b..4e6550b 100644 --- a/line_bot/Function.py +++ b/line_bot/Function.py @@ -1,13 +1,10 @@ #這個檔案的作用是:建立功能列表 -#===============這些是LINE提供的功能套組,先用import叫出來============= from linebot import (LineBotApi, WebhookHandler) from linebot.exceptions import (InvalidSignatureError) from linebot.models import * -#===============LINEAPI============================================= #以下是本檔案的內容本文 - #1.建立旋轉木馬訊息,名為function_list(未來可以叫出此函數來使用) #function_list的括號內是設定此函數呼叫時需要給函數的參數有哪些 @@ -169,4 +166,5 @@ def function_list(): ] ) ) + return message \ No newline at end of file diff --git a/line_bot/message.py b/line_bot/message.py index 407e97d..261017a 100644 --- a/line_bot/message.py +++ b/line_bot/message.py @@ -1,8 +1,8 @@ -#這些是LINE官方開放的套件組合透過import來套用這個檔案上 from linebot import (LineBotApi, WebhookHandler) from linebot.exceptions import (InvalidSignatureError) from linebot.models import * + #ImagemapSendMessage(組圖訊息) def imagemap_message(): message = ImagemapSendMessage( @@ -47,6 +47,7 @@ def imagemap_message(): ) ] ) + return message #TemplateSendMessage - ButtonsTemplate (按鈕介面訊息) @@ -77,8 +78,10 @@ def buttons_message(): ] ) ) + return message + #TemplateSendMessage - ConfirmTemplate(確認介面訊息) def Confirm_Template(): @@ -99,10 +102,11 @@ def Confirm_Template(): ] ) ) + return message -#旋轉木馬按鈕訊息介面 +#旋轉木馬按鈕訊息介面 def Carousel_Template(): message = TemplateSendMessage( alt_text='一則旋轉木馬按鈕訊息', @@ -168,8 +172,10 @@ def Carousel_Template(): ] ) ) + return message + #TemplateSendMessage - ImageCarouselTemplate(圖片旋轉木馬) def image_carousel_message1(): message = TemplateSendMessage( @@ -207,6 +213,5 @@ def image_carousel_message1(): ] ) ) - return message -#關於LINEBOT聊天內容範例 \ No newline at end of file + return message \ No newline at end of file diff --git a/line_bot/new.py b/line_bot/new.py index 38e00b4..3f0b6cb 100644 --- a/line_bot/new.py +++ b/line_bot/new.py @@ -1,8 +1,8 @@ -#這些是LINE官方開放的套件組合透過import來套用這個檔案上 from linebot import (LineBotApi, WebhookHandler) from linebot.exceptions import (InvalidSignatureError) from linebot.models import * + def test(): message = TemplateSendMessage( alt_text='圖片旋轉木馬', @@ -39,4 +39,5 @@ def test(): ] ) ) + return message \ No newline at end of file diff --git a/line_bot/rich_menu.py b/line_bot/rich_menu.py index 197dfa7..f39a72c 100644 --- a/line_bot/rich_menu.py +++ b/line_bot/rich_menu.py @@ -20,6 +20,7 @@ headers = {"Authorization":"Bearer UmN0XsFJwcHP8lL7cPrvu30LXwqhMfni5+cTHdjrHKfOGW3DdgNh04ZmflN74CzwyhcRqiDCSzbnGDXhKxRwzXeYmO/1ELsnZFZnKJneME5cWq+hmbUjCongPvcsaSVOI1Ml6KfKoHybjIGM67pFXQdB04t89/1O/w1cDnyilFU=" , "Content-Type":"application/json"} # ==================================================== + # ''' # Step 1 : 設定一次就可以註解掉了 # ''' @@ -55,12 +56,13 @@ # print(req.text) # 在這裡要記起 rich_menu_id - # ==================================================== + ''' Step 2 : import 要的東西 這段也不能刪掉 ''' + from linebot import ( LineBotApi, WebhookHandler ) @@ -80,15 +82,14 @@ # ==================================================== - req = requests.request('POST', 'https://api.line.me/v2/bot/user/all/richmenu/'+rich_menu_id, headers=headers) print(req.text) rich_menu_list = line_bot_api.get_rich_menu_list() - # ==================================================== + """ 上面要重新設定的話要把 ID 刪掉重來 """ diff --git a/line_bot/scraper/law_scraper.py b/line_bot/scraper/law_scraper.py index 6af89ce..40768c5 100644 --- a/line_bot/scraper/law_scraper.py +++ b/line_bot/scraper/law_scraper.py @@ -1,8 +1,10 @@ -# coding=utf-8 +# coding=UTF-8 + from bs4 import BeautifulSoup import requests import re + def get_law_detail(article_of_fact): laws = ['刑法', '毒品危害防制條例', '商標法', '槍砲彈藥刀械管制條例', '藥事法', '家庭暴力防治法', '性騷擾防治法', '廢棄物清理法', '妨害兵役治罪條例', '性侵害犯罪防治法', '公職人員選舉罷免法', @@ -16,6 +18,7 @@ def get_law_detail(article_of_fact): # Convert fact to url re_string = '^([\u4e00-\u9fa5]*)第(\d+)條(之(\d)+)?$' match = re.findall(re_string, article_of_fact)[0] + try: pcode = pcodes[laws.index(match[0])] except: @@ -53,8 +56,8 @@ def get_law_detail(article_of_fact): law_detail += '\n' if idx != len(content) - 1 else '' true_idx += 1 - # print(law_detail) return law_detail + if __name__ == '__main__': get_law_detail('刑法第339條之1') \ No newline at end of file diff --git a/line_bot/scraper/today_news_scraper.py b/line_bot/scraper/today_news_scraper.py index ec4ca61..784f038 100644 --- a/line_bot/scraper/today_news_scraper.py +++ b/line_bot/scraper/today_news_scraper.py @@ -1,5 +1,6 @@ # Google search news '刑法' -# coding=utf-8 +# coding=UTF-8 + from bs4 import BeautifulSoup import requests import json @@ -7,6 +8,7 @@ def get_today_news(news_count): url = 'https://news.google.com/topics/CAAqJQgKIh9DQkFTRVFvSUwyMHZNREZ1TlhjU0JYcG9MVlJYS0FBUAE?hl=zh-TW&gl=TW&ceid=TW%3Azh-Hant' + # get html page and parse to soup response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") @@ -30,9 +32,8 @@ def get_today_news(news_count): urls.append(short_url) titles.append(content[i].text) - # print(urls) - # print(titles) return urls, titles + if __name__ == '__main__': get_today_news(3) \ No newline at end of file diff --git a/line_bot/scraper/yahoo_scraping.py b/line_bot/scraper/yahoo_scraping.py index 47daf73..24f06f9 100644 --- a/line_bot/scraper/yahoo_scraping.py +++ b/line_bot/scraper/yahoo_scraping.py @@ -1,13 +1,15 @@ # Yahoo news -# coding=utf-8 +# coding=UTF-8 + # 1. pick raw text excluding link and figure # 2. exclude first

with ".*報導$" -# 3. -# remove "【《〈()"" +# 3. remove "【《〈()"" + from bs4 import BeautifulSoup import requests import re + def get_news_content(url): # get html page and parse to soup response = requests.get(url) @@ -17,15 +19,18 @@ def get_news_content(url): content = soup.find('div', class_='caas-body') with open('page2.html', 'w') as f: f.write(content.prettify()) + print(content.text) + # get all

# all_paragraph = content.select("p[") # law_detail = '' + # for idx, a in enumerate(content): # if idx == 0: continue # law_detail += f'{idx}. {a.text}' # law_detail += '\n' if idx != len(content) - 1 else '' - # # print(law_detail) + # return law_detail if __name__ == '__main__': diff --git a/main.py b/main.py index 3994129..61a1269 100644 --- a/main.py +++ b/main.py @@ -88,4 +88,4 @@ do_test = args.do_test train(parameters, config, gpu_list, do_test) else: # mode == 'eval' - eval(parameters, config, gpu_list) + eval(parameters, config, gpu_list) \ No newline at end of file