tpha4308 commited on
Commit
3c950f2
·
verified ·
1 Parent(s): 664c81e

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +105 -0
README.md ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - keyword-extraction
4
+ language:
5
+ - vi
6
+ ---
7
+
8
+
9
+ # <a name="introduction"></a> KeyBERTVi - Keyword Extraction for Vietnamese language
10
+
11
+ Inspired by [KeyBERT](https://github.com/MaartenGr/KeyBERT), KeyBERTVi implements a similar keyword extraction technique that leverages the embeddings of [PhoBERT](https://huggingface.co/vinai/phobert-base) and minimal linguistics properties to extract keywords and keyphrases that are most similar to the document.
12
+
13
+ <a name="toc"/></a>
14
+ ## Table of Contents
15
+ <!--ts-->
16
+ 1. [About the Project](#about)
17
+ 2. [Getting Started](#gettingstarted)
18
+ 2.1. [Installation](#installation)
19
+ 2.2. [Basic Usage](#usage)
20
+ 2.3. [Diversify Results](#diversify)
21
+ 3. [Limitations](#limitations)
22
+ <!--te-->
23
+
24
+ <a name="about"/></a>
25
+ ## 1. About the Project
26
+
27
+ This implementation took inspiration from the simple yet intuitive and powerful method of [KeyBERT](https://github.com/MaartenGr/KeyBERT/), applied for the Vietnamese language. PhoBERT are used to generate both document-level embeddings and word-level embeddings for extracted N-grams. Cosine similarity is then used to compute which N-grams are most similar to the document-level embedding, thus can be perceived as most representative of the document.
28
+ Preprocessing catered to the Vietnamese language was applied.
29
+
30
+ <a name="gettingstarted"/></a>
31
+ ## 2. Getting Started
32
+ <a name="installation"/></a>
33
+ ### 2.1. Setting up
34
+
35
+ ```bash
36
+ git clone https://huggingface.co/tpha4308/keyword-extraction-viet
37
+ ```
38
+
39
+ You can use existing pre-trained models in the repo or download your own and put them in `pretrained-models` folder.
40
+
41
+ ```python
42
+ phobert = AutoModel.from_pretrained("vinai/phobert-base-v2")
43
+ phobert.eval()
44
+ torch.save(phobert, f'{dir_path}/pretrained-models/phobert.pt')
45
+
46
+ ner_model = AutoModelForTokenClassification.from_pretrained("NlpHUST/ner-vietnamese-electra-base")
47
+ ner_model.eval()
48
+ torch.save(ner_model, f'{dir_path}/pretrained-models/ner-vietnamese-electra-base.pt')
49
+ ```
50
+
51
+ As [PhoBERT](https://huggingface.co/vinai/phobert-base) requires [VnCoreNLP](https://github.com/vncorenlp/VnCoreNLP) as part of pre-processing, the folder `pretrained-models/vncorenlp` is required. To download your own:
52
+ ```bash
53
+ pip install py_vncorenlp
54
+ ```
55
+
56
+ ```python
57
+ import py_vncorenlp
58
+
59
+ py_vncorenlp.download_model(save_dir=f'{dir_path}/pretrained-models/vncorenlp')
60
+ ```
61
+
62
+ <a name="usage"/></a>
63
+ ### 2.2. Basic Usage
64
+
65
+ ```python
66
+ phobert = torch.load(f'{dir_path}/pretrained-models/phobert.pt')
67
+ phobert.eval()
68
+ ner_model = torch.load(f'{dir_path}/pretrained-models/ner-vietnamese-electra-base.pt')
69
+ ner_model.eval()
70
+ kw_pipeline = KeywordExtractorPipeline(phobert, ner_model)
71
+ ```
72
+
73
+ ```python
74
+ title = "Truyền thuyết và hiện tại Thành Cổ Loa"
75
+ text = """
76
+ Nhắc đến Cổ Loa, người ta nghĩ ngay đến truyền thuyết về An Dương Vương được thần Kim Quy bày cho cách xây thành, về chiếc lẫy nỏ thần làm từ móng chân rùa thần và mối tình bi thương Mỵ Châu – Trọng Thủy. Đằng sau những câu chuyện thiên về tâm linh ấy, thế hệ con cháu còn khám phá được những giá trị khảo cổ to lớn của Cổ Loa.
77
+ Khu di tích Cổ Loa cách trung – tâm Hà Nội 17km thuộc huyện Đông Anh, Hà Nội, có diện tích bảo tồn gần 500ha được coi là địa chỉ văn hóa đặc biệt của thủ đô và cả nước. Cổ Loa có hàng loạt di chỉ khảo cổ học đã được phát hiện, phản ánh quá trình phát triển liên tục của dân tộc ta từ sơ khai qua các thời kỳ đồ đồng, đồ đá và đồ sắt mà đỉnh cao là văn hóa Đông Sơn, vẫn được coi là nền văn minh sông Hồng thời kỳ tiền sử của dân tộc Việt Nam.
78
+ Cổ Loa từng là kinh đô của nhà nước Âu Lạc thời kỳ An Dương Vương (thế kỷ III TCN) và của nước Đại Việt thời Ngô Quyền (thế kỷ X) mà thành Cổ Loa là một di tích minh chứng còn lại cho đến ngày nay. Thành Cổ Loa được các nhà khảo cổ học đánh giá là “tòa thành cổ nhất, quy mô lớn vào bậc nhất, cấu trúc cũng thuộc loại độc đáo nhất trong lịch sử xây dựng thành lũy của người Việt cổ”.
79
+ """
80
+ inp = {"title": title, "text": text}
81
+ kws = kw_pipeline(inputs=inp, min_freq=1, ngram_n=(1, 3), top_n=5, diversify_result=False)
82
+
83
+ [('Khu di_tích Cổ_Loa', 0.88987315),
84
+ ('Âu_Lạc thời_kỳ An_Dương_Vương', 0.8680505),
85
+ ('thành Cổ_Loa', 0.8661723),
86
+ ('hàng_loạt di_chỉ khảo_cổ_học', 0.8644231),
87
+ ('lịch_sử xây_dựng thành_luỹ', 0.8375939)]
88
+ ```
89
+
90
+ <a name="diversify"/></a>
91
+ ### 2.3. Diversify Results
92
+
93
+ More information needed
94
+
95
+ <a name="limitations"/></a>
96
+ ## 3. Limitations
97
+
98
+ More information needed
99
+
100
+ ## References
101
+ 1. https://github.com/MaartenGr/KeyBERT
102
+ 2. https://github.com/VinAIResearch/PhoBERT
103
+ 3. https://huggingface.co/NlpHUST/ner-vietnamese-electra-base
104
+ 4. https://github.com/undertheseanlp/underthesea
105
+ 5. https://github.com/vncorenlp/VnCoreNLP