Text Classification for PHP Applications

The task of text classification is one of the oldest in Natural Language Processing and was well considered in the past. There are some common methods for classification using Artificial Neural Networks, Support Vector Machines and other machine learning models. The problem with these approaches is the amount of training data that has to be found, normalized as well as labeled for a specific system. Another problem comes with the selection of features, which – in most cases – wastes a lot of time. Because of that, a simpler algorithm must be found to allow classification of text for people who are not interested in machine learning. Some approaches that reach these requirements already exist and were introduced here. This article provides a short introduction to one of these algorithms (NCD) as well as an implementation into PHP scripting language. This code can be simply used for an own PHP project.


Why should we choose compression algorithms for classification? The answer is easy. A compression algorithm tries to find a way to minimize data. For that, one way is to find similarities and reduce them. To tackle this task without a computer – just with paper and pen – how would you compress the following examples:

(I) aaaaaabbbbbbcccccc


(II) ccccccbbbbbbffffff

I decided to use a simple compression rule (just for this example)
A possible result for the first example: 6a6b6c
And for the second example: 6c6b6f
If we concatenate both examples together: 6a6b12c6b6f
The length of these two examples (I) and (II) are in both cases 18. The compressed version of these examples uses 6 tokens for each string. We compressed a 18 token string to a 6 token string. After this concatenate operation the length of the compressed string also could be 6+6 = 12 tokens, but the compression (6a6b12c6f) uses just 11 tokens. Our simple compression algorithm has found a similarity (the b sequence at the end of example 1 and at the start of example 2). For more complex texts a good compression algorithm finds similarities in words, structures and so one. To conclude, the more the length of the concatenated string differs from the length of the concatenated string that underwent the compression, the more are these two strings similar to each other. To express it with more colloquial words, the length of the compressed string depends on the similarity of the two input strings.

Normalized Compression Distance

The NCD algorithm is normally used to measure the quality of compression algorithms. For that, some theoretical considerations including the Kolmogorov complexity are necessary. Text classification using NCD is a bit more uncomplex, but not feasible without some definitions:

  • Z(x), Z(y)… Compression function (in theoretical considerations: a function that is able to build a shorter representation of a string or token stream), returns the length of the compressed representation
  • min(x,y)… Minimum function (calculates the more minimal value of two variables) returns x when x
  • max(x,y)… Maximal function (calculates the more maximal value of two variables) returns x when x > y and y when y >= x
  • NCD(x,y)… Normalized Compression Distance (calculates the compression distance with the formula below)
  • x, y, xy… Variables and Concatenation (x and y are variables, xy is the concatenation of x and y, which means the two token streams are combined together)

With these definitions and the related formula you can calculate the NCD for your input string (test string) against all of your known phrases. The comparison with the lowest NCD value is most likely to be the correct class.

PHP Implementation

I’ve implemented the NCD approach in PHP and the class is very easy to use. You can use this classifier to detect spam or classify other input. The example below shows the usage of the code:

require 'ncd.class.php';

$c = new NCD();

        "hi my name is alex, what about you?" => 'ok',
        "are you hungy? what about some fries?" => 'ok',
        "how are you?" => 'ok',
        "buy viagra or dope" => 'spam',
        "viagra spam drugs sex" => 'spam',
        "buy drugs and have fun" => 'spam'

print_r($c->compare('hi guy, how are you?'));
print_r($c->compare('buy viagra m0therfuck5r'));

The add() method requires an array that contains the phrases for the comparison and the corresponding classes. You can call the add() method as much as you want to add more examples. It is important to provide good classification examples to the classifier. Not well chosen examples can affect the classification performance.

The print_r() methods used in the fragment above output the following lines:

    [class] => ok
    [score] => 0.39285714285714
    [class] => spam
    [score] => 0.48387096774194

As can be seen above, “hi guy, how are you?” was successfully classified to ‘ok’ and “buy viagra m0therfuck5r” was correct classified to ‘spam’. The score value can give an insight into the relation between the input string and string with the minimal distance to the input string. If you’re testing your classifier and this value is to high for a clear example, then you should add another comparison example using the add() method for this class.

Get the code here.

I hope you enjoyed this article. Have fun with the code and don’t hesitate to contact me if you have any suggestions or ideas.

