Sunday, April 15, 2018

Artificial Intelligence Transgender: Kings and Queens

Researching for some Natural Language Processing tasks for a .NET application recently, I've come across Facebook's FastText library.

FastText is an open-source, free, lightweight library that allows users to learn text representations and text classifiers. It works on standard, generic hardware. Models can later be reduced in size to even fit on mobile devices.
It's written in C++ and builds on modern Mac OS and Linux distributions. Since it uses some C++11 features, it requires a compiler with good C++11 support. Python bindings are included.

Using this library, you can train models which represent words as multi-dimensional vectors. The models can be queried to find correlations between these vectors in the multi-dimensional space. Depending on the volume and quality of your input data, as well as the training parameters you specify, you can obtain better or worse results to your queries. An introductory explanation of how it works: king - man + woman is queen; but why?

An unofficial FastText fork for Windows is available on GitHub. I'm using this as a starting point to create a DLL in Visual Studio, exposing the C++ classes in a "flattened" C-style API as described in the article Using C++ objects in Delphi by Rudy Velthuis. Such a DLL can also be used from .NET via platform invoke.

As a result, when it's done I'll be able to use this library from Python, .NET, Delphi/Free Pascal and JavaScript (by embedding ChakraCore).

Some examples of console output from my experiments:

King - Man + Woman = ? (JavaScript)

C:\Code\fasttextConsole\chakra\Win64\Debug>ftcc ft.js
Loading file "C:\Data\fasttext\wiki\enwik9.bin"...
Computing vectors...
positive words:
king woman
negative words:
"queen": 0.7796817421913147
"regnant": 0.7554017305374145
"consort": 0.7433754205703735
"daughter": 0.7231032848358154
"throne": 0.721994161605835

Berlin - Germany + Argentina = ? (C#)

Loading file "C:\Data\fasttext\wiki\enwik9.bin"...done.
Computing vectors...done.
positive words:
berlin argentina
negative words:
"aires": 0.8183396
"buenos": 0.8142648
"argentinan": 0.7616609
"argentinas": 0.7580159
"caracas": 0.740073

Berlin - Germany + Slovakia = ? (Free Pascal)

Loading file 'C:\Data\fasttext\wiki\enwik9.bin'...done.
Computing vectors...done.
positive words:
berlin slovakia
negative words:
'zagreb': 0.81 (Oops! ;-))
'bratislava': 0.79 (Yeah!)
'budapesti': 0.79
'slavonski': 0.79
'podgorica': 0.78

Playstation - Sony + Nintendo = ? (JavaScript)

C:\Code\fasttextConsole\chakra\Win64\Debug>ftcc ft.js
Loading file "C:\Data\fasttext\wiki\enwik9.bin"...
Computing vectors...
positive words:
playstation nintendo
negative words:
"gamecube": 0.8729094862937927
"nintendogs": 0.8490696549415588
"playstationjapan": 0.840140163898468
"snes": 0.8312469720840454
"sega": 0.822517454624176

Here are some code examples:

1. C-style API (DLL exported functions)
2. Imports for .NET
3. Wrapper class for .NET
4. C# usage
5. Imports for Pascal
6. Pascal usage
7. Pascal class for ChakraCore host
8. JavaScript usage

Monday, April 02, 2018

First steps with Lazarus Qt5 interface

Steps for building and installing Qt5 for Lazarus

Using a quick test project with relevant code shown here: unit1.pas
I'm using Qt 5.6.2 because that's the version with which Lazarus qt5 interface has been tested. You can experiment with later versions, too.

Windows 32-bit

Using MinGW

$(LazarusDir): C:\fpcupdeluxe\lazarus
$(QtDir): C:\Qt\Qt5.6.2

- download: qt-opensource-windows-x86-mingw492-5.6.2.exe
- install
- open command prompt in the bindings directory $(LazarusDir)\lcl\interfaces\qt5\cbindings
set PATH=$(QtDir)\5.6\mingw49_32\bin;$(QtDir)\Tools\mingw492_32\bin;%PATH%
mingw32-make install
$(FpcDir)\bin\i386-win32\strip.exe $(QtDir)\5.6\mingw49_32\bin\Qt5Pas1.dll

Windows 64-bit

Using Visual Studio 2015

$(LazarusDir): C:\fpcupdeluxe\lazarus
$(QtDir): C:\Qt\Qt5.6.2

- download: qt-opensource-windows-x86-msvc2015_64-5.6.2.exe
- install
- fix the binding sources in the bindings directory $(LazarusDir)\lcl\interfaces\qt5\cbindings to be able to compile with Visual Studio: patch
- open command prompt in the bindings directory $(LazarusDir)\lcl\interfaces\qt5\cbindings
set PATH=$(QtDir)\5.6\msvc2015_64\bin;%PATH%
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

- edit the Makefile to remove -Wfatal-errors from CXXFLAGS
nmake install

OSX 64-bit

$(LazarusDir): ~/fpcupdeluxe/lazarus
$(QtDir): ~/Qt5.6.2

- download: qt-opensource-mac-x64-clang-5.6.2.dmg
- install
- open terminal in the bindings directory $(LazarusDir)/lcl/interfaces/qt5/cbindings
sudo make install

Linux 64-bit (Mint 18.3 Sylvia)

$(LazarusDir): ~/fpcupdeluxe/lazarus
$(QtDir): ~/Qt5.6.2

- make sure OpenGL headers are installed:
sudo apt install mesa-common-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/mesa/ /usr/lib/
sudo ldconfig

- download:
- install
Modify the system-wide config in /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf to point to your Qt5 directories:

- open terminal in the bindings directory $(LazarusDir)/lcl/interfaces/qt5/cbindings
sudo make install
sudo strip $(QtDir)/5.6/gcc_64/lib/

Happy Easter!

Sunday, February 11, 2018

Not interested in improving your product?

This morning I've found some sad news over at The Wiert Corner.

Apparently, Jeroen Wiert Pluimers - a valued member of the Delphi community, whose work I appreciate a lot - got his MVP status and technical partner licenses revoked. Jeroen won't be able to support Delphi community and open source projects as much as before because the latest licenses of his own are Delphi 2007 and XE4.

Jeroen's been a very active member of the Delphi community over the years, providing countless articles, blog posts, comments, bug reports and constructive participation in technical discussions. His contributions were always standing out as helpful, to the point, clearly with the aim and wish to improve Delphi, to provide value to the people involved, to support the community, and, need I really say it, thus also the company selling the product.

What happened, Embarcadero? Care to comment on this? Since this might be interpreted as "constructive criticism not welcome here".

I hope this could still come out as a misunderstanding which can be sorted out and forgotten soon.

Sunday, February 04, 2018

Friday, January 12, 2018

chakracore-delphi initial release

The initial release of chakracore-delphi is out, published under the MIT license.

Short summary:

Comments, ideas and of course pull requests are very welcome. Cheers!

Saturday, January 06, 2018


With the initial release coming up soon, here are a few teaser screenshots of chakracore-delphi, a new opensource library with Delphi and Free Pascal bindings and classes for Microsoft's ChakraCore Javascript engine:

FPC 3.0.4 and Lazarus 1.8 build (win64):

FPC 3.0.4 and Lazarus 1.8 build (linux64):

FPC 3.0.4 and Lazarus 1.8 build (macosx64):

Delphi XE 10.2 Tokyo build (win64):

Delphi 2009 build (win32):

Delphi 7 build (win32):

and a tiny code snippet showing an example of embedding the Javascript engine and exposing some native methods to it: