DevStory

Building a Document Classifier with Deep Learning

Sharing the story of developing a dev document classifier for the Daily Devblog service.

Project Introduction

I subscribe to a great service called Daily Devblog (sadly, the service has since shut down), which collects posts from IT blogs and emails them every morning at 10 AM. The posts it sends come from Awesome Devblog, and every now and then a post unrelated to IT shows up. It's not a big problem, but as a devoted subscriber, I hoped to help out even a little, so I built a simple document classifier using deep learning. ๐ŸŽ‰

You can find the code and data used in development at the links below.


Development Process

The overall development went in this order: data collection and preprocessing, developing a word embedding model, and then developing a document classification model.


Data Crawling & Labeling

I used the data provided by Awesome Devblog.

data sample
{
  _id        : "5e566644e0ffb12d4cae83ba",
  title      : "[์˜ํ™” ์†Œ๊ฐ] 1917. ๋•Œ๋ก  ์˜์›…์€ ์˜†์œผ๋กœ ๋‹ฌ๋ฆฐ๋‹ค.",
  link       : "http://blog.hwang.gg/20200226/",
  date       : "2020-02-26T11:45:20.000Z",
  description: "ํ•œ๊ตญ ์‚ฌํšŒ์—์„œ ์กฐ์ง ๋ฌธํ™” ๊ฐœ์„ ์„ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” ๋ชฉ์ˆจ์„ ๋‚ด๋˜์ง€๊ณ  ์˜†์œผ๋กœ ๋‹ฌ๋ ค์•ผ ํ•˜๋Š”๊ฐ€?",
  author     : "ํ™ฉ๊ฑด๊ตฌ",
  imgUrl     : "http://blog.hwang.gg/favicon.png",
  tags       : [ ],
  count      : 0,
  postImage  : "",
  _class     : "com.sarojaba.awesomedevblog.feed.Feed"
}

As of early February 2020, there were 34,620 posts in total, and I labeled 10,382 of them by hand. Labeling was the hardest part.. I only managed to label about a third of the whole dataset, but I wanted to see results, so I moved on to the next steps.

Labeling criteria
Category                       Label   Count
Not yet labeled                  -1    24,238
Related to development            1     7,634
Not related to development        0     2,748

Among the roughly 10,000 labeled items, the ratio of development-related to unrelated posts was about 3:1, with more development-related posts.

Of the labeled data, 0.74 are development documents and 0.26 are not, so development documents are the large majority.
Ratio of labeled data

Preprocessing

The longest representative sentence is 358 characters, the shortest is 3 characters, and the average is 145 characters. Since I planned to use the average of the embedded words as each document's representative value, I didn't truncate sentences to a specific length or add padding.

Max sentence length : 358
Min sentence length : 3
Mean sentence length : 145.02
Sentence length std : 60.59
Median sentence length : 132
Sentence length 1st quartile : 108
Sentence length 3rd quartile : 203
Log-scale histogram of representative sentence lengths. There are two peaks, around 100 and around 200 characters, and almost no sentences over 300.
Log-Histogram of representative sentence lengths

The link in the data from Awesome Devblog lets you fetch the post's HTML body, but many old links no longer exist, so this time I simply used only title, tags, and description, preprocessed as follows.

Preprocessing steps
Join the tags array with spaces
Keep only English, Korean, and whitespace
Remove html tags
Remove \n, \r
Collapse 2 or more consecutive spaces into one
Convert English uppercase to lowercase
Strip leading and trailing whitespace
Exclude data with an empty title
Exclude data whose title contains the word 'about'

Data whose title contains the word 'about' was without exception a blog owner's self-introduction, which I judged didn't need to appear in Daily Devblog, so I excluded it. I combined tags, title, and description into a single representative sentence, text.

Example of data used for training
{
  text  : "์˜ํ™” ์†Œ๊ฐ ๋•Œ๋ก  ์˜์›…์€ ์˜†์œผ๋กœ ๋‹ฌ๋ฆฐ๋‹ค ํ•œ๊ตญ ์‚ฌํšŒ์—์„œ ์กฐ์ง ๋ฌธํ™” ๊ฐœ์„ ์„ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” ๋ชฉ์ˆจ์„ ๋‚ด๋˜์ง€๊ณ  ์˜†์œผ๋กœ ๋‹ฌ๋ ค์•ผ ํ•˜๋Š”๊ฐ€"
  label : 0
}
Word cloud of the preprocessed sentences. Common words and leftover ad-code fragments such as dataadslot and adsbygoogle stand out as the largest words.
A WordCloud of the words in the preprocessed representative sentences.

