-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multilingual spam classifier #33
Open
yanisdb
wants to merge
6
commits into
zenodo:master
Choose a base branch
from
yanisdb:feature/msc_bert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e9e267
Add BERT experiments
yanisdb 6287a91
Add BERT multilingual classification
yanisdb 250bcf8
Fix train and restart from checkpoints
yanisdb 6130d89
Add visualization
yanisdb 536f3df
Add missing scripts for experiments
yanisdb 4bf2a8e
Few fixes that we forgot to push
yanisdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# Project specific files | ||
data/* | ||
models/* | ||
|
||
# Logs | ||
*.log | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# Environments | ||
.env | ||
.venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Experiments | ||
|
||
Luka Secilmis, Thomas Ecabert, Yanis De Busschere | ||
|
||
## Abstract | ||
|
||
This folder contains all the notebooks used for experimenting with the differents models during the project. | ||
|
||
## Summary of experiments | ||
|
||
| Experiment | Folder | Training Time | Prediction Time | Accuracy | F1-Score | | ||
|---------------------|----------------------------------------------------|---------------|-----------------|----------|----------| | ||
| BERT (english only) | [./en-spam-classifier](./en-spam-classifier) | 1h02 | 0.005s | 98.759 | 98.600 | | ||
| BERT (multilingual) | [./multi-spam-classifier](./multi-spam-classifier) | 1h09 | 0.005 | 98.814 | 98.779 | | ||
|
||
All computation and time measurement were made using an NVIDIA RTX A5000. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### BERT (English only) | ||
|
||
## Abstract | ||
|
||
We developed an NLP-based spam classifier through a transfer learning approach, by fine-tuning a pre-trained English DistilBERT model on the Zenodo dataset for text classification. | ||
|
||
## Results | ||
|
||
| Training Time | Prediction Time | Accuracy | F1-Score | | ||
|---------------|-----------------|----------|----------| | ||
| 1h02 | 0.005s | 98.759 | 98.600 | | ||
|
||
All computation and time measurement were made using an NVIDIA RTX A5000. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pandas as pd | ||
from bs4 import BeautifulSoup | ||
from ftlangdetect import detect | ||
import re | ||
|
||
KEEP = ['description', 'spam'] | ||
SPAMS = pd.DataFrame() | ||
HAMS = pd.DataFrame() | ||
CLEANING_REGEX = re.compile(r'[^a-zA-Z0-9\s]', re.MULTILINE) | ||
|
||
def detect_lang(descr): | ||
descr = CLEANING_REGEX.sub('', descr) | ||
descr = descr.replace('\r', ' ').replace('\n', ' ') | ||
lang = detect(descr)['lang'] | ||
return lang | ||
|
||
for chunk in pd.read_json('zenodo_open_metadata_2020-10-19.jsonl', lines=True, chunksize=100000): | ||
chunk = chunk[KEEP].dropna() | ||
|
||
chunk_spams = chunk[chunk['spam'] == True] | ||
chunk_spams['description'] = chunk_spams['description'].map(lambda x: BeautifulSoup(x, 'html.parser').get_text()) | ||
chunk_spams['lang'] = chunk_spams['description'].map(lambda x: detect_lang(x) if not pd.isna(x) else None).dropna() | ||
chunk_spams = chunk_spams[chunk_spams['lang'] == 'en'] | ||
chunk_spams = chunk_spams.drop(columns=['lang']) | ||
chunk_spams['spam'] = chunk_spams['spam'].map(lambda x: 1) | ||
SPAMS = pd.concat([SPAMS, chunk_spams]) | ||
|
||
chunk_hams = chunk[chunk['spam'] == False] | ||
chunk_hams['description'] = chunk_hams['description'].map(lambda x: BeautifulSoup(x, 'html.parser').get_text()) | ||
chunk_hams['lang'] = chunk_hams['description'].map(lambda x: detect_lang(x) if not pd.isna(x) else None).dropna() | ||
chunk_hams = chunk_hams[chunk_hams['lang'] == 'en'] | ||
chunk_hams = chunk_hams.drop(columns=['lang']) | ||
chunk_hams['spam'] = chunk_hams['spam'].map(lambda x: 0) | ||
HAMS = pd.concat([HAMS, chunk_hams]) | ||
|
||
HAMS = HAMS.sample(n= 2*len(SPAMS)) | ||
df = pd.concat([SPAMS, HAMS]).rename(columns={'spam': 'label'}) | ||
|
||
df.to_csv('dataset-esc.csv', index=False) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep the reference to the dataset DOI, to make it easier to get hold of the (test) data for training.