94 thoughts on “Text Classification for PHP Applications

  1. トリーバーチのサングラス中国製は偽物?,チェーン付き 財布 トリーバーチ,トリーバーチ 40代 さいふ says:

    コンバース オールスター 緑,コンバース hi,コンバース 運動靴
    uggアンスレーブログ,ugg ベビー ケアンズ,ゴールドコースト アグアウトレット
    エルメス ブリッ

  2. Pingback: 梅邦虫草精
  3. coats says:

    Thank you. Very good information!

  4. 哈尔滨风华甲状腺结节医院 says:


  5. トリーバーチ リュック 2013 カタログ says:

    トリーバーチ リュック 2013 カタログ

  6. トリーバーチ 財布 柄物 says:

    トリーバーチ 財布 柄物

  7. anna sui 財布 2014 says:

    リーダー、偉大のもの、私のにブログサイト私はちょうどこれを追加しました。 できない十分に得る!

  8. 経典,正規品SALE 「スタッキングミーティングチェア」 チェア ミーティングチェア 会議用チェア スタッキング 2色あり 新作,卸売価格 says:

    爆買い,セール アニエスベー 財布 さいふ agnes b. VOYAGE アニエスベー ボヤージュ 二つ折り財布 エナメル ブラック vfwh_08_blk 限定セール,本物保証

  9. 2015 ティンバーランド 女 says:

    2015 ティンバーランド 女

  10. Toms shoes says:

    Canada Goose onlinecheap Nike Free RunNike Air Max UK online shopCanada Goose outlet saleNike Free Run mens saleLouis Vuitton Canada outlet onlineMackage coatMulberry factoryNike KD shoes saleKevin Durant shoesCanadian Goose outlet storehttp://www.kobebryand9shoes.us/Moncler Toronto onlineWoolrichCanada Goose outletLouis Vuitton outlet Canada storeMoncler factory outletLebron James shoesLouis Vuittonhttp://www.drdrebeatsonlinestore.co.uk/http://www.tomsshoes-outletonline.com/Dr Dre Beats studio saleNike KD 7Woolrich outlet storehttp://www.mackageshop.us/outlet Moncler jacketsMackageMackage jackethttp://www.freerunonsales.com/Moncler UK saleshop Mulberry bagsMoncler USA outletMonclerdiscount Nike Air Max saleCanada Goose jacket outletMoncler outletNobis parkaMackage coat outletDr Dre BeatsNike Kobe 9 shoeshttp://www.monclerjacketsoutlets-uk.co.uk/Moncler outletWoolrich parka for saleCanada Goose parkaNike KD 7 shoes onlineNike Air Max salehttp://www.louisvuittonhandbagsoutlets.us/Nobis jacket outletToms shoes outlet onlineLebron James shoes saleMulberryMoncler jackets outletNike Air Max UKMoncler jacket onlineLouis Vuitton CanadaCanadian GooseCanadian Goose down coatMoncler jacketsMulberry factory shophttp://www.canadagooseonlineshops.ca/Kobe 9 eliteLouis Vuitton handbags outletNobisMonclerhttp://www.mackage-canada.ca/Moncler outlet UKMackage jackets on saleMackage coatsNike Kobe 9Nobis jackets saleNike Free Run mensToms shoes
    Toms shoes

  11. カナダグース レディース ピンク says:

    あなたウェブサイトありません正しく表示レンダリングに私の AppleのiPhone – もししようと修正したいという

  12. coach 財布 かわいい says:

    coach 財布 かわいい

  13. diablo 3 gold guide says:

    What I can do is to find frontrunners concerning this passing away wearingfusion clans coupled with allow them to form propagate mmorpgfusion matches AE:in. That is why plan has. So if you have an acquaintance who is desirable ample make sure to explain me. I throw a Sager 5760 also known as Clevo 570u. Such 17″ Video panel pc experiences 2gig good old ram, The multiple fundamental pair 2 2.0ghz, NVidia 7950 GTX 512meg debit credit cards, 120gig high-definition. (More or less $2,300) The actual normally can anyone declare that a laptop computer not be able to play technology xbox table exercises.
    diablo 3 gold guide http://www.madaboutart.biz/guides

  14. エルメス コピー says:


  15. 激安コピーブランド、スーパーコピー商品、N級ブランド通販、専業激安ブランドコピー商品販売店! says:

    ブランドコピー 代引き 国内発送 brandsecret-ブランドシークレットhttp://www.kristaharris.com

  16. http://www.bmxhawaii.com/4990r2BuSL3KolXeiCt1.html says:


  17. mbt women's moja shoe black says:

    http://www.teflalumni.com/mbt-usa/mbt-shoes-noticias.php mbt shoes noticias

  18. Mythiques Nike Femmes Runnings Air Jodan 6 says:

    I am curious to find out what blog platform you have been utilizing? I’m having some small security problems with my latest site and I’d like to find something more secure. Do you have any suggestions?

  19. http://villainkeri.com/sidebar.php?fodboldstoevler/6520-evospeed-kollektion/ says:

    Mine needed to come in by a couple more inches at the waist because it wasn’t fitting qui.First, term.Some analysts and Republican strategists say that as tough as the map looks for the GOP, therepublican and Democratic nominations.

  20. Large choix de Adidas Noir Vert says:

    Audio started playing any time I opened up this web page, so annoying!

  21. http://www.joshreso.com/843lR243jwQtGvBQjuf.html says:

    http://www.kanberinsaat.com/6159JfMy2F9bX2SZTGlv.html経典,激安 SHOEI J-CRUISE ヘルメット【マットブラック】【ショウエイ バイク用 ジェットヘルメット ショーエイ Jクルーズ】【smtb-k】 経典,最安値

  22. http://www.sublimimpact.com/4939B0MpL72mMXv6GhJj.html says:

    超激安,限定SALE ロダニア RODANIA クロコダイル レディース コインケース OKU0321BNSP GOLDBROWN 爆買い,新作登場

  23. 最安値 2015 [レビューを書いて3年保証][正規品] ゲス腕時計 GUESS 腕時計 メンズ ゲス 腕時計/メンズ ホリゾン HORIZON メンズ/ブルー W0379G3 [おしゃれ/ゲス 腕時計メンズ/ブランド/青/シルバー/ク says:


  24. Beste Joggesko Nike Menn Jogging Zoom Toppmøtet Hvitt Antrasitt Utfordre Rød says:

    Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks

  25. 2015激安限定,SALE Lynx Studio Technology HILO BK 格安,人気新作 says:

    http://www.sophiespeople.com/4479c9eBEX6hPyOYxwhF.html2015人気,送料無料 【店頭】ティーリングウィスキーカンパニーティーリング ヴィンテージリザーヴ 21年シルバーボトリング バッチ1 定番人気,豊富な

  26. http://www.fitnessgiardino.com/5sJyrnST4oC4Ef1f4685.html says:

    経典,2015人気新作 ブライトリング BREITLING クロノマット44 / Ref.B011A93PA 【腕時計】【メンズ】 人気セール,新作

  27. In virtue of essence about an all-covering airport, Samui provides a perimeter environment for retrogressive
    opulency investor.

  28. Vi.sualize.us says:

    Distinct, individual and putting, each of the choices inside our
    set of one of the better Samui seashore resorts
    is a winner.

  29. www.behance.net says:

    At Samui Island Villas we provide a variety of carefully chosen villas in Koh Samui which are since beautiful and diverse
    because the area itself.

  30. Morris says:

    Stayed inside their Resort into the Maldives before and it also ended up being just the most useful I ever practiced.

  31. Rosalyn says:

    That costs a really brilliant quantity of lease and comes demonstrably rubbishy, and great if you want on approach
    to work at one cost savings.

  32. roger vivier sale says:

    Text Classification for PHP Applications | code is poetry laboratories
    roger vivier sale http://www.naturterapeuten.com/images/rv.html

  33. retro 11 72-10 says:

    I need my very own \ gamma blue 11s! They can be super very good, \ I want individuals too much \
    retro 11 72-10 http://www.preorderjordan117210.org/

  34. http://www.findyourcheapjordans.com says:

    Please note, the quantity for some styles of the Nike products are limited, but we have been trying to update the latest and most fashionable Nike products for you always. We provide wholesale price if you buy in large quantity. For more information about the price, please contact our customer service.

  35. jordan sneakers for boys says:

    Official Jordan Shoes Outlet Online Store|Buy Jordan Shoes Handbags & Enjoy Jordan Shoes From Now Off Free Shipping.
    jordan sneakers for boys http://airsneakershoes.com

  36. cheap jordan shoes says:

    Air Jordan shoes have not only dominated the sports and particularly the Basketball, they also have been incorporated into the music world; for instance it’s nothing new observe pop music celebrities adorning the shoes or boots. Hollywood celebrities too have not been unnoticed.
    cheap jordan shoes http://blog.supplycheapjordans.com

  37. jordan sneakers for boys says:

    Real Air Jordans in C.G INC . Our company is located in PuTian. We supply various high quality products with good services and prompt delivery(jordan shoes). With 8 years of experience, we have won a good reputation among customers. We export 97% of our products and provide services range from design to final production. With high quality, reasonable prices and the best services, our products are popular in the worldwide markets.We always regard “self-innovation, pursuit of excellence, exciting entrepreneurial spirit, both quality and credibility” as the principle of our company. We continue to try our best to meet customers’ expectations. Our operating principle is “quality first, supremacy of customers and pursuits of excellence”. Our operating philosophy is “integrity and excellence, win-win cooperation and sustainable business”. For more information about our company and products, please feel free to contact us. We are ready to serve you sincerely and hope to establish long term cooperation with friends at home and abroad.
    jordan sneakers for boys http://airsneakershoes.com

  38. 天津阀门生产厂家 says:


  39. 超激安 TSI737-2394 2015春夏 サッカースパイク アシックス DS LIGHT WD ■対応グラウンド:土 天然芝 人工芝 新品 says:

    バニーセレクションネザーランドドワーフ1.3 人気高級

  40. 大人気 マウンテンダックス レラ-1 限定モデル,数量限定 says:

    2016新作 カリマー クーガー 40-55 免税激安

  41. adidas zx 750 says:

    tubular x

  42. 大人気 ペットメイト PETMATE(ペットメイト) Lookn'see ルックアンドシー ブルー バリケンネル キャリーバッグ 免税激安 says:

    激安店舗 MISSIONWORKSHOP ミッションワークショップ The Fitzroy APvxBlack [フィッツロイ][VXブラック][バックパック][36L] 魅力

  43. jordan 12 release date says:

    Free Shipping Air Jordan 4 Women Red Black White

  44. バーテープ、グリップ says:

    正規保証 フランチャコルタ·ヴェッツォーリ·ロゼ 750ml ワイン フランス·シャンパーニュ ロゼ 発泡 シャンパン スパークリング スパークリングワイン スパーク 低価格,経典

Leave a Reply

Your email address will not be published. Required fields are marked *