Word Embedding

For Word Embedding, I used Gensim FastText. At first I tried the crawled data to train the embedding model, but since it was scraped from RSS feeds, the sentences were cut off or contained only html tags, so the data quality was poor and the amount insufficient, and the results were underwhelming. So I built the embedding model using 4.49GB of Korean wiki data. The embedding model alone became quite heavy at 14.5GB, but the embedding results got much better.

Using the embedding model to get the Top 5 words similar to โ€œํŒŒ์ด์ฌโ€ (Python).
$ python word_embedding.py --predict 'ํŒŒ์ด์ฌ'
[
  ('Python', 0.565061628818512),
  ('์ž์ด์ฌ', 0.5624369382858276),
  ('๋ ˆ์ผ์Šค', 0.5598082542419434),
  ('ํŒŒ์ด์ฌ์„', 0.5595801472663879),
  ('์–ธ์–ด์šฉ', 0.5288202166557312)
]

Next comes embedding the representative sentences as vectors using the embedding model. The embedding model embeds a single word as a vector of shape (300,), and as explained earlier, I embedded each word in a representative sentence and used the average as the sentence's vector.

embedding function
def embedding(self, we_model, text, embedding_dim=300):
    """
    Vectorize the given sentence word by word and return the average as the sentence vector

    - input
    : we_model / FastText word embedding model
    : text / str / the sentence to vectorize
    : embedding_dim / int / number of dimensions of the we_model vector (300 for wiki-based fasttext)

    - return
    : nparray / shape = (embedding_dim)
    """
    words = text.split(' ')
    words_num = len(words)

    # model dimension (300 for wiki fasttext)
    vector = np.zeros(embedding_dim)
    for word in words:
        vector += we_model[word]
    return vector/words_num

Developing the Classifier Model

Using tf.keras in Tensorflow 2.0.1, I implemented it simply by stacking just three Dense Layers. I also tried an LSTM, but its performance was worse, so I used only Dense Layers.

layer
model = Sequential()
model.add(Dense(100, activation='relu', kernel_initializer='he_normal', input_shape=(X_train.shape[1],)))
model.add(Dense(80, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['acc',
                        self.f1_m,
                        self.precision_m,
                        self.recall_m])

Performance Test

I tested the model's performance using 33% (3,426) of the total 10,382 items as test data, and measured a validation accuracy of 88 percent.

Model Performance
loss : 0.278
accuracy : 0.883
f1_score : 0.847
precision : 0.739
recall : 1.0

I then used the resulting model to filter the titles of the 41 posts sent by the actual Daily Devblog on February 3, 2020. (Posts whose titles consisted only of numbers were excluded.) Since I used a comma as the delimiter for multiple predictions, I removed all commas (,) in the titles.

Running predict (predicting multiple sentences at once, separated by commas)
python predict.py --predict '๋งฅ๋ถ ์ดˆ๋ณด์ž๋“ค์ด ๊ผญ ์•Œ์•„์•ผ ํ•  ๋‹จ์ถ•ํ‚ค top 5! mac keyboard shortcut 5. feat.๋งฅ๋ถ์„ ์‚ฐ  ์ด์œ , [Spring Boot] ๋‚ด์žฅ ์›น ์„œ๋ฒ„ - 2 (์Šคํ”„๋ง๋ถ€ํŠธ HTTPS / HTTP2), ๋„ค์ด๋ฒ„ํด๋ผ์šฐ๋“œํ”Œ๋žซํผ Certificate Manager ์— LetsEncrypt ์ธ์ฆ์„œ ๋“ฑ๋ก, Go๋กœ ๋ธ”๋ก์ฒด์ธ ๋งŒ๋“ค๊ธฐ #1, Ansible ํŒŒ์ผ ๋งˆ์ง€ ๋ง‰ ๋ณ€๊ฒฝ ์ผ์ž ํ™•์ธํ•˜๊ธฐ, ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘, [11775]Compactness criteria for clustering, ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค CI/DI ๋ฅผ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ ์•Œ์•„๋ณด๊ธฐ, [Windows] USB ์œˆ๋„์šฐ 10 ์„ค์น˜ /  ๋‹ค์šด๋กœ๋“œ ๋ฐฉ ๋ฒ•, [Spring Boot] JARํŒŒ์ผ(๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๊ฐ€๋Šฅ), ์ผ€๋ผ์Šค(Keras)์˜ get_file ํ•จ์ˆ˜, ์›น ์„œ๋น„์Šค Maintenance Mode (์ ๊ฒ€ ๋ชจ๋“œ) ์ง€์›๊ธฐ, Nodejs AES 128 CCM ์•”ํ˜ธํ™”(๋ณตํ˜ธํ™”) ์˜ˆ์ œ - crypto, ์˜์–ดํŒจํ„ด#37, [์šด์˜์ฒด์ œ(OS)] 7. ์“ฐ๋ ˆ๋“œ(Thread), ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์™€ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๊ทธ๋ฆฌ๊ณ  Promise, docker๊ธฐ๋ฐ˜ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”ํˆด Superset ์„ค์น˜ํ•˜๊ธฐ (๋ฆฌ๋ˆ…์Šค), Wayland๊ณผ Weston, ํ…Œ์ŠคํŠธ ์ฃผ๋„ ๊ฐœ๋ฐœ(Test-Driven Development:By Example) - 1๋ถ€ : ํ™”ํ ์˜ˆ์ œ (9 ~ 10์žฅ), ๋‹น์‹ ์€ ์ •๋ง ์˜์ง€๊ฐ€ ์žˆ๋Š”๊ฐ€?, [B๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋จธ] 1์›” 5์ฃผ ์†Œ์‹(๋น…๋ฐ์ดํ„ฐ/์ธ๊ณต์ง€๋Šฅ ์ฝ์„๊ฑฐ๋ฆฌ ๋ถ€๋ฌธ), ์†ํฅ๋ฏผ ์‹œ์ฆŒ 13ํ˜ธ๊ณจ! epl 25๋ผ์šด๋“œ ํ† ํŠธ๋„˜ vs ๋งจ์ฒด์Šคํ„ฐ์‹œํ‹ฐ, [์šด์˜์ฒด์ œ(OS)] 9. ํ”„๋กœ์„ธ์Šค ๋™๊ธฐํ™” 2, ์ž๋ฐ” String StringBuilder ๊ทธ๋ฆฌ๊ณ  StringBuffer ์ฐจ์ด ๋น„๊ต, (์—…๋ฌด)2020๋…„ 2์›” 3์ผ REACT๋กœ ์ž„์ƒ์‹œํ—˜์ž๋™ํ™”์†”๋ฃจ ์…˜ ๊ฐœ๋ฐœ Start, [์šด์˜์ฒด์ œ(OS)] 11. ๋ชจ๋‹ˆํ„ฐ, ํ•„๋ฆฌํ•€ ์„ธ๋ถ€ ์‹œํ‹ฐ์˜ ๋ง›์ง‘! ํ•˜์šฐ์Šค ์˜ค๋ธŒ ๋ ˆ์ดŒ cebu city House of Lechon, [์šด์˜์ฒด์ œ(OS)] 8. ํ”„๋กœ์„ธ์Šค ๋™๊ธฐํ™” 1, ์˜์–ดํŒจํ„ด#38, c์–ธ์–ด fopen ํ•จ์ˆ˜ : ํŒŒ์ผ์„ ์—ฐ๋‹ค., ์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA์™€ Querydsl ์ธํ”„๋Ÿฐ ๊ฐ•์˜ ์ •๋ฆฌ, [Sprint #10] Server Side Techniques Sprint, ์นด์นด์˜ค๋ฉ”์ผ IMAP / POP3 / SMTP ์„ค์ •๋ฐฉ๋ฒ•, [Algorithm] ์ด์ง„ํŠธ ๋ฆฌ์˜ ๊ตฌํ˜„ ๊ณผ ์ˆœํšŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜, [์šด์˜์ฒด์ œ(OS)] 10. ํ”„๋กœ์„ธ์Šค ๋™๊ธฐํ™” 3, [๋งฅ๋ถ] ํŠธ๋ž™ํŒจ๋“œ ์ œ์Šค์ฒ˜, [์Šคํ”„๋ง ๋ถ€ํŠธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ] ๋กœ๊น…, ๊ธฐ์ˆ  ๋‰ด์Šค #143 : 20-02-03, [์Šคํ”„๋ง ๋ถ€ํŠธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ] Profile, ์–ธ ๋• ๋„ˆ๋จธ'

Looking at the results below, apart from the post Ansible ํŒŒ์ผ ๋งˆ์ง€๋ง‰ ๋ณ€๊ฒฝ ์ผ์ž ํ™•์ธํ•˜๊ธฐ ("Checking the last modified date of a file in Ansible"), which was judged as a non-dev document, 39 of the 40 posts were classified correctly, for a performance of 97.5%. ๐ŸŽ‰๐ŸŽ‰

Judged as non-dev documents
(False, 0.191) ์˜์–ดํŒจํ„ด#37
(False, 0.013) ๋‹น์‹ ์€ ์ •๋ง ์˜์ง€๊ฐ€ ์žˆ๋Š”๊ฐ€?
(False, 0.041) ์†ํฅ๋ฏผ ์‹œ์ฆŒ 13ํ˜ธ๊ณจ! epl 25๋ผ์šด๋“œ ํ† ํŠธ๋„˜ vs ๋งจ์ฒด์Šคํ„ฐ์‹œํ‹ฐ
(False, 0.051) ํ•„๋ฆฌํ•€ ์„ธ๋ถ€ ์‹œํ‹ฐ์˜ ๋ง›์ง‘! ํ•˜์šฐ์Šค ์˜ค๋ธŒ ๋ ˆ์ดŒ cebu city House of Lechon
(False, 0.191) ์˜์–ดํŒจํ„ด#38
(False, 0.053) ์–ธ๋• ๋„ˆ๋จธ
(False, 0.185) Ansible ํŒŒ์ผ ๋งˆ์ง€๋ง‰ ๋ณ€๊ฒฝ ์ผ์ž ํ™•์ธํ•˜๊ธฐ ๐Ÿ˜“
Judged as dev documents
(True, 0.983) ๋งฅ๋ถ ์ดˆ๋ณด์ž๋“ค์ด ๊ผญ ์•Œ์•„์•ผ ํ•  ๋‹จ์ถ•ํ‚ค top 5! mac keyboard shortcut 5. feat.๋งฅ๋ถ์„ ์‚ฐ  ์ด์œ 
(True, 1.000) [Spring Boot] ๋‚ด์žฅ ์›น ์„œ๋ฒ„ - 2 (์Šคํ”„๋ง๋ถ€ํŠธ HTTPS / HTTP2)
(True, 0.971) ๋„ค์ด๋ฒ„ํด๋ผ์šฐ๋“œํ”Œ๋žซํผ Certificate Manager ์— LetsEncrypt ์ธ์ฆ์„œ ๋“ฑ๋ก
(True, 0.791) Go๋กœ ๋ธ”๋ก์ฒด์ธ ๋งŒ๋“ค๊ธฐ #1
(True, 1.000) ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘
(True, 0.973) [11775]Compactness criteria for clustering
(True, 0.989) ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค CI/DI ๋ฅผ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ ์•Œ์•„๋ณด๊ธฐ
(True, 0.992) [Windows] USB ์œˆ๋„์šฐ 10 ์„ค์น˜ /  ๋‹ค์šด๋กœ๋“œ ๋ฐฉ๋ฒ•
(True, 0.981) [Spring Boot] JARํŒŒ์ผ(๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๊ฐ€๋Šฅ)
(True, 1.000) ์ผ€๋ผ์Šค(Keras)์˜ get_file ํ•จ์ˆ˜
(True, 0.987) ์›น ์„œ๋น„์Šค Maintenance Mode (์ ๊ฒ€ ๋ชจ๋“œ) ์ง€์›๊ธฐ
(True, 0.996) Nodejs AES 128 CCM ์•”ํ˜ธํ™”(๋ณตํ˜ธํ™”) ์˜ˆ์ œ - crypto
(True, 0.706) [์šด์˜์ฒด์ œ(OS)] 7. ์“ฐ๋ ˆ๋“œ(Thread)
(True, 1.000) ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์™€ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๊ทธ๋ฆฌ๊ณ  Promise
(True, 0.994) docker๊ธฐ๋ฐ˜ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”ํˆด Superset ์„ค์น˜ํ•˜๊ธฐ (๋ฆฌ๋ˆ…์Šค)
(True, 0.980) Wayland๊ณผ Weston
(True, 0.937) ํ…Œ์ŠคํŠธ ์ฃผ๋„ ๊ฐœ๋ฐœ(Test-Driven Development:By Example) - 1๋ถ€ : ํ™”ํ ์˜ˆ์ œ (9 ~ 10์žฅ)
(True, 0.584) [B๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋จธ] 1์›” 5์ฃผ ์†Œ์‹(๋น…๋ฐ์ดํ„ฐ/์ธ๊ณต์ง€๋Šฅ ์ฝ์„๊ฑฐ๋ฆฌ ๋ถ€๋ฌธ)
(True, 0.999) [์šด์˜์ฒด์ œ(OS)] 9. ํ”„๋กœ์„ธ์Šค ๋™๊ธฐํ™” 2
(True, 1.000) ์ž๋ฐ” String StringBuilder ๊ทธ๋ฆฌ๊ณ  StringBuffer ์ฐจ์ด ๋น„๊ต
(True, 0.892) (์—…๋ฌด)2020๋…„ 2์›” 3์ผ REACT๋กœ ์ž„์ƒ์‹œํ—˜์ž๋™ํ™”์†”๋ฃจ์…˜ ๊ฐœ๋ฐœ Start
(True, 0.544) [์šด์˜์ฒด์ œ(OS)] 11. ๋ชจ๋‹ˆํ„ฐ
(True, 0.999) [์šด์˜์ฒด์ œ(OS)] 8. ํ”„๋กœ์„ธ์Šค ๋™๊ธฐํ™” 1
(True, 1.000) c์–ธ์–ด fopen ํ•จ์ˆ˜ : ํŒŒ์ผ์„ ์—ฐ๋‹ค.
(True, 0.726) ์Šคํ”„๋ง ๋ฐ์ดํ„ฐ JPA์™€ Querydsl ์ธํ”„๋Ÿฐ ๊ฐ•์˜ ์ •๋ฆฌ
(True, 0.991) [Sprint #10] Server Side Techniques Sprint
(True, 0.463) ๊ธฐ์ˆ  ๋‰ด์Šค #143 : 20-02-03
(True, 0.632) ์นด์นด์˜ค๋ฉ”์ผ IMAP / POP3 / SMTP ์„ค์ •๋ฐฉ๋ฒ•
(True, 0.985) [Algorithm] ์ด์ง„ํŠธ ๋ฆฌ์˜ ๊ตฌํ˜„๊ณผ ์ˆœํšŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜
(True, 0.999) [์šด์˜์ฒด์ œ(OS)] 10. ํ”„๋กœ์„ธ์Šค ๋™๊ธฐํ™” 3
(True, 0.989) [๋งฅ๋ถ] ํŠธ๋ž™ํŒจ๋“œ ์ œ์Šค์ฒ˜
(True, 1.000) [์Šคํ”„๋ง ๋ถ€ํŠธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ] ๋กœ๊น…
(True, 1.000) [์Šคํ”„๋ง ๋ถ€ํŠธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ] Profile

Closing

In this version, I built the classifier lightly using only title / tags / description. Along the way I felt that clear labeling rules were needed, and since there was a lot of data that even a person would find confusing from title / tags / description alone, I was worried the results wouldn't turn out well, but fortunately applying it to real data gave more satisfying results than I expected. In the next version, I plan to add 20,000 more items and train by fetching the html document through each item's link instead of title / tags / description.

That wraps up the project of building a document classifier with deep learning.

Comments