Bài Báo Cáo Nhóm 1 Môn Trí Tuệ Nhân Tạo.docx

8 4 0
Bài Báo Cáo Nhóm 1 Môn Trí Tuệ Nhân Tạo.docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC BÁCH KHOA ĐÀ NẴNG SAU ĐẠI HỌC ~~~~~~*~~~~~~ BÀI BÁO CÁO MÔN HỌC TÊN BÀI DỰ ĐOÁN CHỦ ĐỀ TIN TỨC BẰNG MULTINOMIALNB Đà Nẵng – 2022 Học viên thực hiện NGUYỄN HOÀNG A[.]

BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC BÁCH KHOA ĐÀ NẴNG SAU ĐẠI HỌC ~~~~~~*~~~~~~ BÀI BÁO CÁO MƠN HỌC TÊN BÀI: DỰ ĐỐN CHỦ ĐỀ TIN TỨC BẰNG MULTINOMIALNB Học viên thực : NGUYỄN HOÀNG ANH VŨ : Lớp : Môn học Giảng viên hướng dẫn : LÊ HỒNG PHƯƠNG VƯƠNG NHẬT QUANG K43.KMT TRÍ TUỆ NHÂN TẠO TS PHẠM MINH TUẤN Đà Nẵng – 2022 Mục lục Nội dung Tách từ thư viện pyvi Kiểm tra từ tách thuộc tính từ, động từ hay danh từ .3 Điếm số lượng tính từ, động từ hay danh từ chủ đề 4 Tìm xem từ sử dụng nhiều cho chủ đề 5 Dự đoán chủ đề tin tức MultinomialNB Nội dung Tách từ thư viện pyvi Kiểm tra từ tách thuộc tính từ, động từ hay danh từ Crawl liệu từ vnexpress import json import requests import sqlite3 import traceback from tqdm import tqdm from bs4 import BeautifulSoup, PageElement def get_meta_data(page, name: str): try: meta = page.find("meta", attrs={"name": name}) return meta["content"] except: return None def parse_single_news(url: str): if url is None: return try: item = BeautifulSoup(requests.get(url).text, "html.parser") # Parse title title = item.find("h1", class_="title-detail") if title is None: title = item.find("h1", class_="title-post") title = title.text.strip() if title is not None else None # Parse description desc = item.find("p", class_="description") desc = [desc.text.strip()] if desc is not None else [] # Parse content content = item.find("article", class_="fck_detail") if content is None: content = item.find("div", class_="fck_detail") content = content.find_all("p", class_="Normal") content = "\n".join(desc + [p.text.strip() for p in content]) return {"link": url, "title": title, "content": content} except Exception as e: # traceback.print_exc() # print(url, e) return None def get_news_url(article: PageElement): if article.find(class_="info-ads") is not None or article.find(class_="adsbyeclick") is not None: # Ignore ads return None try: title = article.find(class_="title-news") url = title.find("a")["href"] return url except: print(article) return None def parse_category_news_urls(category_url: str): soup = BeautifulSoup(requests.get(category_url).text) items = soup.find_all("article", attrs={"class": "item-news item-newscommon"}) u rls = filter(None, [get_news_url(item) for item in items]) return urls categories = ["Thời sự", "Góc nhìn", "Thế giới", "Kinh doanh", "Khoa học", "Thể thao", "Pháp luật", "Giáo dục", "Sức khỏe", "Đời sống", "Du lịch", "Số hóa", "Ơ tơ, xe máy", "Ý kiến"] category_urls = { "https://vnexpress.net/thoi-su", "https://vnexpress.net/goc-nhin", "https://vnexpress.net/the-gioi", "https://vnexpress.net/kinh-doanh", "https://vnexpress.net/khoa-hoc", "https://vnexpress.net/the-thao", "https://vnexpress.net/phap-luat", "https://vnexpress.net/giao-duc", "https://vnexpress.net/suc-khoe", "https://vnexpress.net/doi-song", "https://vnexpress.net/du-lich", "https://vnexpress.net/so-hoa", "https://vnexpress.net/oto-xe-may", "https://vnexpress.net/y-kien" } category_urls = dict(zip(categories, category_urls)) categories = ["Thời sự", "Góc nhìn", "Thế giới", "Kinh doanh", "Khoa học", "Thể thao", "Pháp luật", "Giáo dục", "Sức khỏe", "Đời sống", "Du lịch", "Số hóa", "Ơ tơ, xe máy", "Ý kiến"] category_urls = { "https://vnexpress.net/thoi-su", "https://vnexpress.net/goc-nhin", "https://vnexpress.net/the-gioi", "https://vnexpress.net/kinh-doanh", "https://vnexpress.net/khoa-hoc", "https://vnexpress.net/the-thao", "https://vnexpress.net/phap-luat", "https://vnexpress.net/giao-duc", "https://vnexpress.net/suc-khoe", "https://vnexpress.net/doi-song", "https://vnexpress.net/du-lich", "https://vnexpress.net/so-hoa", "https://vnexpress.net/oto-xe-may", "https://vnexpress.net/y-kien" } category_urls = dict(zip(categories, category_urls)) Điếm số lượng tính từ, động từ hay danh từ chủ đề Tìm xem từ sử dụng nhiều cho chủ đề Thống kê từ sử dụng chủ đề import collections import json from tqdm import tqdm from pyvi import ViTokenizer, ViPosTagger, ViUtils def statistic(s: str, kind = ["A", "V", "N"]): s = s.replace("\n", " ") tokenized = ViTokenizer.tokenize(s) tagged = ViPosTagger.postagging(tokenized) parts_of_speech = list(filter(lambda i: i in kind, tagged[1])) parts_of_speech_counter = collections.Counter(parts_of_speech) words = list(filter(None, [item[0].lower() if item[1] in kind else None for item in zip(tagged[0], tagged[1])])) word_counter = collections.Counter(words) return parts_of_speech_counter, word_counter with open("vnexpress.json") as f: data = json.load(f) counters = {} for category in data: category_name = category['category'] parts_of_speech_counter = collections.Counter() words_counter = collections.Counter() articles = filter(None, category["articles"]) for article in tqdm(articles, desc="Articles"): content = article["content"] if content is not None: result = statistic(content) parts_of_speech_counter += result[0] words_counter += result[1] parts_of_speech_counter = dict(parts_of_speech_counter.most_common()) words_counter = dict(words_counter.most_common()) counters[category_name] = {"parts_of_speech": parts_of_speech_counter, "words": words_counter} with open("thongke.json", "w", encoding='utf8') as f: json.dump(counters, f, ensure_ascii=False) Dự đoán chủ đề tin tức MultinomialNB import json import string from joblib import dump, load from pyvi import ViTokenizer, ViPosTagger from sklearn.feature_extraction.text import TfidfVectorizer from sklearn import preprocessing from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from sklearn.naive_bayes import MultinomialNB def tokenize(s: str) -> str: return ViTokenizer.tokenize(s).lower() with open("vnexpress.json", encoding="utf8") as f: data = json.load(f) X_Data = [] Y_Data = [] for category in data: for article in category['articles']: if article !=None and article['content'] != None: X_Data.append(tokenize(article['content'])) Y_Data.append(category['category']) with open("vietnamese-stopwords-dash.txt", encoding="utf8") as f: content = f.readlines() stop_words = [line.rstrip() for line in content] stop_words += ["\n"] stop_words += [c for c in string.punctuation] tfidf_vect = TfidfVectorizer(analyzer='word', stop_words=stop_words, max_features=3000) X_train_tfidf = tfidf_vect.fit_transform(X_Data) def train_model(classifier, X_data, y_data): X_train, X_val, y_train, y_val = train_test_split(X_data, y_data, stratify=y_data, test_size=0.1, random_state=42) classifier.fit(X_train, y_train) train_predictions = classifier.predict(X_train) val_predictions = classifier.predict(X_val) print("Train accuracy: ", accuracy_score(train_predictions, y_train)) print("Validation accuracy: ", accuracy_score(val_predictions, y_val)) dump(classifier, 'phanloai.joblib') train_model(MultinomialNB(), X_train_tfidf, Y_Data) content = ["Nội dung tin tức"] content = [tokenize(c) for c in content] content_tfidf = tfidf_vect.transform(content) clf = load('phanloai.joblib') print(clf.predict(content_tfidf)) Tài liệu tham khảo o o o o https://hyperledger-fabric.readthedocs.io/en/latest/install.html https://github.com/hyperledger/fabric-samples https://hyperledger-fabric.readthedocs.io/en/release-1.4/write_first_app.html https://www.youtube.com/watch?v=8tVx0r6pgU4

Ngày đăng: 19/06/2023, 21:26

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan