├── NEWS ├── CONTRIBUTING.md ├── doc ├── bayes-glib-overrides.txt ├── version.xml.in ├── bayes-glib.types ├── Makefile.am ├── bayes-glib-sections.txt └── bayes-glib-docs.sgml ├── m4 ├── Makefile.am ├── ltversion.m4 ├── nls.m4 ├── ax_require_defined.m4 ├── ax_append_flag.m4 ├── ax_append_compile_flags.m4 ├── appstream-xml.m4 ├── ax_check_link_flag.m4 ├── ax_check_compile_flag.m4 ├── gsettings.m4 ├── vapigen.m4 ├── introspection.m4 ├── ax_compiler_vendor.m4 ├── ltsugar.m4 ├── vala.m4 ├── lt~obsolete.m4 ├── ax_cxx_compile_stdcxx_11.m4 ├── pkg.m4 ├── intltool.m4 ├── glib-gettext.m4 └── ltoptions.m4 ├── AUTHORS ├── data ├── Makefile.am └── bayes-glib-1.0.pc.in ├── README.md ├── tests ├── test-bayes-guess.c ├── Makefile.am └── test-bayes-storage-memory.c ├── autogen.sh ├── tools ├── Makefile.am ├── classifier.vala └── trainer.vala ├── src ├── bayes-glib.h ├── bayes-storage-memory-private.h ├── bayes-guess-private.h ├── bayes-version.h.in ├── bayes-guess.h ├── bayes-tokenizer.h ├── Makefile.am ├── bayes-guess.c ├── bayes-storage-memory.h ├── bayes-storage.c ├── bayes-classifier.h ├── bayes-storage.h ├── bayes-tokenizer.c ├── bayes-classifier.c └── bayes-storage-memory.c ├── Makefile.am ├── configure.ac └── git.mk /NEWS: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /doc/bayes-glib-overrides.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /doc/version.xml.in: -------------------------------------------------------------------------------- 1 | @VERSION@ 2 | -------------------------------------------------------------------------------- /m4/Makefile.am: -------------------------------------------------------------------------------- 1 | GITIGNOREFILES = gtk-doc.m4 2 | 3 | -include $(top_srcdir)/git.mk 4 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # Generated by Makefile. Do not edit. 2 | 3 | Ben Iofel 4 | Christian Hergert 5 | Prince781 6 | -------------------------------------------------------------------------------- /doc/bayes-glib.types: -------------------------------------------------------------------------------- 1 | bayes_classifier_get_type 2 | bayes_guess_get_type 3 | bayes_storage_get_type 4 | bayes_storage_memory_get_type 5 | bayes_tokens_get_type 6 | -------------------------------------------------------------------------------- /data/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | pkgconfigdir = $(libdir)/pkgconfig 3 | pkgconfig_DATA = bayes-glib-1.0.pc 4 | EXTRA_DIST = bayes-glib-1.0.pc.in 5 | 6 | 7 | GITIGNOREFILES = \ 8 | *.pc \ 9 | $(NULL) 10 | 11 | -include $(top_srcdir)/git.mk 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bayes-glib 2 | ---------- 3 | 4 | Bayesian classifier GObject-based library. 5 | 6 | ##Install 7 | ``` 8 | $ ./autogen.sh 9 | $ ./configure --enable-gtk-doc --prefix=/usr --enable-introspection --enable-vala 10 | $ sudo make install 11 | ``` 12 | -------------------------------------------------------------------------------- /data/bayes-glib-1.0.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: Bayes-GLib 7 | Description: Bayesian classifier for GLib 8 | Version: @VERSION@ 9 | Libs: -L${libdir} -lbayes-glib-1.0 10 | Cflags: -I${includedir}/bayes-glib-1.0 11 | Requires: gobject-2.0 gio-2.0 json-glib-1.0 12 | -------------------------------------------------------------------------------- /tests/test-bayes-guess.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void 5 | test1 (void) 6 | { 7 | BayesGuess *guess; 8 | 9 | guess = bayes_guess_new ("english", 0.4567); 10 | g_assert_cmpstr ("english", ==, bayes_guess_get_name(guess)); 11 | g_assert (0.4567 == bayes_guess_get_probability(guess)); 12 | bayes_guess_unref (guess); 13 | } 14 | 15 | gint 16 | main (gint argc, 17 | gchar *argv[]) 18 | { 19 | g_test_init (&argc, &argv, NULL); 20 | g_test_add_func ("/Guess/basic", test1); 21 | return g_test_run (); 22 | } 23 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | TESTS = 2 | 3 | 4 | test_cflags = \ 5 | $(BAYES_GLIB_CFLAGS) \ 6 | -I$(top_srcdir)/src \ 7 | -I$(top_builddir)/src 8 | 9 | test_libs = \ 10 | $(BAYES_GLIB_LIBS) \ 11 | $(top_builddir)/src/libbayes-glib-1.0.la \ 12 | -lm 13 | 14 | 15 | TESTS += test-bayes-guess 16 | test_bayes_guess_SOURCES = test-bayes-guess.c 17 | test_bayes_guess_CFLAGS = $(test_cflags) 18 | test_bayes_guess_LDADD = $(test_libs) 19 | 20 | 21 | TESTS += test-bayes-storage-memory 22 | test_bayes_storage_memory_SOURCES = test-bayes-storage-memory.c 23 | test_bayes_storage_memory_CFLAGS = $(test_cflags) 24 | test_bayes_storage_memory_LDADD = $(test_libs) 25 | 26 | 27 | noinst_PROGRAMS = $(TESTS) 28 | 29 | 30 | -include $(top_srcdir)/git.mk 31 | -------------------------------------------------------------------------------- /m4/ltversion.m4: -------------------------------------------------------------------------------- 1 | # ltversion.m4 -- version numbers -*- Autoconf -*- 2 | # 3 | # Copyright (C) 2004, 2011-2015 Free Software Foundation, Inc. 4 | # Written by Scott James Remnant, 2004 5 | # 6 | # This file is free software; the Free Software Foundation gives 7 | # unlimited permission to copy and/or distribute it, with or without 8 | # modifications, as long as this notice is preserved. 9 | 10 | # @configure_input@ 11 | 12 | # serial 4179 ltversion.m4 13 | # This file is part of GNU Libtool 14 | 15 | m4_define([LT_PACKAGE_VERSION], [2.4.6]) 16 | m4_define([LT_PACKAGE_REVISION], [2.4.6]) 17 | 18 | AC_DEFUN([LTVERSION_VERSION], 19 | [macro_version='2.4.6' 20 | macro_revision='2.4.6' 21 | _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) 22 | _LT_DECL(, macro_revision, 0) 23 | ]) 24 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Run this to generate all the initial makefiles, etc. 4 | 5 | srcdir=`dirname $0` 6 | test -z "$srcdir" && srcdir=. 7 | 8 | (test -f $srcdir/configure.ac) || { 9 | echo -n "**Error**: Directory "\`$srcdir\'" does not look like the" 10 | echo " top-level bayes-glib directory" 11 | exit 1 12 | } 13 | 14 | datadir=$srcdir/data 15 | test -d "$datadir" || mkdir -p $datadir 16 | 17 | libdir=$srcdir/libs 18 | test -d "$libdir" || mkdir -p $libdir 19 | 20 | touch ChangeLog 21 | touch INSTALL 22 | 23 | aclocal --install -I m4 || exit 1 24 | gtkdocize || exit 1 25 | autoreconf --force --install -Wno-portability || exit 1 26 | 27 | if [ "$NOCONFIGURE" = "" ]; then 28 | $srcdir/configure "$@" || exit 1 29 | 30 | if [ "$1" = "--help" ]; then exit 0 else 31 | echo "Now type \`make\' to compile" || exit 1 32 | fi 33 | else 34 | echo "Skipping configure process." 35 | fi 36 | 37 | set +x 38 | -------------------------------------------------------------------------------- /tools/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS = bayes-classifier bayes-trainer 2 | 3 | bayes_classifier_SOURCES = classifier.vala 4 | 5 | bayes_classifier_CFLAGS = \ 6 | -Wno-incompatible-pointer-types \ 7 | $(BAYES_GLIB_CFLAGS) \ 8 | -I$(top_srcdir)/src 9 | 10 | bayes_classifier_LDADD = \ 11 | $(BAYES_GLIB_LIBS) \ 12 | $(top_builddir)/src/libbayes-glib-1.0.la 13 | -lm 14 | 15 | bayes_classifier_VALAFLAGS = \ 16 | --vapidir $(top_builddir)/src \ 17 | --pkg bayes-glib-1.0 \ 18 | --pkg json-glib-1.0 19 | 20 | 21 | bayes_trainer_SOURCES = trainer.vala 22 | 23 | bayes_trainer_CFLAGS = \ 24 | -Wno-incompatible-pointer-types \ 25 | $(BAYES_GLIB_CFLAGS) \ 26 | -I$(top_srcdir)/src 27 | 28 | bayes_trainer_LDADD = \ 29 | $(BAYES_GLIB_LIBS) \ 30 | $(top_builddir)/src/libbayes-glib-1.0.la 31 | -lm 32 | 33 | bayes_trainer_VALAFLAGS = \ 34 | --vapidir $(top_builddir)/src \ 35 | --pkg bayes-glib-1.0 \ 36 | --pkg json-glib-1.0 37 | 38 | 39 | CLEANFILES = *.h *.c *.stamp 40 | 41 | -include $(top_srcdir)/git.mk 42 | -------------------------------------------------------------------------------- /tests/test-bayes-storage-memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static void 4 | test1 (void) 5 | { 6 | g_autoptr(BayesStorage) storage = NULL; 7 | BayesStorageMemory *storage_memory; 8 | 9 | storage_memory = bayes_storage_memory_new (); 10 | storage = BAYES_STORAGE (storage_memory); 11 | bayes_storage_add_token (storage, "english", "turbo"); 12 | bayes_storage_add_token (storage, "english", "brakes"); 13 | bayes_storage_add_token (storage, "english", "suspension"); 14 | g_assert_cmpint (1, ==, bayes_storage_get_token_count (storage, "english", "turbo")); 15 | g_assert_cmpint (1, ==, bayes_storage_get_token_count (storage, "english", "brakes")); 16 | g_assert_cmpint (1, ==, bayes_storage_get_token_count (storage, "english", "suspension")); 17 | g_assert_cmpint (0, ==, bayes_storage_get_token_count (storage, "english", "cops")); 18 | } 19 | 20 | gint 21 | main (gint argc, 22 | gchar *argv[]) 23 | { 24 | g_test_init (&argc, &argv, NULL); 25 | g_test_add_func ("/Storage/Memory/basic_tests", test1); 26 | return g_test_run (); 27 | } 28 | -------------------------------------------------------------------------------- /src/bayes-glib.h: -------------------------------------------------------------------------------- 1 | /* bayes-glib.h 2 | * 3 | * Copyright (C) 2012-2016 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_GLIB_H 20 | #define BAYES_GLIB_H 21 | 22 | #define BAYES_GLIB_INSIDE 1 23 | #include "bayes-classifier.h" 24 | #include "bayes-guess.h" 25 | #include "bayes-storage.h" 26 | #include "bayes-storage-memory.h" 27 | #include "bayes-tokenizer.h" 28 | #undef BAYES_GLIB_INSIDE 29 | 30 | #endif /* BAYES_GLIB_H */ 31 | -------------------------------------------------------------------------------- /src/bayes-storage-memory-private.h: -------------------------------------------------------------------------------- 1 | /* bayes-storage-memory-private.h 2 | * 3 | * Copyright (C) 2016 Princeton Ferro 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | #ifndef BAYES_STORAGE_MEMORY_PRIVATE_H 19 | #define BAYES_STORAGE_MEMORY_PRIVATE_H 20 | 21 | #include 22 | 23 | G_BEGIN_DECLS 24 | 25 | struct _BayesTokens 26 | { 27 | /*< private >*/ 28 | GHashTable *tokens; 29 | guint count; 30 | }; 31 | 32 | G_END_DECLS 33 | 34 | #endif /* BAYES_STORAGE_MEMORY_PRIVATE_H */ 35 | -------------------------------------------------------------------------------- /src/bayes-guess-private.h: -------------------------------------------------------------------------------- 1 | /* bayes-guess-private.h 2 | * 3 | * Copyright (C) 2015 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_GUESS_PRIVATE_H 20 | #define BAYES_GUESS_PRIVATE_H 21 | 22 | #include 23 | 24 | G_BEGIN_DECLS 25 | 26 | struct _BayesGuess 27 | { 28 | /*< private >*/ 29 | volatile gint ref_count; 30 | gdouble probability; 31 | gchar *name; 32 | }; 33 | 34 | G_END_DECLS 35 | 36 | #endif /* BAYES_GUESS_PRIVATE_H */ 37 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = m4 data src tests doc tools 2 | EXTRA_DIST = AUTHORS 3 | 4 | 5 | AUTHORS: 6 | @if test -d "$(srcdir)/.git"; \ 7 | then \ 8 | echo Creating $@ && \ 9 | ( cd "$(top_srcdir)" && \ 10 | echo '# Generated by Makefile. Do not edit.'; echo; \ 11 | git log --no-merges --pretty=format:"%an" $(SUBDIRS) \ 12 | | sort | uniq ) > $@.tmp \ 13 | && mv -f $@.tmp $@ \ 14 | || ( rm -f $@.tmp ; \ 15 | echo Failed to generate $@ >&2 ); \ 16 | fi 17 | 18 | dist-hook: 19 | @if test -d "$(srcdir)/.git"; \ 20 | then \ 21 | echo Creating ChangeLog && \ 22 | ( cd "$(top_srcdir)" && \ 23 | echo '# Generated by Makefile. Do not edit.'; echo; \ 24 | $(top_srcdir)/missing --run git log --stat ) > ChangeLog.tmp \ 25 | && mv -f ChangeLog.tmp $(top_distdir)/ChangeLog \ 26 | || ( rm -f ChangeLog.tmp ; \ 27 | echo Failed to generate ChangeLog >&2 ); \ 28 | else \ 29 | echo A git clone is required to generate a ChangeLog >&2; \ 30 | fi 31 | 32 | 33 | .PHONY: AUTHORS 34 | 35 | 36 | GITIGNOREFILES = \ 37 | **/*.swp \ 38 | *.o \ 39 | aclocal.m4 \ 40 | build-aux \ 41 | ChangeLog \ 42 | config \ 43 | config.h.in \ 44 | gtk-doc.m4 \ 45 | gtk-doc.make \ 46 | INSTALL \ 47 | $(NULL) 48 | 49 | 50 | -include $(top_srcdir)/git.mk 51 | -------------------------------------------------------------------------------- /m4/nls.m4: -------------------------------------------------------------------------------- 1 | # nls.m4 serial 5 (gettext-0.18) 2 | dnl Copyright (C) 1995-2003, 2005-2006, 2008-2014 Free Software Foundation, 3 | dnl Inc. 4 | dnl This file is free software; the Free Software Foundation 5 | dnl gives unlimited permission to copy and/or distribute it, 6 | dnl with or without modifications, as long as this notice is preserved. 7 | dnl 8 | dnl This file can be used in projects which are not available under 9 | dnl the GNU General Public License or the GNU Library General Public 10 | dnl License but which still want to provide support for the GNU gettext 11 | dnl functionality. 12 | dnl Please note that the actual code of the GNU gettext library is covered 13 | dnl by the GNU Library General Public License, and the rest of the GNU 14 | dnl gettext package is covered by the GNU General Public License. 15 | dnl They are *not* in the public domain. 16 | 17 | dnl Authors: 18 | dnl Ulrich Drepper , 1995-2000. 19 | dnl Bruno Haible , 2000-2003. 20 | 21 | AC_PREREQ([2.50]) 22 | 23 | AC_DEFUN([AM_NLS], 24 | [ 25 | AC_MSG_CHECKING([whether NLS is requested]) 26 | dnl Default is enabled NLS 27 | AC_ARG_ENABLE([nls], 28 | [ --disable-nls do not use Native Language Support], 29 | USE_NLS=$enableval, USE_NLS=yes) 30 | AC_MSG_RESULT([$USE_NLS]) 31 | AC_SUBST([USE_NLS]) 32 | ]) 33 | -------------------------------------------------------------------------------- /m4/ax_require_defined.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_require_defined.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_REQUIRE_DEFINED(MACRO) 8 | # 9 | # DESCRIPTION 10 | # 11 | # AX_REQUIRE_DEFINED is a simple helper for making sure other macros have 12 | # been defined and thus are available for use. This avoids random issues 13 | # where a macro isn't expanded. Instead the configure script emits a 14 | # non-fatal: 15 | # 16 | # ./configure: line 1673: AX_CFLAGS_WARN_ALL: command not found 17 | # 18 | # It's like AC_REQUIRE except it doesn't expand the required macro. 19 | # 20 | # Here's an example: 21 | # 22 | # AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG]) 23 | # 24 | # LICENSE 25 | # 26 | # Copyright (c) 2014 Mike Frysinger 27 | # 28 | # Copying and distribution of this file, with or without modification, are 29 | # permitted in any medium without royalty provided the copyright notice 30 | # and this notice are preserved. This file is offered as-is, without any 31 | # warranty. 32 | 33 | #serial 1 34 | 35 | AC_DEFUN([AX_REQUIRE_DEFINED], [dnl 36 | m4_ifndef([$1], [m4_fatal([macro ]$1[ is not defined; is a m4 file missing?])]) 37 | ])dnl AX_REQUIRE_DEFINED 38 | -------------------------------------------------------------------------------- /doc/Makefile.am: -------------------------------------------------------------------------------- 1 | ## Process this file with automake to produce Makefile.in 2 | 3 | NULL = 4 | 5 | AUTOMAKE_OPTIONS = 1.6 6 | 7 | # The name of the module. 8 | DOC_MODULE=bayes-glib 9 | 10 | # The top-level SGML file. 11 | DOC_MAIN_SGML_FILE=bayes-glib-docs.sgml 12 | 13 | # Extra options to supply to gtkdoc-scan 14 | SCAN_OPTIONS= 15 | 16 | # Extra options to pass to gtkdoc-scangobj 17 | SCANGOBJ_OPTIONS= 18 | 19 | # The directory containing the source code. Relative to $(srcdir) 20 | DOC_SOURCE_DIR=$(top_srcdir)/src 21 | 22 | # Used for dependencies 23 | HFILE_GLOB=$(top_srcdir)/src/bayes-*.h 24 | CFILE_GLOB=$(top_srcdir)/src/bayes-*.c 25 | 26 | # Header files to ignore when scanning 27 | IGNORE_HFILES=$(top_srcdir)/bayes-glib/bayes-glib.h 28 | 29 | # CFLAGS and LDFLAGS for compiling scan program. Only needed 30 | # if $(DOC_MODULE).types is non-empty. 31 | AM_CPPFLAGS = \ 32 | -I$(top_srcdir)/src \ 33 | -I$(top_builddir)/src 34 | 35 | GTKDOC_LIBS = -L$(top_builddir)/src -lbayes-glib-1.0 36 | 37 | # Extra options to supply to gtkdoc-mkdb 38 | MKDB_OPTIONS=--sgml-mode --output-format=xml 39 | 40 | # Extra SGML files that are included by $(DOC_MAIN_SGML_FILE) 41 | content_files = version.xml 42 | 43 | # Images to copy into HTML directory 44 | HTML_IMAGES = 45 | 46 | # Extra options to supply to gtkdoc-fixref 47 | FIXXREF_OPTIONS = \ 48 | --extra-dir=$(GLIB_PREFIX)/share/gtk-doc/html/gobject \ 49 | --extra-dir=$(GLIB_PREFIX)/share/gtk-doc/html/gio \ 50 | --extra-dir=$(GLIB_PREFIX)/share/gtk-doc/html/glib 51 | 52 | include $(top_srcdir)/gtk-doc.make 53 | 54 | # Other files to distribute 55 | EXTRA_DIST += version.xml.in 56 | 57 | -include $(top_srcdir)/git.mk 58 | -------------------------------------------------------------------------------- /doc/bayes-glib-sections.txt: -------------------------------------------------------------------------------- 1 |
2 | bayes-classifier 3 | BAYES_TYPE_CLASSIFIER 4 | bayes_classifier_get_storage 5 | bayes_classifier_guess 6 | bayes_classifier_new 7 | bayes_classifier_set_storage 8 | bayes_classifier_set_tokenizer 9 | bayes_classifier_train 10 | BayesClassifier 11 |
12 | 13 |
14 | bayes-glib 15 | BAYES_GLIB_INSIDE 16 |
17 | 18 |
19 | bayes-guess 20 | bayes_guess_get_name 21 | bayes_guess_get_probability 22 | bayes_guess_new 23 | bayes_guess_ref 24 | bayes_guess_unref 25 | 26 | BAYES_TYPE_GUESS 27 | BayesGuess 28 | bayes_guess_get_type 29 |
30 | 31 |
32 | bayes-guess-private 33 | BayesGuess 34 |
35 | 36 |
37 | bayes-storage 38 | BayesStorage 39 | BAYES_TYPE_STORAGE 40 | BayesStorageInterface 41 | bayes_storage_add_token 42 | bayes_storage_add_token_count 43 | bayes_storage_get_names 44 | bayes_storage_get_token_count 45 | bayes_storage_get_token_probability 46 | BayesStorage 47 |
48 | 49 |
50 | bayes-storage-memory 51 | BAYES_TYPE_STORAGE_MEMORY 52 | BAYES_TYPE_TOKENS 53 | bayes_storage_memory_new 54 | bayes_storage_memory_new_from_file 55 | bayes_storage_memory_new_from_stream 56 | bayes_storage_memory_save_to_file 57 | BayesStorageMemory 58 | BayesTokens 59 |
60 | 61 |
62 | bayes-tokenizer 63 | BayesTokenizer 64 | bayes_tokenizer_word 65 | bayes_tokenizer_code_tokens 66 |
67 | 68 |
69 | bayes-version 70 | BAYES_MAJOR_VERSION 71 | BAYES_MINOR_VERSION 72 | BAYES_MICRO_VERSION 73 | BAYES_VERSION 74 | BAYES_VERSION_S 75 | BAYES_ENCODE_VERSION 76 | BAYES_VERSION_HEX 77 | BAYES_CHECK_VERSION 78 |
79 | 80 | -------------------------------------------------------------------------------- /src/bayes-version.h.in: -------------------------------------------------------------------------------- 1 | /* license */ 2 | 3 | #ifndef BAYES_GLIB_VERSION_H 4 | #define BAYES_GLIB_VERSION_H 5 | 6 | #if !defined(BAYES_GLIB_INSIDE) && !defined(BAYES_GLIB_COMPILATION) 7 | # error "Only can be included directly." 8 | #endif 9 | 10 | /** 11 | * SECTION:bayes-version 12 | * @short_description: bayes-glib version checking 13 | * 14 | * bayes-glib provides macros to check the version of the library 15 | * at compile-time 16 | */ 17 | 18 | /** 19 | * BAYES_MAJOR_VERSION: 20 | * 21 | * bayes-glib major version component (e.g. 1 if %BAYES_VERSION is 1.2.3) 22 | */ 23 | #define BAYES_MAJOR_VERSION (@MAJOR_VERSION@) 24 | 25 | /** 26 | * BAYES_MINOR_VERSION: 27 | * 28 | * bayes-glib minor version component (e.g. 2 if %BAYES_VERSION is 1.2.3) 29 | */ 30 | #define BAYES_MINOR_VERSION (@MINOR_VERSION@) 31 | 32 | /** 33 | * BAYES_MICRO_VERSION: 34 | * 35 | * bayes-glib micro version component (e.g. 3 if %BAYES_VERSION is 1.2.3) 36 | */ 37 | #define BAYES_MICRO_VERSION (@MICRO_VERSION@) 38 | 39 | /** 40 | * BAYES_VERSION 41 | * 42 | * bayes-glib version. 43 | */ 44 | #define BAYES_VERSION (@VERSION@) 45 | 46 | /** 47 | * BAYES_VERSION_S: 48 | * 49 | * bayes-glib version, encoded as a string, useful for printing and 50 | * concatenation. 51 | */ 52 | #define BAYES_VERSION_S "@VERSION@" 53 | 54 | #define BAYES_ENCODE_VERSION(major,minor,micro) \ 55 | ((major) << 24 | (minor) << 16 | (micro) << 8) 56 | 57 | /** 58 | * BAYES_VERSION_HEX: 59 | * 60 | * bayes-glib version, encoded as an hexadecimal number, useful for 61 | * integer comparisons. 62 | */ 63 | #define BAYES_VERSION_HEX \ 64 | (BAYES_ENCODE_VERSION (BAYES_MAJOR_VERSION, BAYES_MINOR_VERSION, BAYES_MICRO_VERSION)) 65 | 66 | /** 67 | * BAYES_CHECK_VERSION: 68 | * @major: required major version 69 | * @minor: required minor version 70 | * @micro: required micro version 71 | * 72 | * Compile-time version checking. Evaluates to %TRUE if the version 73 | * of bayes-glib is greater than the required one. 74 | */ 75 | #define BAYES_CHECK_VERSION(major,minor,micro) \ 76 | (BAYES_MAJOR_VERSION > (major) || \ 77 | (BAYES_MAJOR_VERSION == (major) && BAYES_MINOR_VERSION > (minor)) || \ 78 | (BAYES_MAJOR_VERSION == (major) && BAYES_MINOR_VERSION == (minor) && \ 79 | BAYES_MICRO_VERSION >= (micro))) 80 | 81 | #endif /* BAYES_GLIB_VERSION_H */ 82 | -------------------------------------------------------------------------------- /src/bayes-guess.h: -------------------------------------------------------------------------------- 1 | /* bayes-guess.h 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_GUESS_H 20 | #define BAYES_GUESS_H 21 | 22 | #include 23 | 24 | G_BEGIN_DECLS 25 | 26 | #define BAYES_TYPE_GUESS (bayes_guess_get_type()) 27 | 28 | typedef struct _BayesGuess BayesGuess; 29 | 30 | /** 31 | * bayes_guess_get_name: 32 | * @guess: (in): A #BayesGuess. 33 | * 34 | * Retrieves the classification name for which this guess represents. 35 | * 36 | * Returns: A string which should not be modified or freed. 37 | */ 38 | const gchar *bayes_guess_get_name (BayesGuess *guess); 39 | 40 | /** 41 | * bayes_guess_get_probability: 42 | * @guess: (in): A #BayesGuess. 43 | * 44 | * Retrieves the probability that the input data matches the classification 45 | * this guess represents. 46 | * 47 | * Returns: A #gdouble between 0.0 and 1.0. 48 | */ 49 | gdouble bayes_guess_get_probability (BayesGuess *guess); 50 | GType bayes_guess_get_type (void); 51 | 52 | /** 53 | * bayes_guess_new: 54 | * @name: (in): The name of the classification. 55 | * @probability: (in): The probability of the classification. 56 | * 57 | * Creates a new #BayesGuess that is the probability of a given 58 | * classification. 59 | * 60 | * Returns: (transfer full): A newly allocated #BayesGuess. 61 | */ 62 | BayesGuess *bayes_guess_new (const gchar *name, 63 | gdouble probability); 64 | BayesGuess *bayes_guess_ref (BayesGuess *guess); 65 | void bayes_guess_unref (BayesGuess *guess); 66 | 67 | G_DEFINE_AUTOPTR_CLEANUP_FUNC (BayesGuess, bayes_guess_unref) 68 | 69 | G_END_DECLS 70 | 71 | #endif /* BAYES_GUESS_H */ 72 | 73 | -------------------------------------------------------------------------------- /doc/bayes-glib-docs.sgml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | ]> 8 | 9 | 10 | Bayes Reference Manual 11 | Version &version; 12 | 13 | 2012 14 | Christian Hergert 15 | 16 | 17 | 18 | Permission is granted to copy, distribute and/or modify this 19 | document under the terms of the GNU Free 20 | Documentation License, Version 1.1 or any later 21 | version published by the Free Software Foundation with no 22 | Invariant Sections, no Front-Cover Texts, and no Back-Cover 23 | Texts. You may obtain a copy of the GNU Free 24 | Documentation License from the Free Software 25 | Foundation by visiting their Web site or by writing 27 | to: 28 | 29 |
30 | The Free Software Foundation, Inc., 31 | 59 Temple Place - Suite 330, 32 | Boston, MA 02111-1307, 33 | USA 34 |
35 |
36 |
37 |
38 | 39 | 40 | Bayes API Reference 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | API Index 51 | 52 | 53 | 54 | Index of deprecated API 55 | 56 | 57 | 58 | Object Hierarchy 59 | 60 | 61 | 62 |
63 | -------------------------------------------------------------------------------- /src/bayes-tokenizer.h: -------------------------------------------------------------------------------- 1 | /* bayes-tokenizer.h 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_TOKENIZER_H 20 | #define BAYES_TOKENIZER_H 21 | 22 | #include 23 | 24 | G_BEGIN_DECLS 25 | 26 | /** 27 | * BayesTokenizer: 28 | * @text: (in): The text to tokenize. 29 | * @user_data: (closure): User data provided during registration. 30 | * 31 | * #BayesTokenizer is a callback that can be used to tokenize 32 | * a piece of text into individual tokens. This is used by a 33 | * #BayesClassifier to convert the input text into a form that 34 | * may be used when training or guessing probabilities. 35 | * 36 | * The caller is responsible for freeing the result with 37 | * g_strfreev(). 38 | * 39 | * Returns: (array zero-terminated=1) (transfer full): 40 | * A newly allocated, null-terminated array of strings. 41 | */ 42 | typedef gchar **(*BayesTokenizer) (const gchar *text, 43 | gpointer user_data); 44 | 45 | /** 46 | * bayes_tokenizer_word: 47 | * @text: (in): A string of text to tokenize. 48 | * @user_data: (skip): Unused. 49 | * 50 | * Standard tokenizer for input text that tries to split the text 51 | * based on whitespace. This uses a simple regex "\w+". 52 | * 53 | * Returns: (array zero-terminated=1) (transfer full): 54 | * A newly allocated, null-terminated array of strings. 55 | */ 56 | gchar **bayes_tokenizer_word (const gchar *text, 57 | gpointer user_data); 58 | 59 | /** 60 | * bayes_tokenizer_code_tokens: 61 | * @text: (in): A string of text to tokenize. 62 | * @user_data: (skip): Unused. 63 | * 64 | * Tokenizer for source code input that analyzes words 65 | * and operators. 66 | * 67 | * Returns: (array zero-terminated=1) (transfer full): 68 | * A newly allocated, null-terminated array of strings. 69 | */ 70 | gchar **bayes_tokenizer_code_tokens (const gchar *text, 71 | gpointer user_data); 72 | 73 | G_END_DECLS 74 | 75 | #endif /* BAYES_TOKENIZER_H */ 76 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES = libbayes-glib-1.0.la 2 | noinst_LIBRARIES = libbayes-glib-1.0.a 3 | 4 | pkgincludedir = $(includedir)/bayes-glib-1.0 5 | pkginclude_HEADERS = \ 6 | bayes-classifier.h \ 7 | bayes-glib.h \ 8 | bayes-guess.h \ 9 | bayes-storage-memory.h \ 10 | bayes-storage.h \ 11 | bayes-tokenizer.h \ 12 | bayes-version.h 13 | 14 | libbayes_glib_1_0_la_SOURCES = \ 15 | $(pkginclude_HEADERS) \ 16 | bayes-classifier.c \ 17 | bayes-guess.c \ 18 | bayes-guess-private.h \ 19 | bayes-storage-memory-private.h \ 20 | bayes-storage-memory.c \ 21 | bayes-storage.c \ 22 | bayes-tokenizer.c 23 | 24 | libbayes_glib_1_0_la_CFLAGS = $(BAYES_GLIB_CFLAGS) 25 | libbayes_glib_1_0_la_LIBADD = $(BAYES_GLIB_LIBS) -lm 26 | 27 | libbayes_glib_1_0_a_SOURCES = $(libbayes_glib_1_0_la_SOURCES) 28 | libbayes_glib_1_0_a_CFLAGS = $(libbayes_glib_1_0_la_CFLAGS) 29 | libbayes_glib_1_0_a_LDFLAGS = $(libbayes_glib_1_0_la_LIBADD) 30 | 31 | if HAVE_INTROSPECTION 32 | -include $(INTROSPECTION_MAKEFILE) 33 | INTROSPECTION_GIRS = 34 | INTROSPECTION_SCANNER_ARGS = --add-include-path=$(srcdir) --warn-all 35 | INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir) 36 | 37 | introspection_sources_0 = \ 38 | bayes-classifier.c \ 39 | bayes-guess.c \ 40 | bayes-storage-memory.c \ 41 | bayes-storage.c \ 42 | bayes-tokenizer.c 43 | 44 | introspection_sources = $(introspection_sources_0) 45 | introspection_sources += $(introspection_sources_0:.c=.h) 46 | introspection_sources += \ 47 | bayes-guess-private.h \ 48 | bayes-storage-memory-private.h 49 | 50 | Bayes-1.0.gir: $(INTROSPECTION_SCANNER) $(lib_LTLIBRARIES) 51 | Bayes_1_0_gir_PACKAGES = gobject-2.0 gio-2.0 glib-2.0 json-glib-1.0 52 | Bayes_1_0_gir_INCLUDES = GObject-2.0 GLib-2.0 Gio-2.0 Json-1.0 53 | Bayes_1_0_gir_CFLAGS = -I$(builddir) -I$(srcdir) 54 | Bayes_1_0_gir_SCANNERFLAGS = --c-include="bayes-glib.h" 55 | Bayes_1_0_gir_LIBS = $(lib_LTLIBRARIES) 56 | Bayes_1_0_gir_FILES = $(introspection_sources) 57 | Bayes_1_0_gir_NAMESPACE = Bayes 58 | Bayes_1_0_gir_EXPORT_PACKAGES = bayes-glib-1.0 59 | INTROSPECTION_GIRS += Bayes-1.0.gir 60 | 61 | girdir = $(datadir)/gir-1.0 62 | gir_DATA = $(INTROSPECTION_GIRS) 63 | 64 | typelibdir = $(libdir)/girepository-1.0 65 | typelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib) 66 | 67 | CLEANFILES = $(gir_DATA) $(typelib_DATA) 68 | endif 69 | 70 | if ENABLE_VAPIGEN 71 | -include $(VAPIGEN_MAKEFILE) 72 | 73 | bayes-glib-1.0.vapi: Bayes-1.0.gir 74 | 75 | VAPIGEN_VAPIS = bayes-glib-1.0.vapi 76 | 77 | bayes_glib_1_0_vapi_DEPS = \ 78 | gio-2.0 \ 79 | json-glib-1.0 80 | 81 | bayes_glib_1_0_vapi_METADATADIRS = $(srcdir) 82 | bayes_glib_1_0_vapi_FILES = Bayes-1.0.gir 83 | 84 | bayes-glib-1.0.deps: Makefile 85 | $(AM_V_GEN) echo $(bayes_glib_1_0_vapi_DEPS) | tr ' ' '\n' > $@ 86 | 87 | vapidir = $(datadir)/vapi 88 | vapi_DATA = $(VAPIGEN_VAPIS) $(VAPIGEN_VAPIS:.vapi=.deps) 89 | 90 | EXTRA_DIST = bayes-glib-1.0.deps 91 | 92 | DISTCLEANFILES = $(vapi_DATA) 93 | endif 94 | 95 | -include $(top_srcdir)/git.mk 96 | -------------------------------------------------------------------------------- /m4/ax_append_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_append_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_APPEND_FLAG(FLAG, [FLAGS-VARIABLE]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # FLAG is appended to the FLAGS-VARIABLE shell variable, with a space 12 | # added in between. 13 | # 14 | # If FLAGS-VARIABLE is not specified, the current language's flags (e.g. 15 | # CFLAGS) is used. FLAGS-VARIABLE is not changed if it already contains 16 | # FLAG. If FLAGS-VARIABLE is unset in the shell, it is set to exactly 17 | # FLAG. 18 | # 19 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. 20 | # 21 | # LICENSE 22 | # 23 | # Copyright (c) 2008 Guido U. Draheim 24 | # Copyright (c) 2011 Maarten Bosmans 25 | # 26 | # This program is free software: you can redistribute it and/or modify it 27 | # under the terms of the GNU General Public License as published by the 28 | # Free Software Foundation, either version 3 of the License, or (at your 29 | # option) any later version. 30 | # 31 | # This program is distributed in the hope that it will be useful, but 32 | # WITHOUT ANY WARRANTY; without even the implied warranty of 33 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 34 | # Public License for more details. 35 | # 36 | # You should have received a copy of the GNU General Public License along 37 | # with this program. If not, see . 38 | # 39 | # As a special exception, the respective Autoconf Macro's copyright owner 40 | # gives unlimited permission to copy, distribute and modify the configure 41 | # scripts that are the output of Autoconf when processing the Macro. You 42 | # need not follow the terms of the GNU General Public License when using 43 | # or distributing such scripts, even though portions of the text of the 44 | # Macro appear in them. The GNU General Public License (GPL) does govern 45 | # all other use of the material that constitutes the Autoconf Macro. 46 | # 47 | # This special exception to the GPL applies to versions of the Autoconf 48 | # Macro released by the Autoconf Archive. When you make and distribute a 49 | # modified version of the Autoconf Macro, you may extend this special 50 | # exception to the GPL to apply to your modified version as well. 51 | 52 | #serial 6 53 | 54 | AC_DEFUN([AX_APPEND_FLAG], 55 | [dnl 56 | AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_SET_IF 57 | AS_VAR_PUSHDEF([FLAGS], [m4_default($2,_AC_LANG_PREFIX[FLAGS])]) 58 | AS_VAR_SET_IF(FLAGS,[ 59 | AS_CASE([" AS_VAR_GET(FLAGS) "], 60 | [*" $1 "*], [AC_RUN_LOG([: FLAGS already contains $1])], 61 | [ 62 | AS_VAR_APPEND(FLAGS,[" $1"]) 63 | AC_RUN_LOG([: FLAGS="$FLAGS"]) 64 | ]) 65 | ], 66 | [ 67 | AS_VAR_SET(FLAGS,[$1]) 68 | AC_RUN_LOG([: FLAGS="$FLAGS"]) 69 | ]) 70 | AS_VAR_POPDEF([FLAGS])dnl 71 | ])dnl AX_APPEND_FLAG 72 | -------------------------------------------------------------------------------- /m4/ax_append_compile_flags.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_append_compile_flags.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_APPEND_COMPILE_FLAGS([FLAG1 FLAG2 ...], [FLAGS-VARIABLE], [EXTRA-FLAGS]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # For every FLAG1, FLAG2 it is checked whether the compiler works with the 12 | # flag. If it does, the flag is added FLAGS-VARIABLE 13 | # 14 | # If FLAGS-VARIABLE is not specified, the current language's flags (e.g. 15 | # CFLAGS) is used. During the check the flag is always added to the 16 | # current language's flags. 17 | # 18 | # If EXTRA-FLAGS is defined, it is added to the current language's default 19 | # flags (e.g. CFLAGS) when the check is done. The check is thus made with 20 | # the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to 21 | # force the compiler to issue an error when a bad flag is given. 22 | # 23 | # NOTE: This macro depends on the AX_APPEND_FLAG and 24 | # AX_CHECK_COMPILE_FLAG. Please keep this macro in sync with 25 | # AX_APPEND_LINK_FLAGS. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2011 Maarten Bosmans 30 | # 31 | # This program is free software: you can redistribute it and/or modify it 32 | # under the terms of the GNU General Public License as published by the 33 | # Free Software Foundation, either version 3 of the License, or (at your 34 | # option) any later version. 35 | # 36 | # This program is distributed in the hope that it will be useful, but 37 | # WITHOUT ANY WARRANTY; without even the implied warranty of 38 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 39 | # Public License for more details. 40 | # 41 | # You should have received a copy of the GNU General Public License along 42 | # with this program. If not, see . 43 | # 44 | # As a special exception, the respective Autoconf Macro's copyright owner 45 | # gives unlimited permission to copy, distribute and modify the configure 46 | # scripts that are the output of Autoconf when processing the Macro. You 47 | # need not follow the terms of the GNU General Public License when using 48 | # or distributing such scripts, even though portions of the text of the 49 | # Macro appear in them. The GNU General Public License (GPL) does govern 50 | # all other use of the material that constitutes the Autoconf Macro. 51 | # 52 | # This special exception to the GPL applies to versions of the Autoconf 53 | # Macro released by the Autoconf Archive. When you make and distribute a 54 | # modified version of the Autoconf Macro, you may extend this special 55 | # exception to the GPL to apply to your modified version as well. 56 | 57 | #serial 4 58 | 59 | AC_DEFUN([AX_APPEND_COMPILE_FLAGS], 60 | [AX_REQUIRE_DEFINED([AX_CHECK_COMPILE_FLAG]) 61 | AX_REQUIRE_DEFINED([AX_APPEND_FLAG]) 62 | for flag in $1; do 63 | AX_CHECK_COMPILE_FLAG([$flag], [AX_APPEND_FLAG([$flag], [$2])], [], [$3]) 64 | done 65 | ])dnl AX_APPEND_COMPILE_FLAGS 66 | -------------------------------------------------------------------------------- /tools/classifier.vala: -------------------------------------------------------------------------------- 1 | // valac {BayesStorageSerializable,classifier}.vala -g --enable-experimental --pkg Bayes-1.0 --pkg gio-2.0 --pkg json-glib-1.0 -o classifier 2 | 3 | public class SourceCodeClassifier { 4 | static Bayes.Classifier classifier; 5 | 6 | static GLib.List guess_file (string filename) { 7 | File file = File.new_for_path (filename); 8 | uint8[] buf; 9 | string etag; 10 | 11 | try { 12 | file.load_contents (null, out buf, out etag); 13 | return classifier.guess ((string) buf); 14 | } catch (Error e) { 15 | error (e.message); 16 | } 17 | } 18 | 19 | static void guess_test (string filename) { 20 | var guesses = guess_file (filename); 21 | foreach (Bayes.Guess guess in guesses) 22 | stdout.printf (" %s (%f)\n", guess.get_name (), guess.get_probability ()); 23 | } 24 | 25 | private static string? training_file = null; 26 | [CCode (array_length = false, array_null_terminated = true)] 27 | private static string[]? files = null; 28 | private static string? serialize_file = null; 29 | private static string? tokenizer = "word"; 30 | 31 | private const OptionEntry[] options = { 32 | // --training-file 33 | { "training-file", 't', 0, OptionArg.FILENAME, ref training_file, "Training data file", "FILE"}, 34 | // --files 35 | { "files", 'i', 0, OptionArg.FILENAME_ARRAY, ref files, "Files to classify", "FILE..." }, 36 | // --reserialize 37 | { "reserialize", 'o', 0, OptionArg.FILENAME, ref serialize_file, "File to re-serialize training data to", "FILE"}, 38 | // --tokenizer 39 | { "tokenizer", 0, 0, OptionArg.STRING, ref tokenizer, "Tokenizer function", "'word' | 'code_tokens'"}, 40 | { null } 41 | }; 42 | 43 | // classify source code files 44 | static int main (string[] args) { 45 | try { 46 | var opt_context = new OptionContext ("- Classify Source Code"); 47 | opt_context.add_main_entries (options, null); 48 | opt_context.set_help_enabled (true); 49 | opt_context.parse (ref args); 50 | } catch (OptionError e) { 51 | stdout.printf ("error: %s\n", e.message); 52 | stdout.printf ("Run '%s --help' to see available command-line options.\n", args[0]); 53 | return 0; 54 | } 55 | classifier = new Bayes.Classifier (); 56 | 57 | // FIXME: bindings 58 | if (tokenizer == "word") 59 | classifier.set_tokenizer (text => { 60 | return Bayes.tokenizer_word (text, null); 61 | }); 62 | else if (tokenizer == "code_tokens") 63 | classifier.set_tokenizer (text => { 64 | return Bayes.tokenizer_code_tokens (text, null); 65 | }); 66 | 67 | // deserializing 68 | try { 69 | classifier.storage = new Bayes.StorageMemory.from_file (training_file); 70 | stdout.printf ("loaded %s\n", training_file); 71 | } catch (Error e) { 72 | error (e.message); 73 | } 74 | 75 | if (serialize_file != null) { 76 | (classifier.storage as Bayes.StorageMemory).save_to_file (serialize_file); 77 | stdout.printf ("wrote to %s\n", serialize_file); 78 | } 79 | 80 | // guessing 81 | foreach (var file in files) 82 | if (file != null) { 83 | stdout.printf ("%s\n", file); 84 | guess_test (file); 85 | } 86 | 87 | return 0; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /m4/appstream-xml.m4: -------------------------------------------------------------------------------- 1 | # appstream-xml.m4 2 | # 3 | # serial 6 4 | 5 | dnl APPSTREAM_XML 6 | dnl Installs and validates AppData XML files. 7 | dnl 8 | dnl Call APPSTREAM_XML in configure.ac to check for the appstream-util tool. 9 | dnl Add @APPSTREAM_XML_RULES@ to a Makefile.am to substitute the make rules. Add 10 | dnl .appdata.xml files to appstream_XML in Makefile.am and they will be validated 11 | dnl at make check time, if appstream-util is installed, as well as installed 12 | dnl to the correct location automatically. Add --enable-appstream-util to 13 | dnl DISTCHECK_CONFIGURE_FLAGS in Makefile.am to require valid AppData XML when 14 | dnl doing a distcheck. 15 | dnl 16 | dnl Adding files to appstream_XML does not distribute them automatically. 17 | 18 | AC_DEFUN([APPSTREAM_XML], 19 | [ 20 | m4_pattern_allow([AM_V_GEN]) 21 | AC_ARG_ENABLE([appstream-util], 22 | [AS_HELP_STRING([--disable-appstream-util], 23 | [Disable validating AppData XML files during check phase])]) 24 | 25 | AS_IF([test "x$enable_appstream_validate" != "xno"], 26 | [AC_PATH_PROG([APPSTREAM_UTIL], [appstream-util]) 27 | AS_IF([test "x$APPSTREAM_UTIL" = "x"], 28 | [have_appstream_validate=no], 29 | [have_appstream_validate=yes 30 | AC_SUBST([APPSTREAM_UTIL])])], 31 | [have_appstream_validate=no]) 32 | 33 | AS_IF([test "x$have_appstream_validate" != "xno"], 34 | [appstream_validate=yes], 35 | [appstream_validate=no 36 | AS_IF([test "x$enable_appstream_validate" = "xyes"], 37 | [AC_MSG_ERROR([AppData validation was requested but appstream-util was not found])])]) 38 | 39 | AC_SUBST([appstreamxmldir], [${datadir}/appdata]) 40 | 41 | APPSTREAM_XML_RULES=' 42 | .PHONY : uninstall-appstream-xml install-appstream-xml clean-appstream-xml 43 | 44 | mostlyclean-am: clean-appstream-xml 45 | 46 | %.appdata.valid: %.appdata.xml 47 | $(AM_V_GEN) if test -f "$<"; then d=; else d="$(srcdir)/"; fi; \ 48 | if test -n "$(APPSTREAM_UTIL)"; \ 49 | then $(APPSTREAM_UTIL) --nonet validate $${d}$<; fi \ 50 | && touch [$]@ 51 | 52 | check-am: $(appstream_XML:.appdata.xml=.appdata.valid) 53 | uninstall-am: uninstall-appstream-xml 54 | install-data-am: install-appstream-xml 55 | 56 | .SECONDARY: $(appstream_XML) 57 | 58 | install-appstream-xml: $(appstream_XML) 59 | @$(NORMAL_INSTALL) 60 | if test -n "$^"; then \ 61 | test -z "$(appstreamxmldir)" || $(MKDIR_P) "$(DESTDIR)$(appstreamxmldir)"; \ 62 | $(INSTALL_DATA) $^ "$(DESTDIR)$(appstreamxmldir)"; \ 63 | fi 64 | 65 | uninstall-appstream-xml: 66 | @$(NORMAL_UNINSTALL) 67 | @list='\''$(appstream_XML)'\''; test -n "$(appstreamxmldir)" || list=; \ 68 | files=`for p in $$list; do echo $$p; done | sed -e '\''s|^.*/||'\''`; \ 69 | test -n "$$files" || exit 0; \ 70 | echo " ( cd '\''$(DESTDIR)$(appstreamxmldir)'\'' && rm -f" $$files ")"; \ 71 | cd "$(DESTDIR)$(appstreamxmldir)" && rm -f $$files 72 | 73 | clean-appstream-xml: 74 | rm -f $(appstream_XML:.appdata.xml=.appdata.valid) 75 | ' 76 | _APPSTREAM_XML_SUBST(APPSTREAM_XML_RULES) 77 | ]) 78 | 79 | dnl _APPSTREAM_XML_SUBST(VARIABLE) 80 | dnl Abstract macro to do either _AM_SUBST_NOTMAKE or AC_SUBST 81 | AC_DEFUN([_APPSTREAM_XML_SUBST], 82 | [ 83 | AC_SUBST([$1]) 84 | m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([$1])]) 85 | ] 86 | ) 87 | -------------------------------------------------------------------------------- /m4/ax_check_link_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_link_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_LINK_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the linker or gives an error. 12 | # (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the linker's default flags 18 | # when the check is done. The check is thus made with the flags: "LDFLAGS 19 | # EXTRA-FLAGS FLAG". This can for example be used to force the linker to 20 | # issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_LINK_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,COMPILE}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_LINK_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_ldflags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether the linker accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$LDFLAGS 65 | LDFLAGS="$LDFLAGS $4 $1" 66 | AC_LINK_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | LDFLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_LINK_FLAGS 75 | -------------------------------------------------------------------------------- /src/bayes-guess.c: -------------------------------------------------------------------------------- 1 | /* bayes-guess.c 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "bayes-guess.h" 20 | #include "bayes-guess-private.h" 21 | 22 | /** 23 | * SECTION:bayes-guess 24 | * @title: BayesGuess 25 | * @short_description: An immutable structure containing a probability. 26 | * @see_also: bayes_guess_get_name() bayes_guess_get_probability() 27 | * 28 | * #BayesGuess represents a guess at the classification of given input 29 | * data. The guess also includes the probability calculated for the input 30 | * being the classification. The guess structure contains the classification 31 | * name and a probability between 0.0 and 1.0. 32 | * 33 | * The #BayesGuess structure is a reference counted #GBoxed type. You can 34 | * reference the structure with bayes_guess_ref() and free the structure 35 | * with bayes_guess_unref(). 36 | * 37 | */ 38 | 39 | G_DEFINE_BOXED_TYPE (BayesGuess, bayes_guess, bayes_guess_ref, bayes_guess_unref) 40 | 41 | BayesGuess * 42 | bayes_guess_new (const gchar *name, 43 | gdouble probability) 44 | { 45 | BayesGuess *guess; 46 | 47 | g_return_val_if_fail (name, NULL); 48 | 49 | guess = g_slice_new0 (BayesGuess); 50 | guess->ref_count = 1; 51 | guess->name = g_strdup(name); 52 | guess->probability = CLAMP(probability, 0.0, 1.0); 53 | 54 | return guess; 55 | } 56 | 57 | /** 58 | * bayes_guess_ref: 59 | * @guess: (in): A #BayesGuess. 60 | * 61 | * Increments the reference count of @guess by one. 62 | * 63 | * Returns: The instance provided, @guess. 64 | */ 65 | BayesGuess * 66 | bayes_guess_ref (BayesGuess *guess) 67 | { 68 | g_return_val_if_fail (guess != NULL, NULL); 69 | g_return_val_if_fail (guess->ref_count > 0, NULL); 70 | 71 | g_atomic_int_inc (&guess->ref_count); 72 | 73 | return guess; 74 | } 75 | 76 | /** 77 | * bayes_guess_unref: 78 | * @guess: (in): A #BayesGuess. 79 | * 80 | * Decrements the reference count of @guess by one. Once the reference count 81 | * reaches zero, the structure and allocated resources are released. 82 | */ 83 | void 84 | bayes_guess_unref (BayesGuess *guess) 85 | { 86 | g_return_if_fail (guess != NULL); 87 | g_return_if_fail (guess->ref_count > 0); 88 | 89 | if (g_atomic_int_dec_and_test (&guess->ref_count)) 90 | { 91 | g_free (guess->name); 92 | g_slice_free (BayesGuess, guess); 93 | } 94 | } 95 | 96 | const gchar * 97 | bayes_guess_get_name (BayesGuess *guess) 98 | { 99 | g_return_val_if_fail(guess, NULL); 100 | 101 | return guess->name; 102 | } 103 | 104 | gdouble 105 | bayes_guess_get_probability (BayesGuess *guess) 106 | { 107 | g_return_val_if_fail (guess, 0.0); 108 | 109 | return guess->probability; 110 | } 111 | -------------------------------------------------------------------------------- /m4/ax_check_compile_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's compiler 12 | # or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the current language's default 18 | # flags (e.g. CFLAGS) when the check is done. The check is thus made with 19 | # the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to 20 | # force the compiler to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_COMPILE_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_COMPILE_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS 65 | _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1" 66 | AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_COMPILE_FLAGS 75 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_PREREQ([2.69]) 2 | 3 | 4 | dnl *********************************************************************** 5 | dnl Define Versioning Information 6 | dnl *********************************************************************** 7 | m4_define([major_version],[0]) 8 | m4_define([minor_version],[2]) 9 | m4_define([micro_version],[2]) 10 | m4_define([package_version],[major_version.minor_version.micro_version]) 11 | m4_define([bug_report_url],[https://github.com/chergert/bayes-glib/issues]) 12 | m4_define([interface_age],[0]) 13 | 14 | 15 | dnl *********************************************************************** 16 | dnl Initialize autoconf 17 | dnl *********************************************************************** 18 | AC_INIT([bayes-glib],[package_version],[bug_report_url]) 19 | AC_CONFIG_HEADERS([config.h]) 20 | AC_CONFIG_SRCDIR([data/bayes-glib-1.0.pc.in]) 21 | AC_CONFIG_MACRO_DIR([m4]) 22 | AC_CONFIG_AUX_DIR([build-aux]) 23 | AC_SUBST(ACLOCAL_AMFLAGS, "-I m4") 24 | AC_CANONICAL_HOST 25 | 26 | 27 | dnl *********************************************************************** 28 | dnl Make version information available to autoconf files 29 | dnl *********************************************************************** 30 | MAJOR_VERSION=major_version 31 | MINOR_VERSION=minor_version 32 | MICRO_VERSION=micro_version 33 | AC_SUBST(MAJOR_VERSION) 34 | AC_SUBST(MINOR_VERSION) 35 | AC_SUBST(MICRO_VERSION) 36 | 37 | 38 | dnl *********************************************************************** 39 | dnl Initialize automake 40 | dnl *********************************************************************** 41 | AM_SILENT_RULES([yes]) 42 | AM_INIT_AUTOMAKE([1.11 foreign subdir-objects tar-ustar no-dist-gzip dist-xz]) 43 | AM_MAINTAINER_MODE([enable]) 44 | 45 | 46 | dnl *********************************************************************** 47 | dnl Check for required programs 48 | dnl *********************************************************************** 49 | AC_PROG_CC 50 | AM_PROG_VALAC 51 | AC_PROG_INSTALL 52 | AC_PATH_PROG([GLIB_GENMARSHAL],[glib-genmarshal]) 53 | AC_PATH_PROG([GLIB_MKENUMS],[glib-mkenums]) 54 | AC_PATH_PROG([GLIB_COMPILE_RESOURCES],[glib-compile-resources]) 55 | PKG_PROG_PKG_CONFIG([0.22]) 56 | GLIB_GSETTINGS 57 | GOBJECT_INTROSPECTION_CHECK([1.42.0]) 58 | VAPIGEN_CHECK 59 | GTK_DOC_CHECK 60 | 61 | 62 | dnl *********************************************************************** 63 | dnl Check for required packages 64 | dnl *********************************************************************** 65 | PKG_CHECK_MODULES(BAYES_GLIB, [gio-2.0 >= 2.46.0 66 | json-glib-1.0]) 67 | 68 | 69 | dnl *********************************************************************** 70 | dnl Initialize Libtool 71 | dnl *********************************************************************** 72 | LT_PREREQ([2.2]) 73 | LT_INIT([shared static]) 74 | 75 | 76 | dnl *********************************************************************** 77 | dnl Process .in Files 78 | dnl *********************************************************************** 79 | AC_CONFIG_FILES([ 80 | Makefile 81 | data/Makefile 82 | data/bayes-glib-1.0.pc 83 | doc/Makefile 84 | doc/version.xml 85 | m4/Makefile 86 | src/Makefile 87 | src/bayes-version.h 88 | tests/Makefile 89 | tools/Makefile 90 | ]) 91 | AC_OUTPUT 92 | 93 | 94 | echo "" 95 | echo " ${PACKAGE} - ${VERSION}" 96 | echo "" 97 | echo " Options" 98 | echo "" 99 | echo " Prefix ............................... : ${prefix}" 100 | echo " Libdir ............................... : ${libdir}" 101 | echo "" 102 | -------------------------------------------------------------------------------- /m4/gsettings.m4: -------------------------------------------------------------------------------- 1 | dnl GLIB_GSETTINGS 2 | dnl Defines GSETTINGS_SCHEMAS_INSTALL which controls whether 3 | dnl the schema should be compiled 4 | dnl 5 | 6 | AC_DEFUN([GLIB_GSETTINGS], 7 | [ 8 | m4_pattern_allow([AM_V_GEN]) 9 | AC_ARG_ENABLE(schemas-compile, 10 | AS_HELP_STRING([--disable-schemas-compile], 11 | [Disable regeneration of gschemas.compiled on install]), 12 | [case ${enableval} in 13 | yes) GSETTINGS_DISABLE_SCHEMAS_COMPILE="" ;; 14 | no) GSETTINGS_DISABLE_SCHEMAS_COMPILE="1" ;; 15 | *) AC_MSG_ERROR([bad value ${enableval} for --enable-schemas-compile]) ;; 16 | esac]) 17 | AC_SUBST([GSETTINGS_DISABLE_SCHEMAS_COMPILE]) 18 | PKG_PROG_PKG_CONFIG([0.16]) 19 | AC_SUBST(gsettingsschemadir, [${datadir}/glib-2.0/schemas]) 20 | if test x$cross_compiling != xyes; then 21 | GLIB_COMPILE_SCHEMAS=`$PKG_CONFIG --variable glib_compile_schemas gio-2.0` 22 | else 23 | AC_PATH_PROG(GLIB_COMPILE_SCHEMAS, glib-compile-schemas) 24 | fi 25 | AC_SUBST(GLIB_COMPILE_SCHEMAS) 26 | if test "x$GLIB_COMPILE_SCHEMAS" = "x"; then 27 | ifelse([$2],,[AC_MSG_ERROR([glib-compile-schemas not found.])],[$2]) 28 | else 29 | ifelse([$1],,[:],[$1]) 30 | fi 31 | 32 | GSETTINGS_RULES=' 33 | .PHONY : uninstall-gsettings-schemas install-gsettings-schemas clean-gsettings-schemas 34 | 35 | mostlyclean-am: clean-gsettings-schemas 36 | 37 | gsettings__enum_file = $(addsuffix .enums.xml,$(gsettings_ENUM_NAMESPACE)) 38 | 39 | %.gschema.valid: %.gschema.xml $(gsettings__enum_file) 40 | $(AM_V_GEN) $(GLIB_COMPILE_SCHEMAS) --strict --dry-run $(addprefix --schema-file=,$(gsettings__enum_file)) --schema-file=$< && mkdir -p [$](@D) && touch [$]@ 41 | 42 | all-am: $(gsettings_SCHEMAS:.xml=.valid) 43 | uninstall-am: uninstall-gsettings-schemas 44 | install-data-am: install-gsettings-schemas 45 | 46 | .SECONDARY: $(gsettings_SCHEMAS) 47 | 48 | install-gsettings-schemas: $(gsettings_SCHEMAS) $(gsettings__enum_file) 49 | @$(NORMAL_INSTALL) 50 | if test -n "$^"; then \ 51 | test -z "$(gsettingsschemadir)" || $(MKDIR_P) "$(DESTDIR)$(gsettingsschemadir)"; \ 52 | $(INSTALL_DATA) $^ "$(DESTDIR)$(gsettingsschemadir)"; \ 53 | test -n "$(GSETTINGS_DISABLE_SCHEMAS_COMPILE)$(DESTDIR)" || $(GLIB_COMPILE_SCHEMAS) $(gsettingsschemadir); \ 54 | fi 55 | 56 | uninstall-gsettings-schemas: 57 | @$(NORMAL_UNINSTALL) 58 | @list='\''$(gsettings_SCHEMAS) $(gsettings__enum_file)'\''; test -n "$(gsettingsschemadir)" || list=; \ 59 | files=`for p in $$list; do echo $$p; done | sed -e '\''s|^.*/||'\''`; \ 60 | test -n "$$files" || exit 0; \ 61 | echo " ( cd '\''$(DESTDIR)$(gsettingsschemadir)'\'' && rm -f" $$files ")"; \ 62 | cd "$(DESTDIR)$(gsettingsschemadir)" && rm -f $$files 63 | test -n "$(GSETTINGS_DISABLE_SCHEMAS_COMPILE)$(DESTDIR)" || $(GLIB_COMPILE_SCHEMAS) $(gsettingsschemadir) 64 | 65 | clean-gsettings-schemas: 66 | rm -f $(gsettings_SCHEMAS:.xml=.valid) $(gsettings__enum_file) 67 | 68 | ifdef gsettings_ENUM_NAMESPACE 69 | $(gsettings__enum_file): $(gsettings_ENUM_FILES) 70 | $(AM_V_GEN) glib-mkenums --comments '\'''\'' --fhead "" --vhead " <@type@ id='\''$(gsettings_ENUM_NAMESPACE).@EnumName@'\''>" --vprod " " --vtail " " --ftail "" [$]^ > [$]@.tmp && mv [$]@.tmp [$]@ 71 | endif 72 | ' 73 | _GSETTINGS_SUBST(GSETTINGS_RULES) 74 | ]) 75 | 76 | dnl _GSETTINGS_SUBST(VARIABLE) 77 | dnl Abstract macro to do either _AM_SUBST_NOTMAKE or AC_SUBST 78 | AC_DEFUN([_GSETTINGS_SUBST], 79 | [ 80 | AC_SUBST([$1]) 81 | m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([$1])]) 82 | ] 83 | ) 84 | -------------------------------------------------------------------------------- /src/bayes-storage-memory.h: -------------------------------------------------------------------------------- 1 | /* bayes-storage-memory.h 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_STORAGE_MEMORY_H 20 | #define BAYES_STORAGE_MEMORY_H 21 | 22 | #include "bayes-storage.h" 23 | #include 24 | 25 | G_BEGIN_DECLS 26 | 27 | #define BAYES_TYPE_TOKENS (bayes_tokens_get_type ()) 28 | 29 | typedef struct _BayesTokens BayesTokens; 30 | 31 | #define BAYES_TYPE_STORAGE_MEMORY (bayes_storage_memory_get_type()) 32 | 33 | G_DECLARE_FINAL_TYPE (BayesStorageMemory, bayes_storage_memory, BAYES, STORAGE_MEMORY, GObject) 34 | 35 | struct _BayesStorageMemory 36 | { 37 | GObject parent_instance; 38 | 39 | /*< private >*/ 40 | 41 | /* somehow this is needed to get proper GI types for properties */ 42 | /** 43 | * BayesStorageMemory:names: (type GLib.HashTable(utf8,Bayes.Tokens)) 44 | */ 45 | GHashTable *names; 46 | 47 | /** 48 | * BayesStorageMemory:corpus: (type Bayes.Tokens) 49 | */ 50 | BayesTokens *corpus; 51 | }; 52 | 53 | /** 54 | * bayes_storage_memory_new: 55 | * 56 | * Creates a new #BayesStorageMemory instance. 57 | * 58 | * Returns: (transfer full): A new #BayesStorageMemory 59 | */ 60 | BayesStorageMemory *bayes_storage_memory_new (void); 61 | 62 | /** 63 | * bayes_storage_memory_new_from_file: 64 | * @filename: Name of filename to load 65 | * @error: (allow-none): Return location for an error, or %NULL 66 | * 67 | * Creates a new #BayesStorageMemory instance from a file. The 68 | * file must be a serialized #BayesStorageMemory in JSON format. 69 | * 70 | * Returns: (transfer full): a new BayesStorageMemory or %NULL 71 | * if parsing failed or file could not be loaded. 72 | */ 73 | BayesStorageMemory *bayes_storage_memory_new_from_file (const gchar *filename, 74 | GError **error); 75 | 76 | /** 77 | * bayes_storage_memory_new_from_stream: 78 | * @stream: stream to load from 79 | * @cancellable: (allow-none): to cancel loading 80 | * @error: (allow-none): Return location for an error, or %NULL 81 | * 82 | * Creates a new #BayesStorageMemory instance from a stream. The 83 | * stream must be a serialized #BayesStorageMemory in JSON format. 84 | * 85 | * Returns: (transfer full): a new #BayesStorageMemory or %NULL 86 | * if parsing failed or stream could not be read from. 87 | */ 88 | BayesStorageMemory *bayes_storage_memory_new_from_stream (GInputStream *stream, 89 | GCancellable *cancellable, 90 | GError **error); 91 | 92 | /** 93 | * bayes_storage_memory_save_to_file: 94 | * @self: a #BayesStorageMemory 95 | * @filename: name of file to save 96 | * @error: (allow-none): Return location for an error, or %NULL 97 | * 98 | * Serializes a #BayesStorageMemory instance to a file 99 | * in JSON format. 100 | * 101 | * Returns: %FALSE if @error is set 102 | */ 103 | gboolean bayes_storage_memory_save_to_file (BayesStorageMemory *self, 104 | const gchar *filename, 105 | GError **error); 106 | 107 | G_END_DECLS 108 | 109 | #endif /* BAYES_STORAGE_MEMORY_H */ 110 | -------------------------------------------------------------------------------- /m4/vapigen.m4: -------------------------------------------------------------------------------- 1 | dnl vapigen.m4 2 | dnl 3 | dnl Copyright 2012 Evan Nemerson 4 | dnl 5 | dnl This library is free software; you can redistribute it and/or 6 | dnl modify it under the terms of the GNU Lesser General Public 7 | dnl License as published by the Free Software Foundation; either 8 | dnl version 2.1 of the License, or (at your option) any later version. 9 | dnl 10 | dnl This library is distributed in the hope that it will be useful, 11 | dnl but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | dnl Lesser General Public License for more details. 14 | dnl 15 | dnl You should have received a copy of the GNU Lesser General Public 16 | dnl License along with this library; if not, write to the Free Software 17 | dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | # VAPIGEN_CHECK([VERSION], [API_VERSION], [FOUND_INTROSPECTION], [DEFAULT]) 20 | # -------------------------------------- 21 | # Check vapigen existence and version 22 | # 23 | # See http://live.gnome.org/Vala/UpstreamGuide for detailed documentation 24 | AC_DEFUN([VAPIGEN_CHECK], 25 | [ 26 | AS_IF([test "x$3" != "xyes"], [ 27 | m4_provide_if([GOBJECT_INTROSPECTION_CHECK], [], [ 28 | m4_provide_if([GOBJECT_INTROSPECTION_REQUIRE], [], [ 29 | AC_MSG_ERROR([[You must call GOBJECT_INTROSPECTION_CHECK or GOBJECT_INTROSPECTION_REQUIRE before using VAPIGEN_CHECK unless using the FOUND_INTROSPECTION argument is "yes"]]) 30 | ]) 31 | ]) 32 | ]) 33 | 34 | AC_ARG_ENABLE([vala], 35 | [AS_HELP_STRING([--enable-vala[=@<:@no/auto/yes@:>@]],[build Vala bindings @<:@default=]ifelse($4,,auto,$4)[@:>@])],,[ 36 | AS_IF([test "x$4" = "x"], [ 37 | enable_vala=auto 38 | ], [ 39 | enable_vala=$4 40 | ]) 41 | ]) 42 | 43 | AS_CASE([$enable_vala], [no], [enable_vala=no], 44 | [yes], [ 45 | AS_IF([test "x$3" != "xyes" -a "x$found_introspection" != "xyes"], [ 46 | AC_MSG_ERROR([Vala bindings require GObject Introspection]) 47 | ]) 48 | ], [auto], [ 49 | AS_IF([test "x$3" != "xyes" -a "x$found_introspection" != "xyes"], [ 50 | enable_vala=no 51 | ]) 52 | ], [ 53 | AC_MSG_ERROR([Invalid argument passed to --enable-vala, should be one of @<:@no/auto/yes@:>@]) 54 | ]) 55 | 56 | AS_IF([test "x$2" = "x"], [ 57 | vapigen_pkg_name=vapigen 58 | ], [ 59 | vapigen_pkg_name=vapigen-$2 60 | ]) 61 | AS_IF([test "x$1" = "x"], [ 62 | vapigen_pkg="$vapigen_pkg_name" 63 | ], [ 64 | vapigen_pkg="$vapigen_pkg_name >= $1" 65 | ]) 66 | 67 | PKG_PROG_PKG_CONFIG 68 | 69 | PKG_CHECK_EXISTS([$vapigen_pkg], [ 70 | AS_IF([test "$enable_vala" = "auto"], [ 71 | enable_vala=yes 72 | ]) 73 | ], [ 74 | AS_CASE([$enable_vala], [yes], [ 75 | AC_MSG_ERROR([$vapigen_pkg not found]) 76 | ], [auto], [ 77 | enable_vala=no 78 | ]) 79 | ]) 80 | 81 | AC_MSG_CHECKING([for vapigen]) 82 | 83 | AS_CASE([$enable_vala], 84 | [yes], [ 85 | VAPIGEN=`$PKG_CONFIG --variable=vapigen $vapigen_pkg_name` 86 | VAPIGEN_MAKEFILE=`$PKG_CONFIG --variable=datadir $vapigen_pkg_name`/vala/Makefile.vapigen 87 | AS_IF([test "x$2" = "x"], [ 88 | VAPIGEN_VAPIDIR=`$PKG_CONFIG --variable=vapidir $vapigen_pkg_name` 89 | ], [ 90 | VAPIGEN_VAPIDIR=`$PKG_CONFIG --variable=vapidir_versioned $vapigen_pkg_name` 91 | ]) 92 | ]) 93 | 94 | AC_MSG_RESULT([$enable_vala]) 95 | 96 | AC_SUBST([VAPIGEN]) 97 | AC_SUBST([VAPIGEN_VAPIDIR]) 98 | AC_SUBST([VAPIGEN_MAKEFILE]) 99 | 100 | AM_CONDITIONAL(ENABLE_VAPIGEN, test "x$enable_vala" = "xyes") 101 | ]) 102 | -------------------------------------------------------------------------------- /m4/introspection.m4: -------------------------------------------------------------------------------- 1 | dnl -*- mode: autoconf -*- 2 | dnl Copyright 2009 Johan Dahlin 3 | dnl 4 | dnl This file is free software; the author(s) gives unlimited 5 | dnl permission to copy and/or distribute it, with or without 6 | dnl modifications, as long as this notice is preserved. 7 | dnl 8 | 9 | # serial 1 10 | 11 | m4_define([_GOBJECT_INTROSPECTION_CHECK_INTERNAL], 12 | [ 13 | AC_BEFORE([AC_PROG_LIBTOOL],[$0])dnl setup libtool first 14 | AC_BEFORE([AM_PROG_LIBTOOL],[$0])dnl setup libtool first 15 | AC_BEFORE([LT_INIT],[$0])dnl setup libtool first 16 | 17 | dnl enable/disable introspection 18 | m4_if([$2], [require], 19 | [dnl 20 | enable_introspection=yes 21 | ],[dnl 22 | AC_ARG_ENABLE(introspection, 23 | AS_HELP_STRING([--enable-introspection[=@<:@no/auto/yes@:>@]], 24 | [Enable introspection for this build]),, 25 | [enable_introspection=auto]) 26 | ])dnl 27 | 28 | AC_MSG_CHECKING([for gobject-introspection]) 29 | 30 | dnl presence/version checking 31 | AS_CASE([$enable_introspection], 32 | [no], [dnl 33 | found_introspection="no (disabled, use --enable-introspection to enable)" 34 | ],dnl 35 | [yes],[dnl 36 | PKG_CHECK_EXISTS([gobject-introspection-1.0],, 37 | AC_MSG_ERROR([gobject-introspection-1.0 is not installed])) 38 | PKG_CHECK_EXISTS([gobject-introspection-1.0 >= $1], 39 | found_introspection=yes, 40 | AC_MSG_ERROR([You need to have gobject-introspection >= $1 installed to build AC_PACKAGE_NAME])) 41 | ],dnl 42 | [auto],[dnl 43 | PKG_CHECK_EXISTS([gobject-introspection-1.0 >= $1], found_introspection=yes, found_introspection=no) 44 | dnl Canonicalize enable_introspection 45 | enable_introspection=$found_introspection 46 | ],dnl 47 | [dnl 48 | AC_MSG_ERROR([invalid argument passed to --enable-introspection, should be one of @<:@no/auto/yes@:>@]) 49 | ])dnl 50 | 51 | AC_MSG_RESULT([$found_introspection]) 52 | 53 | INTROSPECTION_SCANNER= 54 | INTROSPECTION_COMPILER= 55 | INTROSPECTION_GENERATE= 56 | INTROSPECTION_GIRDIR= 57 | INTROSPECTION_TYPELIBDIR= 58 | if test "x$found_introspection" = "xyes"; then 59 | INTROSPECTION_SCANNER=`$PKG_CONFIG --variable=g_ir_scanner gobject-introspection-1.0` 60 | INTROSPECTION_COMPILER=`$PKG_CONFIG --variable=g_ir_compiler gobject-introspection-1.0` 61 | INTROSPECTION_GENERATE=`$PKG_CONFIG --variable=g_ir_generate gobject-introspection-1.0` 62 | INTROSPECTION_GIRDIR=`$PKG_CONFIG --variable=girdir gobject-introspection-1.0` 63 | INTROSPECTION_TYPELIBDIR="$($PKG_CONFIG --variable=typelibdir gobject-introspection-1.0)" 64 | INTROSPECTION_CFLAGS=`$PKG_CONFIG --cflags gobject-introspection-1.0` 65 | INTROSPECTION_LIBS=`$PKG_CONFIG --libs gobject-introspection-1.0` 66 | INTROSPECTION_MAKEFILE=`$PKG_CONFIG --variable=datadir gobject-introspection-1.0`/gobject-introspection-1.0/Makefile.introspection 67 | fi 68 | AC_SUBST(INTROSPECTION_SCANNER) 69 | AC_SUBST(INTROSPECTION_COMPILER) 70 | AC_SUBST(INTROSPECTION_GENERATE) 71 | AC_SUBST(INTROSPECTION_GIRDIR) 72 | AC_SUBST(INTROSPECTION_TYPELIBDIR) 73 | AC_SUBST(INTROSPECTION_CFLAGS) 74 | AC_SUBST(INTROSPECTION_LIBS) 75 | AC_SUBST(INTROSPECTION_MAKEFILE) 76 | 77 | AM_CONDITIONAL(HAVE_INTROSPECTION, test "x$found_introspection" = "xyes") 78 | ]) 79 | 80 | 81 | dnl Usage: 82 | dnl GOBJECT_INTROSPECTION_CHECK([minimum-g-i-version]) 83 | 84 | AC_DEFUN([GOBJECT_INTROSPECTION_CHECK], 85 | [ 86 | _GOBJECT_INTROSPECTION_CHECK_INTERNAL([$1]) 87 | ]) 88 | 89 | dnl Usage: 90 | dnl GOBJECT_INTROSPECTION_REQUIRE([minimum-g-i-version]) 91 | 92 | 93 | AC_DEFUN([GOBJECT_INTROSPECTION_REQUIRE], 94 | [ 95 | _GOBJECT_INTROSPECTION_CHECK_INTERNAL([$1], [require]) 96 | ]) 97 | -------------------------------------------------------------------------------- /m4/ax_compiler_vendor.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_compiler_vendor.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_COMPILER_VENDOR 8 | # 9 | # DESCRIPTION 10 | # 11 | # Determine the vendor of the C/C++ compiler, e.g., gnu, intel, ibm, sun, 12 | # hp, borland, comeau, dec, cray, kai, lcc, metrowerks, sgi, microsoft, 13 | # watcom, etc. The vendor is returned in the cache variable 14 | # $ax_cv_c_compiler_vendor for C and $ax_cv_cxx_compiler_vendor for C++. 15 | # 16 | # LICENSE 17 | # 18 | # Copyright (c) 2008 Steven G. Johnson 19 | # Copyright (c) 2008 Matteo Frigo 20 | # 21 | # This program is free software: you can redistribute it and/or modify it 22 | # under the terms of the GNU General Public License as published by the 23 | # Free Software Foundation, either version 3 of the License, or (at your 24 | # option) any later version. 25 | # 26 | # This program is distributed in the hope that it will be useful, but 27 | # WITHOUT ANY WARRANTY; without even the implied warranty of 28 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 29 | # Public License for more details. 30 | # 31 | # You should have received a copy of the GNU General Public License along 32 | # with this program. If not, see . 33 | # 34 | # As a special exception, the respective Autoconf Macro's copyright owner 35 | # gives unlimited permission to copy, distribute and modify the configure 36 | # scripts that are the output of Autoconf when processing the Macro. You 37 | # need not follow the terms of the GNU General Public License when using 38 | # or distributing such scripts, even though portions of the text of the 39 | # Macro appear in them. The GNU General Public License (GPL) does govern 40 | # all other use of the material that constitutes the Autoconf Macro. 41 | # 42 | # This special exception to the GPL applies to versions of the Autoconf 43 | # Macro released by the Autoconf Archive. When you make and distribute a 44 | # modified version of the Autoconf Macro, you may extend this special 45 | # exception to the GPL to apply to your modified version as well. 46 | 47 | #serial 15 48 | 49 | AC_DEFUN([AX_COMPILER_VENDOR], 50 | [AC_CACHE_CHECK([for _AC_LANG compiler vendor], ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor, 51 | dnl Please add if possible support to ax_compiler_version.m4 52 | [# note: don't check for gcc first since some other compilers define __GNUC__ 53 | vendors="intel: __ICC,__ECC,__INTEL_COMPILER 54 | ibm: __xlc__,__xlC__,__IBMC__,__IBMCPP__ 55 | pathscale: __PATHCC__,__PATHSCALE__ 56 | clang: __clang__ 57 | cray: _CRAYC 58 | fujitsu: __FUJITSU 59 | gnu: __GNUC__ 60 | sun: __SUNPRO_C,__SUNPRO_CC 61 | hp: __HP_cc,__HP_aCC 62 | dec: __DECC,__DECCXX,__DECC_VER,__DECCXX_VER 63 | borland: __BORLANDC__,__CODEGEARC__,__TURBOC__ 64 | comeau: __COMO__ 65 | kai: __KCC 66 | lcc: __LCC__ 67 | sgi: __sgi,sgi 68 | microsoft: _MSC_VER 69 | metrowerks: __MWERKS__ 70 | watcom: __WATCOMC__ 71 | portland: __PGI 72 | tcc: __TINYC__ 73 | unknown: UNKNOWN" 74 | for ventest in $vendors; do 75 | case $ventest in 76 | *:) vendor=$ventest; continue ;; 77 | *) vencpp="defined("`echo $ventest | sed 's/,/) || defined(/g'`")" ;; 78 | esac 79 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,[ 80 | #if !($vencpp) 81 | thisisanerror; 82 | #endif 83 | ])], [break]) 84 | done 85 | ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor=`echo $vendor | cut -d: -f1` 86 | ]) 87 | ]) 88 | -------------------------------------------------------------------------------- /src/bayes-storage.c: -------------------------------------------------------------------------------- 1 | /* bayes-storage.c 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "bayes-storage.h" 20 | 21 | /** 22 | * SECTION:bayes-storage 23 | * @title: BayesStorage 24 | * @short_description: Interface for storing training data. 25 | * 26 | * The #BayesStorage interface provides a common interface for storing 27 | * training data for the classifier. 28 | * 29 | * See #BayesStorageMemory for in memory storage of training data. 30 | */ 31 | 32 | G_DEFINE_INTERFACE (BayesStorage, bayes_storage, G_TYPE_OBJECT) 33 | 34 | static void 35 | bayes_storage_real_add_token_count (BayesStorage *self, 36 | const gchar *name, 37 | const gchar *token, 38 | guint count) 39 | { 40 | } 41 | 42 | static gchar ** 43 | bayes_storage_real_get_names (BayesStorage *self) 44 | { 45 | return NULL; 46 | } 47 | 48 | static guint 49 | bayes_storage_real_get_token_count (BayesStorage *self, 50 | const gchar *name, 51 | const gchar *token) 52 | { 53 | return 0; 54 | } 55 | 56 | static gdouble 57 | bayes_storage_real_get_token_probability (BayesStorage *self, 58 | const gchar *name, 59 | const gchar *token) 60 | { 61 | return 0.0; 62 | } 63 | 64 | static void 65 | bayes_storage_default_init (BayesStorageInterface *iface) 66 | { 67 | iface->add_token_count = bayes_storage_real_add_token_count; 68 | iface->get_names = bayes_storage_real_get_names; 69 | iface->get_token_count = bayes_storage_real_get_token_count; 70 | iface->get_token_probability = bayes_storage_real_get_token_probability; 71 | } 72 | 73 | void 74 | bayes_storage_add_token_count (BayesStorage *self, 75 | const gchar *name, 76 | const gchar *token, 77 | guint count) 78 | { 79 | g_return_if_fail (BAYES_IS_STORAGE (self)); 80 | g_return_if_fail (name); 81 | g_return_if_fail (token); 82 | g_return_if_fail (count); 83 | 84 | BAYES_STORAGE_GET_IFACE (self)->add_token_count (self, name, token, count); 85 | } 86 | 87 | void 88 | bayes_storage_add_token (BayesStorage *self, 89 | const gchar *name, 90 | const gchar *token) 91 | { 92 | g_return_if_fail (BAYES_IS_STORAGE (self)); 93 | g_return_if_fail (name); 94 | g_return_if_fail (token); 95 | 96 | BAYES_STORAGE_GET_IFACE (self)->add_token_count (self, name, token, 1); 97 | } 98 | 99 | gchar ** 100 | bayes_storage_get_names (BayesStorage *self) 101 | { 102 | g_return_val_if_fail (BAYES_IS_STORAGE (self), NULL); 103 | 104 | return BAYES_STORAGE_GET_IFACE (self)->get_names (self); 105 | } 106 | 107 | guint 108 | bayes_storage_get_token_count (BayesStorage *self, 109 | const gchar *name, 110 | const gchar *token) 111 | { 112 | g_return_val_if_fail (BAYES_IS_STORAGE (self), 0); 113 | g_return_val_if_fail (name || token, 0); 114 | 115 | return BAYES_STORAGE_GET_IFACE (self)->get_token_count (self, name, token); 116 | } 117 | 118 | gdouble 119 | bayes_storage_get_token_probability (BayesStorage *self, 120 | const gchar *name, 121 | const gchar *token) 122 | { 123 | g_return_val_if_fail (BAYES_IS_STORAGE (self), 0.0); 124 | g_return_val_if_fail (name, 0.0); 125 | g_return_val_if_fail (token, 0.0); 126 | 127 | return BAYES_STORAGE_GET_IFACE (self)->get_token_probability (self, name, token); 128 | } 129 | -------------------------------------------------------------------------------- /src/bayes-classifier.h: -------------------------------------------------------------------------------- 1 | /* bayes-classifier.h 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_CLASSIFIER_H 20 | #define BAYES_CLASSIFIER_H 21 | 22 | #include 23 | 24 | #include "bayes-storage.h" 25 | #include "bayes-tokenizer.h" 26 | 27 | G_BEGIN_DECLS 28 | 29 | #define BAYES_TYPE_CLASSIFIER (bayes_classifier_get_type()) 30 | 31 | G_DECLARE_FINAL_TYPE (BayesClassifier, bayes_classifier, BAYES, CLASSIFIER, GObject) 32 | 33 | /** 34 | * bayes_classifier_get_storage: 35 | * @self: (in): A #BayesClassifier. 36 | * 37 | * Retrieves the #BayesStorage used for tokens by @classifier. 38 | * 39 | * Returns: (transfer none): A #BayesStorage. 40 | */ 41 | BayesStorage *bayes_classifier_get_storage (BayesClassifier *self); 42 | 43 | /** 44 | * bayes_classifier_guess: 45 | * @self: (in): A #BayesClassifier. 46 | * @text: (in): Text to tokenize and guess the classification. 47 | * 48 | * Tries to guess the classification of @text by tokenizing @text using 49 | * the tokenizer provided to bayes_classifier_set_tokenizer() 50 | * and testing each token against the classifiers training. 51 | * 52 | * This method returns a #GList of #BayesGuess instances. It is up to the 53 | * caller to free the individual #BayesGuess structures as well as the 54 | * containing #GList. 55 | * 56 | * |[ 57 | * GList *list = bayes_classifier_guess (classifier, "who am i?"); 58 | * g_list_foreach (list, (GFunc)bayes_guess_unref); 59 | * g_list_free (list); 60 | * ]| 61 | * 62 | * Returns: (element-type BayesGuess) (transfer full): The guesses. 63 | */ 64 | GList *bayes_classifier_guess (BayesClassifier *self, 65 | const gchar *text); 66 | 67 | /** 68 | * bayes_classifier_new: 69 | * 70 | * Create a new instance of #BayesClassifier. The instance should be 71 | * freed using g_object_unref (). 72 | * 73 | * See bayes_classifier_train () for how to train your classifier. 74 | * See bayes_classifier_guess () for guessing the classification of 75 | * unknown input. 76 | * 77 | * Returns: (transfer full): A newly allocated #BayesClassifier. 78 | */ 79 | BayesClassifier *bayes_classifier_new (void); 80 | 81 | /** 82 | * bayes_classifier_set_storage: 83 | * @self: (in): A #BayesClassifier. 84 | * @storage: (in) (allow-none): A #BayesStorage or %NULL. 85 | * 86 | * Sets the storage to use for tokens by the classifier. 87 | * If @storage is %NULL, then in memory storage will be used. 88 | */ 89 | void bayes_classifier_set_storage (BayesClassifier *self, 90 | BayesStorage *storage); 91 | 92 | /** 93 | * bayes_classifier_set_tokenizer: 94 | * @self: (in): A #BayesClassifier. 95 | * @tokenizer: (in): A #BayesTokenizer. 96 | * @user_data: User data for @tokenizer. 97 | * @notify: Destruction notification for @user_data. 98 | * 99 | * Sets the tokenizer to use to tokenize input text by @classifer for 100 | * both training using bayes_classifier_train() and guessing using 101 | * bayes_classifier_guess(). 102 | */ 103 | void bayes_classifier_set_tokenizer (BayesClassifier *self, 104 | BayesTokenizer tokenizer, 105 | gpointer user_data, 106 | GDestroyNotify notify); 107 | 108 | /** 109 | * bayes_classifier_train: 110 | * @self: (in): A #BayesClassifier. 111 | * @name: (in): The classification for @text. 112 | * @text: (in): Text to tokenize and store for guessing. 113 | * 114 | * Tokenizes @text and stores the values under the classification named 115 | * @name. These are used by bayes_classifier_guess () to determine 116 | * the classification. 117 | */ 118 | void bayes_classifier_train (BayesClassifier *self, 119 | const gchar *name, 120 | const gchar *text); 121 | 122 | G_END_DECLS 123 | 124 | #endif /* BAYES_CLASSIFIER_H */ 125 | -------------------------------------------------------------------------------- /src/bayes-storage.h: -------------------------------------------------------------------------------- 1 | /* bayes-storage.h 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef BAYES_STORAGE_H 20 | #define BAYES_STORAGE_H 21 | 22 | #include 23 | 24 | G_BEGIN_DECLS 25 | 26 | #define BAYES_TYPE_STORAGE (bayes_storage_get_type()) 27 | 28 | G_DECLARE_INTERFACE (BayesStorage, bayes_storage, BAYES, STORAGE, GObject) 29 | 30 | struct _BayesStorageInterface 31 | { 32 | GTypeInterface parent; 33 | 34 | void (*add_token_count) (BayesStorage *self, 35 | const gchar *name, 36 | const gchar *token, 37 | guint count); 38 | gchar **(*get_names) (BayesStorage *self); 39 | guint (*get_token_count) (BayesStorage *self, 40 | const gchar *name, 41 | const gchar *token); 42 | gdouble (*get_token_probability) (BayesStorage *self, 43 | const gchar *name, 44 | const gchar *token); 45 | }; 46 | 47 | /** 48 | * bayes_storage_add_token: 49 | * @self: A #BayesStorage. 50 | * @name: The classification to store the token in. 51 | * @token: The token to add. 52 | * 53 | * This function will add a token to the storage, applying it to the given 54 | * classification. 55 | */ 56 | void bayes_storage_add_token (BayesStorage *self, 57 | const gchar *name, 58 | const gchar *token); 59 | /** 60 | * bayes_storage_add_token_count: 61 | * @self: A #BayesStorage. 62 | * @name: The classification to store the token in. 63 | * @token: The token to add. 64 | * @count: The count of times @token was found. 65 | * 66 | * This function will add a token to the storage, applying it to the given 67 | * classification. @count should indiciate the number of times that the 68 | * token was found. 69 | */ 70 | void bayes_storage_add_token_count (BayesStorage *self, 71 | const gchar *name, 72 | const gchar *token, 73 | guint count); 74 | 75 | /** 76 | * bayes_storage_get_names: 77 | * @self: A #BayesStorage. 78 | * 79 | * Retrieves the names of the classifications trained in this storage 80 | * instance. The result should be freed with g_strfreev(). 81 | * 82 | * Returns: (array zero-terminated=1) (transfer full): 83 | * A list of class names. 84 | */ 85 | gchar **bayes_storage_get_names (BayesStorage *self); 86 | 87 | /** 88 | * bayes_storage_get_token_count: 89 | * @self: A #BayesStorage. 90 | * @name: (nullable): The classification or %NULL for all. 91 | * @token: (nullable): The token or %NULL for all. 92 | * 93 | * Retrieves the number of times @token has been found in the training 94 | * data. If @token is %NULL, the count of all items in the classification 95 | * will be retrieved. If @name is %NULL, then the count of all the 96 | * instances of @token in the all the classifications. 97 | * 98 | * Returns: A #guint containing the count of all items. 99 | */ 100 | guint bayes_storage_get_token_count (BayesStorage *self, 101 | const gchar *name, 102 | const gchar *token); 103 | 104 | /** 105 | * bayes_storage_get_token_probability: 106 | * @self: A #BayesStorage. 107 | * @name: The classification. 108 | * @token: The desired token. 109 | * 110 | * Checks to see the probability of a token being a given classification. 111 | * 112 | * Returns: A #gdouble between 0.0 and 1.0 containing the probability. 113 | */ 114 | gdouble bayes_storage_get_token_probability (BayesStorage *self, 115 | const gchar *name, 116 | const gchar *token); 117 | 118 | G_END_DECLS 119 | 120 | #endif /* BAYES_STORAGE_H */ 121 | -------------------------------------------------------------------------------- /m4/ltsugar.m4: -------------------------------------------------------------------------------- 1 | # ltsugar.m4 -- libtool m4 base layer. -*-Autoconf-*- 2 | # 3 | # Copyright (C) 2004-2005, 2007-2008, 2011-2015 Free Software 4 | # Foundation, Inc. 5 | # Written by Gary V. Vaughan, 2004 6 | # 7 | # This file is free software; the Free Software Foundation gives 8 | # unlimited permission to copy and/or distribute it, with or without 9 | # modifications, as long as this notice is preserved. 10 | 11 | # serial 6 ltsugar.m4 12 | 13 | # This is to help aclocal find these macros, as it can't see m4_define. 14 | AC_DEFUN([LTSUGAR_VERSION], [m4_if([0.1])]) 15 | 16 | 17 | # lt_join(SEP, ARG1, [ARG2...]) 18 | # ----------------------------- 19 | # Produce ARG1SEPARG2...SEPARGn, omitting [] arguments and their 20 | # associated separator. 21 | # Needed until we can rely on m4_join from Autoconf 2.62, since all earlier 22 | # versions in m4sugar had bugs. 23 | m4_define([lt_join], 24 | [m4_if([$#], [1], [], 25 | [$#], [2], [[$2]], 26 | [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift(m4_shift($@)))])]) 27 | m4_define([_lt_join], 28 | [m4_if([$#$2], [2], [], 29 | [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift(m4_shift($@)))])]) 30 | 31 | 32 | # lt_car(LIST) 33 | # lt_cdr(LIST) 34 | # ------------ 35 | # Manipulate m4 lists. 36 | # These macros are necessary as long as will still need to support 37 | # Autoconf-2.59, which quotes differently. 38 | m4_define([lt_car], [[$1]]) 39 | m4_define([lt_cdr], 40 | [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])], 41 | [$#], 1, [], 42 | [m4_dquote(m4_shift($@))])]) 43 | m4_define([lt_unquote], $1) 44 | 45 | 46 | # lt_append(MACRO-NAME, STRING, [SEPARATOR]) 47 | # ------------------------------------------ 48 | # Redefine MACRO-NAME to hold its former content plus 'SEPARATOR''STRING'. 49 | # Note that neither SEPARATOR nor STRING are expanded; they are appended 50 | # to MACRO-NAME as is (leaving the expansion for when MACRO-NAME is invoked). 51 | # No SEPARATOR is output if MACRO-NAME was previously undefined (different 52 | # than defined and empty). 53 | # 54 | # This macro is needed until we can rely on Autoconf 2.62, since earlier 55 | # versions of m4sugar mistakenly expanded SEPARATOR but not STRING. 56 | m4_define([lt_append], 57 | [m4_define([$1], 58 | m4_ifdef([$1], [m4_defn([$1])[$3]])[$2])]) 59 | 60 | 61 | 62 | # lt_combine(SEP, PREFIX-LIST, INFIX, SUFFIX1, [SUFFIX2...]) 63 | # ---------------------------------------------------------- 64 | # Produce a SEP delimited list of all paired combinations of elements of 65 | # PREFIX-LIST with SUFFIX1 through SUFFIXn. Each element of the list 66 | # has the form PREFIXmINFIXSUFFIXn. 67 | # Needed until we can rely on m4_combine added in Autoconf 2.62. 68 | m4_define([lt_combine], 69 | [m4_if(m4_eval([$# > 3]), [1], 70 | [m4_pushdef([_Lt_sep], [m4_define([_Lt_sep], m4_defn([lt_car]))])]]dnl 71 | [[m4_foreach([_Lt_prefix], [$2], 72 | [m4_foreach([_Lt_suffix], 73 | ]m4_dquote(m4_dquote(m4_shift(m4_shift(m4_shift($@)))))[, 74 | [_Lt_sep([$1])[]m4_defn([_Lt_prefix])[$3]m4_defn([_Lt_suffix])])])])]) 75 | 76 | 77 | # lt_if_append_uniq(MACRO-NAME, VARNAME, [SEPARATOR], [UNIQ], [NOT-UNIQ]) 78 | # ----------------------------------------------------------------------- 79 | # Iff MACRO-NAME does not yet contain VARNAME, then append it (delimited 80 | # by SEPARATOR if supplied) and expand UNIQ, else NOT-UNIQ. 81 | m4_define([lt_if_append_uniq], 82 | [m4_ifdef([$1], 83 | [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1], 84 | [lt_append([$1], [$2], [$3])$4], 85 | [$5])], 86 | [lt_append([$1], [$2], [$3])$4])]) 87 | 88 | 89 | # lt_dict_add(DICT, KEY, VALUE) 90 | # ----------------------------- 91 | m4_define([lt_dict_add], 92 | [m4_define([$1($2)], [$3])]) 93 | 94 | 95 | # lt_dict_add_subkey(DICT, KEY, SUBKEY, VALUE) 96 | # -------------------------------------------- 97 | m4_define([lt_dict_add_subkey], 98 | [m4_define([$1($2:$3)], [$4])]) 99 | 100 | 101 | # lt_dict_fetch(DICT, KEY, [SUBKEY]) 102 | # ---------------------------------- 103 | m4_define([lt_dict_fetch], 104 | [m4_ifval([$3], 105 | m4_ifdef([$1($2:$3)], [m4_defn([$1($2:$3)])]), 106 | m4_ifdef([$1($2)], [m4_defn([$1($2)])]))]) 107 | 108 | 109 | # lt_if_dict_fetch(DICT, KEY, [SUBKEY], VALUE, IF-TRUE, [IF-FALSE]) 110 | # ----------------------------------------------------------------- 111 | m4_define([lt_if_dict_fetch], 112 | [m4_if(lt_dict_fetch([$1], [$2], [$3]), [$4], 113 | [$5], 114 | [$6])]) 115 | 116 | 117 | # lt_dict_filter(DICT, [SUBKEY], VALUE, [SEPARATOR], KEY, [...]) 118 | # -------------------------------------------------------------- 119 | m4_define([lt_dict_filter], 120 | [m4_if([$5], [], [], 121 | [lt_join(m4_quote(m4_default([$4], [[, ]])), 122 | lt_unquote(m4_split(m4_normalize(m4_foreach(_Lt_key, lt_car([m4_shiftn(4, $@)]), 123 | [lt_if_dict_fetch([$1], _Lt_key, [$2], [$3], [_Lt_key ])])))))])[]dnl 124 | ]) 125 | -------------------------------------------------------------------------------- /tools/trainer.vala: -------------------------------------------------------------------------------- 1 | // valac {BayesStorageSerializable,trainer}.vala -g --enable-experimental --pkg Bayes-1.0 --pkg gio-2.0 --pkg json-glib-1.0 -o trainer 2 | 3 | public class SourceCodeTrainer { 4 | static Bayes.Classifier classifier; 5 | 6 | // options 7 | private static string? samples_dir = null; 8 | private static string? output_file = null; 9 | private static string? tokenizer = "word"; 10 | 11 | static bool train_file (string name, string filename) { 12 | File file = File.new_for_path (filename); 13 | uint8[] buf; 14 | string etag; 15 | 16 | try { 17 | file.load_contents (null, out buf, out etag); 18 | classifier.train (name, (string) buf); 19 | return true; 20 | } catch (Error e) { 21 | stdout.printf ("error: %s\n", e.message); 22 | return false; 23 | } 24 | } 25 | 26 | static void train_test (string lang_dir, string language) { 27 | File samples = File.new_for_path (@"$samples_dir/$lang_dir"); 28 | if (FileUtils.test (samples.get_path (), FileTest.EXISTS) && FileUtils.test (samples.get_path (), FileTest.IS_DIR)) { 29 | FileEnumerator enumerator = samples.enumerate_children ("standard::*", FileQueryInfoFlags.NOFOLLOW_SYMLINKS); 30 | 31 | FileInfo info = null; 32 | while ((info = enumerator.next_file ()) != null) { 33 | if (info.get_file_type () == FileType.DIRECTORY) 34 | continue; 35 | Test.message ("training with %s...", info.get_name ()); 36 | if (!train_file (language, @"$samples_dir/$lang_dir/%s".printf(info.get_name ()))) 37 | Test.fail (); 38 | } 39 | } 40 | } 41 | 42 | private const OptionEntry[] options = { 43 | // --samples 44 | { "samples", 'd', 0, OptionArg.FILENAME, ref samples_dir, "Directory where samples are located", "DIRECTORY" }, 45 | // --output 46 | { "output", 'o', 0, OptionArg.FILENAME, ref output_file, "Output results to file", "FILE" }, 47 | // --tokenizer 48 | { "tokenizer", 't', 0, OptionArg.STRING, ref tokenizer, "Tokenizer function", "'word' | 'code_tokens'" }, 49 | 50 | { null } 51 | }; 52 | 53 | // generate file from tests 54 | static int main (string[] args) { 55 | try { 56 | var opt_context = new OptionContext ("- Generate Training Data"); 57 | opt_context.add_main_entries (options, null); 58 | opt_context.set_help_enabled (true); 59 | opt_context.parse (ref args); 60 | } catch (OptionError e) { 61 | stdout.printf ("error: %s\n", e.message); 62 | stdout.printf ("Run '%s --help' to see available command-line options.\n", args[0]); 63 | return 0; 64 | } 65 | classifier = new Bayes.Classifier (); 66 | classifier.storage = new Bayes.StorageMemory (); 67 | 68 | // FIXME: bindings 69 | if (tokenizer == "word") 70 | classifier.set_tokenizer (text => { 71 | return Bayes.tokenizer_word (text, null); 72 | }); 73 | else if (tokenizer == "code_tokens") 74 | classifier.set_tokenizer (text => { 75 | return Bayes.tokenizer_code_tokens (text, null); 76 | }); 77 | 78 | Test.init (ref args); 79 | 80 | // training 81 | Test.add_func ("/Train/C", () => train_test ("C", "c")); 82 | Test.add_func ("/Train/C#", () => train_test ("C#", "c-sharp")); 83 | Test.add_func ("/Train/C++", () => train_test ("C++", "cpp")); 84 | Test.add_func ("/Train/CMake", () => train_test ("CMake", "cmake")); 85 | Test.add_func ("/Train/CSS", () => train_test ("CSS", "css")); 86 | Test.add_func ("/Train/F#", () => train_test ("F#", "fsharp")); 87 | Test.add_func ("/Train/GLSL", () => train_test ("GLSL", "glsl")); 88 | Test.add_func ("/Train/Java", () => train_test ("Java", "java")); 89 | Test.add_func ("/Train/JavaScript", () => train_test ("JavaScript", "js")); 90 | Test.add_func ("/Train/JSON", () => train_test ("JSON", "json")); 91 | Test.add_func ("/Train/Haskell", () => train_test ("Haskell", "haskell")); 92 | Test.add_func ("/Train/HTML", () => train_test ("HTML", "html")); 93 | Test.add_func ("/Train/Makefile", () => train_test ("Makefile", "makefile")); 94 | Test.add_func ("/Train/PHP", () => train_test ("PHP", "php")); 95 | Test.add_func ("/Train/Python", () => train_test ("Python", "python")); 96 | Test.add_func ("/Train/Ruby", () => train_test ("Ruby", "ruby")); 97 | Test.add_func ("/Train/Scala", () => train_test ("Scala", "scala")); 98 | Test.add_func ("/Train/Shell", () => train_test ("Shell", "sh")); 99 | Test.add_func ("/Train/SQL", () => train_test ("SQL", "sql")); 100 | Test.add_func ("/Train/TeX", () => train_test ("LaTeX", "latex")); 101 | 102 | // serializing 103 | Test.add_func ("/Serialize/Training Data", () => { 104 | if ((classifier.storage as Bayes.StorageMemory).save_to_file (output_file)) 105 | Test.message ("saved to %s", output_file); 106 | else 107 | Test.fail (); 108 | }); 109 | 110 | return Test.run (); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /m4/vala.m4: -------------------------------------------------------------------------------- 1 | dnl vala.m4 2 | dnl 3 | dnl Copyright 2010 Marc-Andre Lureau 4 | dnl Copyright 2011 Rodney Dawes 5 | dnl 6 | dnl This library is free software; you can redistribute it and/or 7 | dnl modify it under the terms of the GNU Lesser General Public 8 | dnl License as published by the Free Software Foundation; either 9 | dnl version 2.1 of the License, or (at your option) any later version. 10 | dnl 11 | dnl This library is distributed in the hope that it will be useful, 12 | dnl but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | dnl Lesser General Public License for more details. 15 | dnl 16 | dnl You should have received a copy of the GNU Lesser General Public 17 | dnl License along with this library; if not, write to the Free Software 18 | dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | 20 | # _VALA_CHECK_COMPILE_WITH_ARGS(ARGS, [ACTION-IF-TRUE], 21 | # [ACTION-IF-FALSE]) 22 | # -------------------------------------- 23 | # Check that Vala compile with ARGS. 24 | # 25 | AC_DEFUN([_VALA_CHECK_COMPILE_WITH_ARGS], 26 | [AC_REQUIRE([AM_PROG_VALAC])[]dnl 27 | 28 | cat <<_ACEOF >conftest.vala 29 | void main(){} 30 | _ACEOF 31 | 32 | AS_IF([vala_error=`$VALAC $1 -q --cc="${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS}" -o conftest$ac_exeext conftest.vala 2>&1`], 33 | [$2], [$3]) 34 | ]) 35 | 36 | ])# _VALA_CHECK_COMPILE_WITH_ARGS 37 | 38 | # VALA_CHECK_PACKAGES(PKGS, [ACTION-IF-FOUND], 39 | # [ACTION-IF-NOT-FOUND]) 40 | # -------------------------------------- 41 | # Check that PKGS Vala bindings are installed and usable. 42 | # 43 | AC_DEFUN([VALA_CHECK_PACKAGES], 44 | [ 45 | unset vala_pkgs 46 | unset vala_bindings 47 | ac_save_ifs="$IFS"; unset IFS 48 | for vala_pkg in $(echo "$1"); do 49 | vala_pkgs="${vala_pkgs:+$vala_pkgs }--pkg $vala_pkg" 50 | vala_bindings="${vala_bindings:+$vala_bindings }$vala_pkg" 51 | done 52 | IFS="$ac_save_ifs" 53 | AC_MSG_CHECKING([for $vala_bindings vala bindings]) 54 | _VALA_CHECK_COMPILE_WITH_ARGS([$vala_pkgs], 55 | [vala_pkg_exists=yes], 56 | [vala_pkg_exists=no]) 57 | 58 | AS_IF([test x${vala_pkg_exists} = xno],[ 59 | ifelse([$3], , [AC_MSG_ERROR([]dnl 60 | [Package requirements were not met: $1 61 | 62 | $vala_error 63 | 64 | Consider adjusting the XDG_DATA_DIRS environment variable if you 65 | installed bindings in a non-standard prefix. 66 | ])], 67 | [AC_MSG_RESULT([no]) 68 | $3])],[ 69 | AC_MSG_RESULT([yes]) 70 | ifelse([$2], , :, [$2])[]dnl 71 | ]) 72 | 73 | ])# VALA_CHECK_PACKAGES 74 | 75 | 76 | # Check for Vala bindings for a package, as well as the pkg-config 77 | # CFLAGS and LIBS for the package. The arguments here work the 78 | # same as those for PKG_CHECK_MODULES, which is called internally. 79 | # As a result, the _CFLAGS, _LIBS, and _VALAFLAGS variables will 80 | # all be declared, rather than only _VALAFLAGS. 81 | # 82 | # VALA_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND], 83 | # [ACTION-IF-NOT-FOUND]) 84 | # -------------------------------------------------------------- 85 | AC_DEFUN([VALA_CHECK_MODULES], 86 | [ 87 | AC_REQUIRE([AM_PROG_VALAC])dnl 88 | AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl 89 | AC_REQUIRE([_VALA_CHECK_COMPILE_WITH_ARGS])dnl 90 | AC_ARG_VAR([$1][_VALAFLAGS], [Vala compiler flags for $1])dnl 91 | 92 | VALA_MODULES="`echo $2 | sed -e 's/ [[=<>]]\+ [[0-9.]]\+//g'`" 93 | for MODULE in $VALA_MODULES; do 94 | $1[]_VALAFLAGS="$[]$1[]_VALAFLAGS --pkg $MODULE" 95 | done 96 | 97 | PKG_CHECK_MODULES([$1], [$2], [$3], [$4]) 98 | 99 | pkg_failed=no 100 | AC_MSG_CHECKING([for $1 vala modules]) 101 | 102 | _VALA_CHECK_COMPILE_WITH_ARGS([$1][_VALAFLAGS], 103 | [pkg_failed=yes], 104 | [pkg_failed=no]) 105 | 106 | if test $pkg_failed = yes; then 107 | AC_MSG_RESULT([no]) 108 | m4_default([$4], [AC_MSG_ERROR( 109 | [Package requirements ($2) were not met.])dnl 110 | ]) 111 | else 112 | AC_MSG_RESULT([yes]) 113 | m4_default([$3], [:]) 114 | fi[]dnl 115 | ]) 116 | 117 | # Check whether the Vala API Generator exists in `PATH'. If it is found, 118 | # the variable VAPIGEN is set. Optionally a minimum release number of the 119 | # generator can be requested. 120 | # 121 | # VALA_PROG_VAPIGEN([MINIMUM-VERSION]) 122 | # ------------------------------------ 123 | AC_DEFUN([VALA_PROG_VAPIGEN], 124 | [AC_PATH_PROG([VAPIGEN], [vapigen], []) 125 | AS_IF([test -z "$VAPIGEN"], 126 | [AC_MSG_WARN([No Vala API Generator found. You will not be able to generate .vapi files.])], 127 | [AS_IF([test -n "$1"], 128 | [AC_MSG_CHECKING([$VAPIGEN is at least version $1]) 129 | am__vapigen_version=`$VAPIGEN --version | sed 's/Vala API Generator *//'` 130 | AS_VERSION_COMPARE([$1], ["$am__vapigen_version"], 131 | [AC_MSG_RESULT([yes])], 132 | [AC_MSG_RESULT([yes])], 133 | [AC_MSG_RESULT([no]) 134 | AC_MSG_ERROR([Vala API Generator $1 not found.])])])]) 135 | ]) 136 | -------------------------------------------------------------------------------- /src/bayes-tokenizer.c: -------------------------------------------------------------------------------- 1 | /* bayes-tokenizer.c 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "bayes-tokenizer.h" 20 | 21 | static GRegex *word_regex; 22 | 23 | /** 24 | * SECTION:bayes-tokenizer 25 | * @title: BayesTokenizer 26 | * @short_description: Reusable tokenizers for input data. 27 | * 28 | * #BayesTokenizer callbacks are designed to allow customization on how 29 | * input text should be tokenized. For some tasks, this may be as simple 30 | * as splitting text on word boundries. Others may be more complicated. 31 | */ 32 | 33 | gchar ** 34 | bayes_tokenizer_word (const gchar *text, 35 | gpointer user_data) 36 | { 37 | static gsize initialized = FALSE; 38 | GMatchInfo *match_info; 39 | GPtrArray *ret; 40 | gchar **strv; 41 | guint i; 42 | 43 | if (g_once_init_enter (&initialized)) 44 | { 45 | word_regex = g_regex_new ("\\w+", G_REGEX_OPTIMIZE, 0, NULL); 46 | g_assert (word_regex); 47 | g_once_init_leave (&initialized, TRUE); 48 | } 49 | 50 | ret = g_ptr_array_new (); 51 | 52 | if (g_regex_match (word_regex, text, 0, &match_info)) 53 | { 54 | while (g_match_info_matches (match_info)) 55 | { 56 | strv = g_match_info_fetch_all (match_info); 57 | 58 | for (i = 0; strv[i]; i++) 59 | { 60 | g_ptr_array_add (ret, strv[i]); 61 | strv[i] = NULL; 62 | } 63 | 64 | g_free (strv); 65 | g_match_info_next (match_info, NULL); 66 | } 67 | } 68 | 69 | g_match_info_free (match_info); 70 | 71 | g_ptr_array_add (ret, NULL); 72 | 73 | return (gchar **)g_ptr_array_free (ret, FALSE); 74 | } 75 | 76 | #define _EXPR(expr, replace) { expr, FALSE, NULL, replace } 77 | 78 | gchar ** 79 | bayes_tokenizer_code_tokens (const gchar *text, 80 | gpointer user_data) 81 | { 82 | struct Expr { 83 | const gchar *expr; 84 | gsize initialized; 85 | GRegex *regex; 86 | const gchar *replace; 87 | }; 88 | static struct Expr expressions[] = { 89 | _EXPR ("(?\"\\)])", ":word:\\0"), 90 | _EXPR ("\\*+(?=[\\w\\[])", "\\0"), 91 | _EXPR ("\\<\\w+\\>", ":angle1:"), 92 | _EXPR ("(?])::(?=\\w)", "\\0"), 95 | _EXPR (" ::: ", "\\0"), 96 | _EXPR (" :: ", "\\0"), 97 | _EXPR ("(?<=[\\w\\)])\\.(?=\\w)", "\\0"), 98 | _EXPR("(?<=[\\w\\)])\\.$", "\\0"), 99 | _EXPR("(?=])=(?![>=])", "\\0"), 100 | _EXPR("[-=]{1,3}>", "\\0"), 101 | _EXPR("(?)", ":3+=:"), 102 | _EXPR(":=+(?![>=])", "::=:"), 103 | _EXPR("\\$[\\(<\\^]", "\\0"), 104 | _EXPR("\\|", "\\0"), 105 | _EXPR("\\[\\]", "\\0"), 106 | _EXPR("\\?", "\\0"), 107 | _EXPR("\"\\w+\"(?=:)", ":property:"), 108 | _EXPR("\\$\\w+", ":$word:"), 109 | _EXPR("{.*}", ":bracketed:"), 110 | _EXPR("#\\w+(?![\\>\"])", "\\0"), 111 | _EXPR("\\((\\w+)\\)\\s*(?=\\w)", ":cast:\\1"), 112 | _EXPR("\\((\\w+) ?\\*+\\)", ":castptr:\\1"), 113 | _EXPR("@\\w+", "\\0"), 114 | _EXPR("%\\w+", "\\0"), 115 | _EXPR("&\\w+", ":ref:"), 116 | _EXPR("<[\\w/]+\\.[a-z]+>", ":angle2:"), 117 | _EXPR(";\\n", ":end;:"), 118 | _EXPR(";(?!\\n)", ";"), 119 | _EXPR("(?!", ":end_tag:"), 124 | _EXPR("\\w+\\.\\w+\\(", ":object_call:"), 125 | _EXPR("\\w+->\\w+\\(", ":object_call_deref:"), 126 | { 0, 0 } 127 | }; 128 | GMatchInfo *match_info; 129 | GPtrArray *ret; 130 | gchar **strv; 131 | gchar *strr; 132 | guint i; 133 | 134 | struct Expr *expr; 135 | 136 | ret = g_ptr_array_new (); 137 | 138 | for (expr = &expressions[0]; expr->expr; ++expr) 139 | { 140 | if (g_once_init_enter (&expr->initialized)) 141 | { 142 | GError *error = NULL; 143 | expr->regex = g_regex_new (expr->expr, G_REGEX_OPTIMIZE, 0, &error); 144 | if (!expr->regex) 145 | { 146 | g_printerr ("failed to compile %s: %s", expr->expr, error->message); 147 | g_assert_not_reached (); 148 | } 149 | g_once_init_leave (&expr->initialized, TRUE); 150 | } 151 | 152 | if (g_regex_match (expr->regex, text, 0, &match_info)) 153 | { 154 | while (g_match_info_matches (match_info)) 155 | { 156 | strv = g_match_info_fetch_all (match_info); 157 | 158 | for (i = 0; strv[i]; i++) 159 | { 160 | strr = g_regex_replace (expr->regex, strv[i], -1, 0, 161 | expr->replace, 0, NULL); 162 | g_ptr_array_add (ret, strr); 163 | g_free (strv[i]); 164 | strv[i] = NULL; 165 | } 166 | 167 | g_free (strv); 168 | g_match_info_next (match_info, NULL); 169 | } 170 | } 171 | 172 | g_match_info_free (match_info); 173 | } 174 | 175 | g_ptr_array_add (ret, NULL); 176 | 177 | return (gchar **)g_ptr_array_free (ret, FALSE); 178 | } 179 | -------------------------------------------------------------------------------- /m4/lt~obsolete.m4: -------------------------------------------------------------------------------- 1 | # lt~obsolete.m4 -- aclocal satisfying obsolete definitions. -*-Autoconf-*- 2 | # 3 | # Copyright (C) 2004-2005, 2007, 2009, 2011-2015 Free Software 4 | # Foundation, Inc. 5 | # Written by Scott James Remnant, 2004. 6 | # 7 | # This file is free software; the Free Software Foundation gives 8 | # unlimited permission to copy and/or distribute it, with or without 9 | # modifications, as long as this notice is preserved. 10 | 11 | # serial 5 lt~obsolete.m4 12 | 13 | # These exist entirely to fool aclocal when bootstrapping libtool. 14 | # 15 | # In the past libtool.m4 has provided macros via AC_DEFUN (or AU_DEFUN), 16 | # which have later been changed to m4_define as they aren't part of the 17 | # exported API, or moved to Autoconf or Automake where they belong. 18 | # 19 | # The trouble is, aclocal is a bit thick. It'll see the old AC_DEFUN 20 | # in /usr/share/aclocal/libtool.m4 and remember it, then when it sees us 21 | # using a macro with the same name in our local m4/libtool.m4 it'll 22 | # pull the old libtool.m4 in (it doesn't see our shiny new m4_define 23 | # and doesn't know about Autoconf macros at all.) 24 | # 25 | # So we provide this file, which has a silly filename so it's always 26 | # included after everything else. This provides aclocal with the 27 | # AC_DEFUNs it wants, but when m4 processes it, it doesn't do anything 28 | # because those macros already exist, or will be overwritten later. 29 | # We use AC_DEFUN over AU_DEFUN for compatibility with aclocal-1.6. 30 | # 31 | # Anytime we withdraw an AC_DEFUN or AU_DEFUN, remember to add it here. 32 | # Yes, that means every name once taken will need to remain here until 33 | # we give up compatibility with versions before 1.7, at which point 34 | # we need to keep only those names which we still refer to. 35 | 36 | # This is to help aclocal find these macros, as it can't see m4_define. 37 | AC_DEFUN([LTOBSOLETE_VERSION], [m4_if([1])]) 38 | 39 | m4_ifndef([AC_LIBTOOL_LINKER_OPTION], [AC_DEFUN([AC_LIBTOOL_LINKER_OPTION])]) 40 | m4_ifndef([AC_PROG_EGREP], [AC_DEFUN([AC_PROG_EGREP])]) 41 | m4_ifndef([_LT_AC_PROG_ECHO_BACKSLASH], [AC_DEFUN([_LT_AC_PROG_ECHO_BACKSLASH])]) 42 | m4_ifndef([_LT_AC_SHELL_INIT], [AC_DEFUN([_LT_AC_SHELL_INIT])]) 43 | m4_ifndef([_LT_AC_SYS_LIBPATH_AIX], [AC_DEFUN([_LT_AC_SYS_LIBPATH_AIX])]) 44 | m4_ifndef([_LT_PROG_LTMAIN], [AC_DEFUN([_LT_PROG_LTMAIN])]) 45 | m4_ifndef([_LT_AC_TAGVAR], [AC_DEFUN([_LT_AC_TAGVAR])]) 46 | m4_ifndef([AC_LTDL_ENABLE_INSTALL], [AC_DEFUN([AC_LTDL_ENABLE_INSTALL])]) 47 | m4_ifndef([AC_LTDL_PREOPEN], [AC_DEFUN([AC_LTDL_PREOPEN])]) 48 | m4_ifndef([_LT_AC_SYS_COMPILER], [AC_DEFUN([_LT_AC_SYS_COMPILER])]) 49 | m4_ifndef([_LT_AC_LOCK], [AC_DEFUN([_LT_AC_LOCK])]) 50 | m4_ifndef([AC_LIBTOOL_SYS_OLD_ARCHIVE], [AC_DEFUN([AC_LIBTOOL_SYS_OLD_ARCHIVE])]) 51 | m4_ifndef([_LT_AC_TRY_DLOPEN_SELF], [AC_DEFUN([_LT_AC_TRY_DLOPEN_SELF])]) 52 | m4_ifndef([AC_LIBTOOL_PROG_CC_C_O], [AC_DEFUN([AC_LIBTOOL_PROG_CC_C_O])]) 53 | m4_ifndef([AC_LIBTOOL_SYS_HARD_LINK_LOCKS], [AC_DEFUN([AC_LIBTOOL_SYS_HARD_LINK_LOCKS])]) 54 | m4_ifndef([AC_LIBTOOL_OBJDIR], [AC_DEFUN([AC_LIBTOOL_OBJDIR])]) 55 | m4_ifndef([AC_LTDL_OBJDIR], [AC_DEFUN([AC_LTDL_OBJDIR])]) 56 | m4_ifndef([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH], [AC_DEFUN([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH])]) 57 | m4_ifndef([AC_LIBTOOL_SYS_LIB_STRIP], [AC_DEFUN([AC_LIBTOOL_SYS_LIB_STRIP])]) 58 | m4_ifndef([AC_PATH_MAGIC], [AC_DEFUN([AC_PATH_MAGIC])]) 59 | m4_ifndef([AC_PROG_LD_GNU], [AC_DEFUN([AC_PROG_LD_GNU])]) 60 | m4_ifndef([AC_PROG_LD_RELOAD_FLAG], [AC_DEFUN([AC_PROG_LD_RELOAD_FLAG])]) 61 | m4_ifndef([AC_DEPLIBS_CHECK_METHOD], [AC_DEFUN([AC_DEPLIBS_CHECK_METHOD])]) 62 | m4_ifndef([AC_LIBTOOL_PROG_COMPILER_NO_RTTI], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_NO_RTTI])]) 63 | m4_ifndef([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE], [AC_DEFUN([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE])]) 64 | m4_ifndef([AC_LIBTOOL_PROG_COMPILER_PIC], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_PIC])]) 65 | m4_ifndef([AC_LIBTOOL_PROG_LD_SHLIBS], [AC_DEFUN([AC_LIBTOOL_PROG_LD_SHLIBS])]) 66 | m4_ifndef([AC_LIBTOOL_POSTDEP_PREDEP], [AC_DEFUN([AC_LIBTOOL_POSTDEP_PREDEP])]) 67 | m4_ifndef([LT_AC_PROG_EGREP], [AC_DEFUN([LT_AC_PROG_EGREP])]) 68 | m4_ifndef([LT_AC_PROG_SED], [AC_DEFUN([LT_AC_PROG_SED])]) 69 | m4_ifndef([_LT_CC_BASENAME], [AC_DEFUN([_LT_CC_BASENAME])]) 70 | m4_ifndef([_LT_COMPILER_BOILERPLATE], [AC_DEFUN([_LT_COMPILER_BOILERPLATE])]) 71 | m4_ifndef([_LT_LINKER_BOILERPLATE], [AC_DEFUN([_LT_LINKER_BOILERPLATE])]) 72 | m4_ifndef([_AC_PROG_LIBTOOL], [AC_DEFUN([_AC_PROG_LIBTOOL])]) 73 | m4_ifndef([AC_LIBTOOL_SETUP], [AC_DEFUN([AC_LIBTOOL_SETUP])]) 74 | m4_ifndef([_LT_AC_CHECK_DLFCN], [AC_DEFUN([_LT_AC_CHECK_DLFCN])]) 75 | m4_ifndef([AC_LIBTOOL_SYS_DYNAMIC_LINKER], [AC_DEFUN([AC_LIBTOOL_SYS_DYNAMIC_LINKER])]) 76 | m4_ifndef([_LT_AC_TAGCONFIG], [AC_DEFUN([_LT_AC_TAGCONFIG])]) 77 | m4_ifndef([AC_DISABLE_FAST_INSTALL], [AC_DEFUN([AC_DISABLE_FAST_INSTALL])]) 78 | m4_ifndef([_LT_AC_LANG_CXX], [AC_DEFUN([_LT_AC_LANG_CXX])]) 79 | m4_ifndef([_LT_AC_LANG_F77], [AC_DEFUN([_LT_AC_LANG_F77])]) 80 | m4_ifndef([_LT_AC_LANG_GCJ], [AC_DEFUN([_LT_AC_LANG_GCJ])]) 81 | m4_ifndef([AC_LIBTOOL_LANG_C_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_C_CONFIG])]) 82 | m4_ifndef([_LT_AC_LANG_C_CONFIG], [AC_DEFUN([_LT_AC_LANG_C_CONFIG])]) 83 | m4_ifndef([AC_LIBTOOL_LANG_CXX_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_CXX_CONFIG])]) 84 | m4_ifndef([_LT_AC_LANG_CXX_CONFIG], [AC_DEFUN([_LT_AC_LANG_CXX_CONFIG])]) 85 | m4_ifndef([AC_LIBTOOL_LANG_F77_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_F77_CONFIG])]) 86 | m4_ifndef([_LT_AC_LANG_F77_CONFIG], [AC_DEFUN([_LT_AC_LANG_F77_CONFIG])]) 87 | m4_ifndef([AC_LIBTOOL_LANG_GCJ_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_GCJ_CONFIG])]) 88 | m4_ifndef([_LT_AC_LANG_GCJ_CONFIG], [AC_DEFUN([_LT_AC_LANG_GCJ_CONFIG])]) 89 | m4_ifndef([AC_LIBTOOL_LANG_RC_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_RC_CONFIG])]) 90 | m4_ifndef([_LT_AC_LANG_RC_CONFIG], [AC_DEFUN([_LT_AC_LANG_RC_CONFIG])]) 91 | m4_ifndef([AC_LIBTOOL_CONFIG], [AC_DEFUN([AC_LIBTOOL_CONFIG])]) 92 | m4_ifndef([_LT_AC_FILE_LTDLL_C], [AC_DEFUN([_LT_AC_FILE_LTDLL_C])]) 93 | m4_ifndef([_LT_REQUIRED_DARWIN_CHECKS], [AC_DEFUN([_LT_REQUIRED_DARWIN_CHECKS])]) 94 | m4_ifndef([_LT_AC_PROG_CXXCPP], [AC_DEFUN([_LT_AC_PROG_CXXCPP])]) 95 | m4_ifndef([_LT_PREPARE_SED_QUOTE_VARS], [AC_DEFUN([_LT_PREPARE_SED_QUOTE_VARS])]) 96 | m4_ifndef([_LT_PROG_ECHO_BACKSLASH], [AC_DEFUN([_LT_PROG_ECHO_BACKSLASH])]) 97 | m4_ifndef([_LT_PROG_F77], [AC_DEFUN([_LT_PROG_F77])]) 98 | m4_ifndef([_LT_PROG_FC], [AC_DEFUN([_LT_PROG_FC])]) 99 | m4_ifndef([_LT_PROG_CXX], [AC_DEFUN([_LT_PROG_CXX])]) 100 | -------------------------------------------------------------------------------- /m4/ax_cxx_compile_stdcxx_11.m4: -------------------------------------------------------------------------------- 1 | # ============================================================================ 2 | # http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html 3 | # ============================================================================ 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CXX_COMPILE_STDCXX_11([ext|noext],[mandatory|optional]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check for baseline language coverage in the compiler for the C++11 12 | # standard; if necessary, add switches to CXXFLAGS to enable support. 13 | # 14 | # The first argument, if specified, indicates whether you insist on an 15 | # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g. 16 | # -std=c++11). If neither is specified, you get whatever works, with 17 | # preference for an extended mode. 18 | # 19 | # The second argument, if specified 'mandatory' or if left unspecified, 20 | # indicates that baseline C++11 support is required and that the macro 21 | # should error out if no mode with that support is found. If specified 22 | # 'optional', then configuration proceeds regardless, after defining 23 | # HAVE_CXX11 if and only if a supporting mode is found. 24 | # 25 | # LICENSE 26 | # 27 | # Copyright (c) 2008 Benjamin Kosnik 28 | # Copyright (c) 2012 Zack Weinberg 29 | # Copyright (c) 2013 Roy Stogner 30 | # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov 31 | # Copyright (c) 2015 Paul Norman 32 | # 33 | # Copying and distribution of this file, with or without modification, are 34 | # permitted in any medium without royalty provided the copyright notice 35 | # and this notice are preserved. This file is offered as-is, without any 36 | # warranty. 37 | 38 | #serial 13 39 | 40 | m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody], [[ 41 | template 42 | struct check 43 | { 44 | static_assert(sizeof(int) <= sizeof(T), "not big enough"); 45 | }; 46 | 47 | struct Base { 48 | virtual void f() {} 49 | }; 50 | struct Child : public Base { 51 | virtual void f() override {} 52 | }; 53 | 54 | typedef check> right_angle_brackets; 55 | 56 | int a; 57 | decltype(a) b; 58 | 59 | typedef check check_type; 60 | check_type c; 61 | check_type&& cr = static_cast(c); 62 | 63 | auto d = a; 64 | auto l = [](){}; 65 | // Prevent Clang error: unused variable 'l' [-Werror,-Wunused-variable] 66 | struct use_l { use_l() { l(); } }; 67 | 68 | // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae 69 | // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function because of this 70 | namespace test_template_alias_sfinae { 71 | struct foo {}; 72 | 73 | template 74 | using member = typename T::member_type; 75 | 76 | template 77 | void func(...) {} 78 | 79 | template 80 | void func(member*) {} 81 | 82 | void test(); 83 | 84 | void test() { 85 | func(0); 86 | } 87 | } 88 | 89 | // Check for C++11 attribute support 90 | void noret [[noreturn]] () { throw 0; } 91 | ]]) 92 | 93 | AC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [dnl 94 | m4_if([$1], [], [], 95 | [$1], [ext], [], 96 | [$1], [noext], [], 97 | [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_11])])dnl 98 | m4_if([$2], [], [ax_cxx_compile_cxx11_required=true], 99 | [$2], [mandatory], [ax_cxx_compile_cxx11_required=true], 100 | [$2], [optional], [ax_cxx_compile_cxx11_required=false], 101 | [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX_11])]) 102 | AC_LANG_PUSH([C++])dnl 103 | ac_success=no 104 | AC_CACHE_CHECK(whether $CXX supports C++11 features by default, 105 | ax_cv_cxx_compile_cxx11, 106 | [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])], 107 | [ax_cv_cxx_compile_cxx11=yes], 108 | [ax_cv_cxx_compile_cxx11=no])]) 109 | if test x$ax_cv_cxx_compile_cxx11 = xyes; then 110 | ac_success=yes 111 | fi 112 | 113 | m4_if([$1], [noext], [], [dnl 114 | if test x$ac_success = xno; then 115 | for switch in -std=gnu++11 -std=gnu++0x; do 116 | cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch]) 117 | AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch, 118 | $cachevar, 119 | [ac_save_CXXFLAGS="$CXXFLAGS" 120 | CXXFLAGS="$CXXFLAGS $switch" 121 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])], 122 | [eval $cachevar=yes], 123 | [eval $cachevar=no]) 124 | CXXFLAGS="$ac_save_CXXFLAGS"]) 125 | if eval test x\$$cachevar = xyes; then 126 | CXXFLAGS="$CXXFLAGS $switch" 127 | ac_success=yes 128 | break 129 | fi 130 | done 131 | fi]) 132 | 133 | m4_if([$1], [ext], [], [dnl 134 | if test x$ac_success = xno; then 135 | dnl HP's aCC needs +std=c++11 according to: 136 | dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf 137 | dnl Cray's crayCC needs "-h std=c++11" 138 | for switch in -std=c++11 -std=c++0x +std=c++11 "-h std=c++11"; do 139 | cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx11_$switch]) 140 | AC_CACHE_CHECK(whether $CXX supports C++11 features with $switch, 141 | $cachevar, 142 | [ac_save_CXXFLAGS="$CXXFLAGS" 143 | CXXFLAGS="$CXXFLAGS $switch" 144 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody])], 145 | [eval $cachevar=yes], 146 | [eval $cachevar=no]) 147 | CXXFLAGS="$ac_save_CXXFLAGS"]) 148 | if eval test x\$$cachevar = xyes; then 149 | CXXFLAGS="$CXXFLAGS $switch" 150 | ac_success=yes 151 | break 152 | fi 153 | done 154 | fi]) 155 | AC_LANG_POP([C++]) 156 | if test x$ax_cxx_compile_cxx11_required = xtrue; then 157 | if test x$ac_success = xno; then 158 | AC_MSG_ERROR([*** A compiler with support for C++11 language features is required.]) 159 | fi 160 | else 161 | if test x$ac_success = xno; then 162 | HAVE_CXX11=0 163 | AC_MSG_NOTICE([No compiler with C++11 support was found]) 164 | else 165 | HAVE_CXX11=1 166 | AC_DEFINE(HAVE_CXX11,1, 167 | [define if the compiler supports basic C++11 syntax]) 168 | fi 169 | 170 | AC_SUBST(HAVE_CXX11) 171 | fi 172 | ]) 173 | -------------------------------------------------------------------------------- /m4/pkg.m4: -------------------------------------------------------------------------------- 1 | # pkg.m4 - Macros to locate and utilise pkg-config. -*- Autoconf -*- 2 | # serial 1 (pkg-config-0.24) 3 | # 4 | # Copyright © 2004 Scott James Remnant . 5 | # 6 | # This program is free software; you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation; either version 2 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, but 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | # General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program; if not, write to the Free Software 18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 | # 20 | # As a special exception to the GNU General Public License, if you 21 | # distribute this file as part of a program that contains a 22 | # configuration script generated by Autoconf, you may include it under 23 | # the same distribution terms that you use for the rest of that program. 24 | 25 | # PKG_PROG_PKG_CONFIG([MIN-VERSION]) 26 | # ---------------------------------- 27 | AC_DEFUN([PKG_PROG_PKG_CONFIG], 28 | [m4_pattern_forbid([^_?PKG_[A-Z_]+$]) 29 | m4_pattern_allow([^PKG_CONFIG(_(PATH|LIBDIR|SYSROOT_DIR|ALLOW_SYSTEM_(CFLAGS|LIBS)))?$]) 30 | m4_pattern_allow([^PKG_CONFIG_(DISABLE_UNINSTALLED|TOP_BUILD_DIR|DEBUG_SPEW)$]) 31 | AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility]) 32 | AC_ARG_VAR([PKG_CONFIG_PATH], [directories to add to pkg-config's search path]) 33 | AC_ARG_VAR([PKG_CONFIG_LIBDIR], [path overriding pkg-config's built-in search path]) 34 | 35 | if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then 36 | AC_PATH_TOOL([PKG_CONFIG], [pkg-config]) 37 | fi 38 | if test -n "$PKG_CONFIG"; then 39 | _pkg_min_version=m4_default([$1], [0.9.0]) 40 | AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version]) 41 | if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then 42 | AC_MSG_RESULT([yes]) 43 | else 44 | AC_MSG_RESULT([no]) 45 | PKG_CONFIG="" 46 | fi 47 | fi[]dnl 48 | ])# PKG_PROG_PKG_CONFIG 49 | 50 | # PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) 51 | # 52 | # Check to see whether a particular set of modules exists. Similar 53 | # to PKG_CHECK_MODULES(), but does not set variables or print errors. 54 | # 55 | # Please remember that m4 expands AC_REQUIRE([PKG_PROG_PKG_CONFIG]) 56 | # only at the first occurence in configure.ac, so if the first place 57 | # it's called might be skipped (such as if it is within an "if", you 58 | # have to call PKG_CHECK_EXISTS manually 59 | # -------------------------------------------------------------- 60 | AC_DEFUN([PKG_CHECK_EXISTS], 61 | [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl 62 | if test -n "$PKG_CONFIG" && \ 63 | AC_RUN_LOG([$PKG_CONFIG --exists --print-errors "$1"]); then 64 | m4_default([$2], [:]) 65 | m4_ifvaln([$3], [else 66 | $3])dnl 67 | fi]) 68 | 69 | # _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES]) 70 | # --------------------------------------------- 71 | m4_define([_PKG_CONFIG], 72 | [if test -n "$$1"; then 73 | pkg_cv_[]$1="$$1" 74 | elif test -n "$PKG_CONFIG"; then 75 | PKG_CHECK_EXISTS([$3], 76 | [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null` 77 | test "x$?" != "x0" && pkg_failed=yes ], 78 | [pkg_failed=yes]) 79 | else 80 | pkg_failed=untried 81 | fi[]dnl 82 | ])# _PKG_CONFIG 83 | 84 | # _PKG_SHORT_ERRORS_SUPPORTED 85 | # ----------------------------- 86 | AC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED], 87 | [AC_REQUIRE([PKG_PROG_PKG_CONFIG]) 88 | if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then 89 | _pkg_short_errors_supported=yes 90 | else 91 | _pkg_short_errors_supported=no 92 | fi[]dnl 93 | ])# _PKG_SHORT_ERRORS_SUPPORTED 94 | 95 | 96 | # PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND], 97 | # [ACTION-IF-NOT-FOUND]) 98 | # 99 | # 100 | # Note that if there is a possibility the first call to 101 | # PKG_CHECK_MODULES might not happen, you should be sure to include an 102 | # explicit call to PKG_PROG_PKG_CONFIG in your configure.ac 103 | # 104 | # 105 | # -------------------------------------------------------------- 106 | AC_DEFUN([PKG_CHECK_MODULES], 107 | [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl 108 | AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl 109 | AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl 110 | 111 | pkg_failed=no 112 | AC_MSG_CHECKING([for $1]) 113 | 114 | _PKG_CONFIG([$1][_CFLAGS], [cflags], [$2]) 115 | _PKG_CONFIG([$1][_LIBS], [libs], [$2]) 116 | 117 | m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS 118 | and $1[]_LIBS to avoid the need to call pkg-config. 119 | See the pkg-config man page for more details.]) 120 | 121 | if test $pkg_failed = yes; then 122 | AC_MSG_RESULT([no]) 123 | _PKG_SHORT_ERRORS_SUPPORTED 124 | if test $_pkg_short_errors_supported = yes; then 125 | $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "$2" 2>&1` 126 | else 127 | $1[]_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "$2" 2>&1` 128 | fi 129 | # Put the nasty error message in config.log where it belongs 130 | echo "$$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD 131 | 132 | m4_default([$4], [AC_MSG_ERROR( 133 | [Package requirements ($2) were not met: 134 | 135 | $$1_PKG_ERRORS 136 | 137 | Consider adjusting the PKG_CONFIG_PATH environment variable if you 138 | installed software in a non-standard prefix. 139 | 140 | _PKG_TEXT])[]dnl 141 | ]) 142 | elif test $pkg_failed = untried; then 143 | AC_MSG_RESULT([no]) 144 | m4_default([$4], [AC_MSG_FAILURE( 145 | [The pkg-config script could not be found or is too old. Make sure it 146 | is in your PATH or set the PKG_CONFIG environment variable to the full 147 | path to pkg-config. 148 | 149 | _PKG_TEXT 150 | 151 | To get pkg-config, see .])[]dnl 152 | ]) 153 | else 154 | $1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS 155 | $1[]_LIBS=$pkg_cv_[]$1[]_LIBS 156 | AC_MSG_RESULT([yes]) 157 | $3 158 | fi[]dnl 159 | ])# PKG_CHECK_MODULES 160 | 161 | 162 | # PKG_INSTALLDIR(DIRECTORY) 163 | # ------------------------- 164 | # Substitutes the variable pkgconfigdir as the location where a module 165 | # should install pkg-config .pc files. By default the directory is 166 | # $libdir/pkgconfig, but the default can be changed by passing 167 | # DIRECTORY. The user can override through the --with-pkgconfigdir 168 | # parameter. 169 | AC_DEFUN([PKG_INSTALLDIR], 170 | [m4_pushdef([pkg_default], [m4_default([$1], ['${libdir}/pkgconfig'])]) 171 | m4_pushdef([pkg_description], 172 | [pkg-config installation directory @<:@]pkg_default[@:>@]) 173 | AC_ARG_WITH([pkgconfigdir], 174 | [AS_HELP_STRING([--with-pkgconfigdir], pkg_description)],, 175 | [with_pkgconfigdir=]pkg_default) 176 | AC_SUBST([pkgconfigdir], [$with_pkgconfigdir]) 177 | m4_popdef([pkg_default]) 178 | m4_popdef([pkg_description]) 179 | ]) dnl PKG_INSTALLDIR 180 | 181 | 182 | # PKG_NOARCH_INSTALLDIR(DIRECTORY) 183 | # ------------------------- 184 | # Substitutes the variable noarch_pkgconfigdir as the location where a 185 | # module should install arch-independent pkg-config .pc files. By 186 | # default the directory is $datadir/pkgconfig, but the default can be 187 | # changed by passing DIRECTORY. The user can override through the 188 | # --with-noarch-pkgconfigdir parameter. 189 | AC_DEFUN([PKG_NOARCH_INSTALLDIR], 190 | [m4_pushdef([pkg_default], [m4_default([$1], ['${datadir}/pkgconfig'])]) 191 | m4_pushdef([pkg_description], 192 | [pkg-config arch-independent installation directory @<:@]pkg_default[@:>@]) 193 | AC_ARG_WITH([noarch-pkgconfigdir], 194 | [AS_HELP_STRING([--with-noarch-pkgconfigdir], pkg_description)],, 195 | [with_noarch_pkgconfigdir=]pkg_default) 196 | AC_SUBST([noarch_pkgconfigdir], [$with_noarch_pkgconfigdir]) 197 | m4_popdef([pkg_default]) 198 | m4_popdef([pkg_description]) 199 | ]) dnl PKG_NOARCH_INSTALLDIR 200 | 201 | 202 | # PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE, 203 | # [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) 204 | # ------------------------------------------- 205 | # Retrieves the value of the pkg-config variable for the given module. 206 | AC_DEFUN([PKG_CHECK_VAR], 207 | [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl 208 | AC_ARG_VAR([$1], [value of $3 for $2, overriding pkg-config])dnl 209 | 210 | _PKG_CONFIG([$1], [variable="][$3]["], [$2]) 211 | AS_VAR_COPY([$1], [pkg_cv_][$1]) 212 | 213 | AS_VAR_IF([$1], [""], [$5], [$4])dnl 214 | ])# PKG_CHECK_VAR 215 | -------------------------------------------------------------------------------- /src/bayes-classifier.c: -------------------------------------------------------------------------------- 1 | /* bayes-classifier.c 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include 20 | 21 | #include "bayes-classifier.h" 22 | #include "bayes-guess.h" 23 | #include "bayes-guess-private.h" 24 | #include "bayes-storage-memory.h" 25 | #include "bayes-tokenizer.h" 26 | 27 | /** 28 | * SECTION:bayes-classifier 29 | * @title: BayesClassifier 30 | * @short_description: Bayesian classifier for textual data. 31 | * 32 | * #BayesClassifier provides a simple API for classifying textual data. 33 | * It can be trained using classified input data and then asked to classify 34 | * input data of unknown classification. 35 | * 36 | * The tokenization of the input data can be changed to suit your desired 37 | * needs. For example, it could be used to train SPAM vs HAM, or perhaps 38 | * even guess if your boyfriend or girlfriend will react negatively to your 39 | * instant message. 40 | */ 41 | 42 | typedef gdouble (*BayesCombiner) (BayesClassifier *classifier, 43 | BayesGuess **guesses, 44 | guint len, 45 | const gchar *name, 46 | gpointer user_data); 47 | 48 | struct _BayesClassifier 49 | { 50 | GObject parent_instance; 51 | BayesStorage *storage; 52 | 53 | BayesTokenizer token_func; 54 | gpointer token_user_data; 55 | GDestroyNotify token_notify; 56 | 57 | BayesCombiner combiner_func; 58 | gpointer combiner_user_data; 59 | GDestroyNotify combiner_notify; 60 | }; 61 | 62 | G_DEFINE_TYPE (BayesClassifier, bayes_classifier, G_TYPE_OBJECT) 63 | 64 | enum { 65 | PROP_0, 66 | PROP_STORAGE, 67 | LAST_PROP 68 | }; 69 | 70 | static GParamSpec *properties [LAST_PROP]; 71 | 72 | static gdouble 73 | bayes_classifier_robinson (BayesClassifier *classifier, 74 | BayesGuess **guesses, 75 | guint len, 76 | const gchar *name, 77 | gpointer user_data) 78 | { 79 | gdouble nth; 80 | gdouble P; 81 | gdouble Q; 82 | gdouble S; 83 | gdouble v; 84 | gdouble w; 85 | gdouble g; 86 | guint i; 87 | 88 | nth = 1.0 / (gdouble)len; 89 | 90 | v = 1.0; 91 | w = 1.0; 92 | 93 | for (i = 0; i < len; i++) 94 | { 95 | g = bayes_guess_get_probability (guesses[i]); 96 | v *= (1.0 - g); 97 | w *= g; 98 | } 99 | 100 | P = 1.0 - pow (v, nth); 101 | Q = 1.0 - pow (w, nth); 102 | S = (P - Q) / (P + Q); 103 | 104 | return (1 + S) / 2.0; 105 | } 106 | 107 | static gchar ** 108 | bayes_classifier_tokenize (BayesClassifier *self, 109 | const gchar *text) 110 | { 111 | g_return_val_if_fail (BAYES_IS_CLASSIFIER (self), NULL); 112 | g_return_val_if_fail (text, NULL); 113 | 114 | return self->token_func (text, self->token_user_data); 115 | } 116 | 117 | BayesClassifier * 118 | bayes_classifier_new (void) 119 | { 120 | return g_object_new (BAYES_TYPE_CLASSIFIER, NULL); 121 | } 122 | 123 | void 124 | bayes_classifier_train (BayesClassifier *self, 125 | const gchar *name, 126 | const gchar *text) 127 | { 128 | gchar **tokens; 129 | guint i; 130 | 131 | g_return_if_fail (BAYES_IS_CLASSIFIER (self)); 132 | g_return_if_fail (name); 133 | g_return_if_fail (text); 134 | 135 | if (NULL != (tokens = bayes_classifier_tokenize (self, text))) 136 | { 137 | for (i = 0; tokens[i]; i++) 138 | bayes_storage_add_token (self->storage, name, tokens [i]); 139 | g_strfreev (tokens); 140 | } 141 | } 142 | 143 | static gint 144 | qsort_guesses (gconstpointer a, 145 | gconstpointer b) 146 | { 147 | const BayesGuess *ag = *(const BayesGuess **)a; 148 | const BayesGuess *bg = *(const BayesGuess **)b; 149 | 150 | return (bg->probability - ag->probability) * 100.0; 151 | } 152 | 153 | static gint 154 | sort_guesses (gconstpointer a, 155 | gconstpointer b) 156 | { 157 | const BayesGuess *ag = (const BayesGuess *)a; 158 | const BayesGuess *bg = (const BayesGuess *)b; 159 | 160 | return (bg->probability - ag->probability) * 100.0; 161 | } 162 | 163 | static gdouble 164 | bayes_classifier_combiner (BayesClassifier *self, 165 | BayesGuess **guesses, 166 | guint len, 167 | const gchar *name) 168 | { 169 | g_return_val_if_fail (BAYES_IS_CLASSIFIER (self), 0.0); 170 | g_return_val_if_fail (guesses, 0.0); 171 | g_return_val_if_fail (len, 0.0); 172 | g_return_val_if_fail (name, 0.0); 173 | 174 | return self->combiner_func (self, guesses, len, name, self->combiner_user_data); 175 | } 176 | 177 | GList * 178 | bayes_classifier_guess (BayesClassifier *self, 179 | const gchar *text) 180 | { 181 | BayesGuess *guess; 182 | GPtrArray *guesses; 183 | gdouble prob; 184 | gchar **tokens; 185 | gchar **names; 186 | GList *ret = NULL; 187 | guint i; 188 | guint j; 189 | 190 | g_return_val_if_fail (BAYES_IS_CLASSIFIER (self), NULL); 191 | g_return_val_if_fail (text, NULL); 192 | 193 | tokens = bayes_classifier_tokenize (self, text); 194 | names = bayes_storage_get_names (self->storage); 195 | 196 | for (i = 0; names[i]; i++) 197 | { 198 | guesses = g_ptr_array_new_with_free_func ((GDestroyNotify)bayes_guess_unref); 199 | 200 | for (j = 0; tokens[j]; j++) 201 | { 202 | prob = bayes_storage_get_token_probability (self->storage, names [i], tokens [j]); 203 | guess = bayes_guess_new (tokens[j], prob); 204 | g_ptr_array_add (guesses, guess); 205 | } 206 | 207 | g_ptr_array_sort (guesses, qsort_guesses); 208 | 209 | if (guesses->len != 0) 210 | { 211 | guess = bayes_guess_new (names[i], 212 | bayes_classifier_combiner (self, 213 | (BayesGuess **)guesses->pdata, 214 | guesses->len, names[i])); 215 | ret = g_list_prepend (ret, guess); 216 | } 217 | 218 | g_ptr_array_unref (guesses); 219 | } 220 | 221 | g_strfreev (names); 222 | g_strfreev (tokens); 223 | 224 | ret = g_list_sort (ret, sort_guesses); 225 | 226 | return ret; 227 | } 228 | 229 | BayesStorage * 230 | bayes_classifier_get_storage (BayesClassifier *self) 231 | { 232 | g_return_val_if_fail (BAYES_IS_CLASSIFIER (self), NULL); 233 | 234 | return self->storage; 235 | } 236 | 237 | void 238 | bayes_classifier_set_storage (BayesClassifier *self, 239 | BayesStorage *storage) 240 | { 241 | g_return_if_fail (BAYES_IS_CLASSIFIER (self)); 242 | g_return_if_fail (!storage || BAYES_IS_STORAGE (storage)); 243 | 244 | if (g_set_object (&self->storage, storage)) 245 | g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_STORAGE]); 246 | } 247 | 248 | void 249 | bayes_classifier_set_tokenizer (BayesClassifier *self, 250 | BayesTokenizer tokenizer, 251 | gpointer user_data, 252 | GDestroyNotify notify) 253 | { 254 | g_return_if_fail (BAYES_IS_CLASSIFIER (self)); 255 | g_return_if_fail (tokenizer || (!user_data && !notify)); 256 | 257 | if (self->token_notify != NULL) 258 | self->token_notify (self->token_user_data); 259 | 260 | self->token_func = tokenizer ? tokenizer : bayes_tokenizer_word; 261 | self->token_user_data = tokenizer ? user_data : NULL; 262 | self->token_notify = tokenizer ? notify : NULL; 263 | } 264 | 265 | static void 266 | bayes_classifier_set_combiner (BayesClassifier *self, 267 | BayesCombiner combiner, 268 | gpointer user_data, 269 | GDestroyNotify notify) 270 | { 271 | g_return_if_fail (BAYES_IS_CLASSIFIER (self)); 272 | g_return_if_fail (combiner || (!user_data && !notify)); 273 | 274 | if (self->combiner_notify != NULL) 275 | self->combiner_notify (self->combiner_user_data); 276 | 277 | self->combiner_func = combiner ? combiner : bayes_classifier_robinson; 278 | self->combiner_user_data = combiner ? user_data : NULL; 279 | self->combiner_notify = combiner ? notify : NULL; 280 | } 281 | 282 | static void 283 | bayes_classifier_finalize (GObject *object) 284 | { 285 | BayesClassifier *self = (BayesClassifier *)object; 286 | 287 | bayes_classifier_set_tokenizer (self, NULL, NULL, NULL); 288 | bayes_classifier_set_combiner (self, NULL, NULL, NULL); 289 | g_clear_object (&self->storage); 290 | 291 | G_OBJECT_CLASS (bayes_classifier_parent_class)->finalize (object); 292 | } 293 | 294 | static void 295 | bayes_classifier_get_property (GObject *object, 296 | guint prop_id, 297 | GValue *value, 298 | GParamSpec *pspec) 299 | { 300 | BayesClassifier *self = BAYES_CLASSIFIER (object); 301 | 302 | switch (prop_id) 303 | { 304 | case PROP_STORAGE: 305 | g_value_set_object (value, bayes_classifier_get_storage (self)); 306 | break; 307 | 308 | default: 309 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 310 | } 311 | } 312 | 313 | static void 314 | bayes_classifier_set_property (GObject *object, 315 | guint prop_id, 316 | const GValue *value, 317 | GParamSpec *pspec) 318 | { 319 | BayesClassifier *self = BAYES_CLASSIFIER (object); 320 | 321 | switch (prop_id) 322 | { 323 | case PROP_STORAGE: 324 | bayes_classifier_set_storage (self, g_value_get_object (value)); 325 | break; 326 | 327 | default: 328 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 329 | } 330 | } 331 | 332 | static void 333 | bayes_classifier_class_init (BayesClassifierClass *klass) 334 | { 335 | GObjectClass *object_class; 336 | 337 | object_class = G_OBJECT_CLASS (klass); 338 | object_class->finalize = bayes_classifier_finalize; 339 | object_class->get_property = bayes_classifier_get_property; 340 | object_class->set_property = bayes_classifier_set_property; 341 | 342 | /** 343 | * BayesClassifier:storage: 344 | * 345 | * The "storage" property. The training data used by @classifier is 346 | * saved to and loaded from @storage. 347 | */ 348 | properties [PROP_STORAGE] = 349 | g_param_spec_object ("storage", 350 | "Storage", 351 | "The storage to use for tokens.", 352 | BAYES_TYPE_STORAGE, 353 | G_PARAM_READWRITE); 354 | 355 | g_object_class_install_properties (object_class, LAST_PROP, properties); 356 | } 357 | 358 | static void 359 | bayes_classifier_init (BayesClassifier *self) 360 | { 361 | bayes_classifier_set_tokenizer (self, NULL, NULL, NULL); 362 | bayes_classifier_set_combiner (self, NULL, NULL, NULL); 363 | bayes_classifier_set_storage (self, NULL); 364 | } 365 | -------------------------------------------------------------------------------- /git.mk: -------------------------------------------------------------------------------- 1 | # git.mk, a small Makefile to autogenerate .gitignore files 2 | # for autotools-based projects. 3 | # 4 | # Copyright 2009, Red Hat, Inc. 5 | # Copyright 2010,2011,2012,2013 Behdad Esfahbod 6 | # Written by Behdad Esfahbod 7 | # 8 | # Copying and distribution of this file, with or without modification, 9 | # is permitted in any medium without royalty provided the copyright 10 | # notice and this notice are preserved. 11 | # 12 | # The latest version of this file can be downloaded from: 13 | GIT_MK_URL = https://raw.githubusercontent.com/behdad/git.mk/master/git.mk 14 | # 15 | # Bugs, etc, should be reported upstream at: 16 | # https://github.com/behdad/git.mk 17 | # 18 | # To use in your project, import this file in your git repo's toplevel, 19 | # then do "make -f git.mk". This modifies all Makefile.am files in 20 | # your project to -include git.mk. Remember to add that line to new 21 | # Makefile.am files you create in your project, or just rerun the 22 | # "make -f git.mk". 23 | # 24 | # This enables automatic .gitignore generation. If you need to ignore 25 | # more files, add them to the GITIGNOREFILES variable in your Makefile.am. 26 | # But think twice before doing that. If a file has to be in .gitignore, 27 | # chances are very high that it's a generated file and should be in one 28 | # of MOSTLYCLEANFILES, CLEANFILES, DISTCLEANFILES, or MAINTAINERCLEANFILES. 29 | # 30 | # The only case that you need to manually add a file to GITIGNOREFILES is 31 | # when remove files in one of mostlyclean-local, clean-local, distclean-local, 32 | # or maintainer-clean-local make targets. 33 | # 34 | # Note that for files like editor backup, etc, there are better places to 35 | # ignore them. See "man gitignore". 36 | # 37 | # If "make maintainer-clean" removes the files but they are not recognized 38 | # by this script (that is, if "git status" shows untracked files still), send 39 | # me the output of "git status" as well as your Makefile.am and Makefile for 40 | # the directories involved and I'll diagnose. 41 | # 42 | # For a list of toplevel files that should be in MAINTAINERCLEANFILES, see 43 | # Makefile.am.sample in the git.mk git repo. 44 | # 45 | # Don't EXTRA_DIST this file. It is supposed to only live in git clones, 46 | # not tarballs. It serves no useful purpose in tarballs and clutters the 47 | # build dir. 48 | # 49 | # This file knows how to handle autoconf, automake, libtool, gtk-doc, 50 | # gnome-doc-utils, yelp.m4, mallard, intltool, gsettings, dejagnu, appdata, 51 | # appstream. 52 | # 53 | # This makefile provides the following targets: 54 | # 55 | # - all: "make all" will build all gitignore files. 56 | # - gitignore: makes all gitignore files in the current dir and subdirs. 57 | # - .gitignore: make gitignore file for the current dir. 58 | # - gitignore-recurse: makes all gitignore files in the subdirs. 59 | # 60 | # KNOWN ISSUES: 61 | # 62 | # - Recursive configure doesn't work as $(top_srcdir)/git.mk inside the 63 | # submodule doesn't find us. If you have configure.{in,ac} files in 64 | # subdirs, add a proxy git.mk file in those dirs that simply does: 65 | # "include $(top_srcdir)/../git.mk". Add more ..'s to your taste. 66 | # And add those files to git. See vte/gnome-pty-helper/git.mk for 67 | # example. 68 | # 69 | 70 | 71 | 72 | ############################################################################### 73 | # Variables user modules may want to add to toplevel MAINTAINERCLEANFILES: 74 | ############################################################################### 75 | 76 | # 77 | # Most autotools-using modules should be fine including this variable in their 78 | # toplevel MAINTAINERCLEANFILES: 79 | GITIGNORE_MAINTAINERCLEANFILES_TOPLEVEL = \ 80 | $(srcdir)/aclocal.m4 \ 81 | $(srcdir)/autoscan.log \ 82 | $(srcdir)/configure.scan \ 83 | `AUX_DIR=$(srcdir)/$$(cd $(top_srcdir); $(AUTOCONF) --trace 'AC_CONFIG_AUX_DIR:$$1' ./configure.ac); \ 84 | test "x$$AUX_DIR" = "x$(srcdir)/" && AUX_DIR=$(srcdir); \ 85 | for x in \ 86 | ar-lib \ 87 | compile \ 88 | config.guess \ 89 | config.sub \ 90 | depcomp \ 91 | install-sh \ 92 | ltmain.sh \ 93 | missing \ 94 | mkinstalldirs \ 95 | test-driver \ 96 | ylwrap \ 97 | ; do echo "$$AUX_DIR/$$x"; done` \ 98 | `cd $(top_srcdir); $(AUTOCONF) --trace 'AC_CONFIG_HEADERS:$$1' ./configure.ac | \ 99 | head -n 1 | while read f; do echo "$(srcdir)/$$f.in"; done` 100 | # 101 | # All modules should also be fine including the following variable, which 102 | # removes automake-generated Makefile.in files: 103 | GITIGNORE_MAINTAINERCLEANFILES_MAKEFILE_IN = \ 104 | `cd $(top_srcdir); $(AUTOCONF) --trace 'AC_CONFIG_FILES:$$1' ./configure.ac | \ 105 | while read f; do \ 106 | case $$f in Makefile|*/Makefile) \ 107 | test -f "$(srcdir)/$$f.am" && echo "$(srcdir)/$$f.in";; esac; \ 108 | done` 109 | # 110 | # Modules that use libtool and use AC_CONFIG_MACRO_DIR() may also include this, 111 | # though it's harmless to include regardless. 112 | GITIGNORE_MAINTAINERCLEANFILES_M4_LIBTOOL = \ 113 | `MACRO_DIR=$(srcdir)/$$(cd $(top_srcdir); $(AUTOCONF) --trace 'AC_CONFIG_MACRO_DIR:$$1' ./configure.ac); \ 114 | if test "x$$MACRO_DIR" != "x$(srcdir)/"; then \ 115 | for x in \ 116 | libtool.m4 \ 117 | ltoptions.m4 \ 118 | ltsugar.m4 \ 119 | ltversion.m4 \ 120 | lt~obsolete.m4 \ 121 | ; do echo "$$MACRO_DIR/$$x"; done; \ 122 | fi` 123 | 124 | 125 | 126 | ############################################################################### 127 | # Default rule is to install ourselves in all Makefile.am files: 128 | ############################################################################### 129 | 130 | git-all: git-mk-install 131 | 132 | git-mk-install: 133 | @echo "Installing git makefile" 134 | @any_failed=; \ 135 | find "`test -z "$(top_srcdir)" && echo . || echo "$(top_srcdir)"`" -name Makefile.am | while read x; do \ 136 | if grep 'include .*/git.mk' $$x >/dev/null; then \ 137 | echo "$$x already includes git.mk"; \ 138 | else \ 139 | failed=; \ 140 | echo "Updating $$x"; \ 141 | { cat $$x; \ 142 | echo ''; \ 143 | echo '-include $$(top_srcdir)/git.mk'; \ 144 | } > $$x.tmp || failed=1; \ 145 | if test x$$failed = x; then \ 146 | mv $$x.tmp $$x || failed=1; \ 147 | fi; \ 148 | if test x$$failed = x; then : else \ 149 | echo "Failed updating $$x"; >&2 \ 150 | any_failed=1; \ 151 | fi; \ 152 | fi; done; test -z "$$any_failed" 153 | 154 | git-mk-update: 155 | wget $(GIT_MK_URL) -O $(top_srcdir)/git.mk 156 | 157 | .PHONY: git-all git-mk-install git-mk-update 158 | 159 | 160 | 161 | ############################################################################### 162 | # Actual .gitignore generation: 163 | ############################################################################### 164 | 165 | $(srcdir)/.gitignore: Makefile.am $(top_srcdir)/git.mk 166 | @echo "git.mk: Generating $@" 167 | @{ \ 168 | if test "x$(DOC_MODULE)" = x -o "x$(DOC_MAIN_SGML_FILE)" = x; then :; else \ 169 | for x in \ 170 | $(DOC_MODULE)-decl-list.txt \ 171 | $(DOC_MODULE)-decl.txt \ 172 | tmpl/$(DOC_MODULE)-unused.sgml \ 173 | "tmpl/*.bak" \ 174 | xml html \ 175 | ; do echo "/$$x"; done; \ 176 | FLAVOR=$$(cd $(top_srcdir); $(AUTOCONF) --trace 'GTK_DOC_CHECK:$$2' ./configure.ac); \ 177 | case $$FLAVOR in *no-tmpl*) echo /tmpl;; esac; \ 178 | fi; \ 179 | if test "x$(DOC_MODULE)$(DOC_ID)" = x -o "x$(DOC_LINGUAS)" = x; then :; else \ 180 | for lc in $(DOC_LINGUAS); do \ 181 | for x in \ 182 | $(if $(DOC_MODULE),$(DOC_MODULE).xml) \ 183 | $(DOC_PAGES) \ 184 | $(DOC_INCLUDES) \ 185 | ; do echo "/$$lc/$$x"; done; \ 186 | done; \ 187 | for x in \ 188 | $(_DOC_OMF_ALL) \ 189 | $(_DOC_DSK_ALL) \ 190 | $(_DOC_HTML_ALL) \ 191 | $(_DOC_MOFILES) \ 192 | $(DOC_H_FILE) \ 193 | "*/.xml2po.mo" \ 194 | "*/*.omf.out" \ 195 | ; do echo /$$x; done; \ 196 | fi; \ 197 | if test "x$(HELP_ID)" = x -o "x$(HELP_LINGUAS)" = x; then :; else \ 198 | for lc in $(HELP_LINGUAS); do \ 199 | for x in \ 200 | $(HELP_FILES) \ 201 | "$$lc.stamp" \ 202 | "$$lc.mo" \ 203 | ; do echo "/$$lc/$$x"; done; \ 204 | done; \ 205 | fi; \ 206 | if test "x$(gsettings_SCHEMAS)" = x; then :; else \ 207 | for x in \ 208 | $(gsettings_SCHEMAS:.xml=.valid) \ 209 | $(gsettings__enum_file) \ 210 | ; do echo "/$$x"; done; \ 211 | fi; \ 212 | if test "x$(appdata_XML)" = x; then :; else \ 213 | for x in \ 214 | $(appdata_XML:.xml=.valid) \ 215 | ; do echo "/$$x"; done; \ 216 | fi; \ 217 | if test "x$(appstream_XML)" = x; then :; else \ 218 | for x in \ 219 | $(appstream_XML:.xml=.valid) \ 220 | ; do echo "/$$x"; done; \ 221 | fi; \ 222 | if test -f $(srcdir)/po/Makefile.in.in; then \ 223 | for x in \ 224 | po/Makefile.in.in \ 225 | po/Makefile.in.in~ \ 226 | po/Makefile.in \ 227 | po/Makefile \ 228 | po/Makevars.template \ 229 | po/POTFILES \ 230 | po/Rules-quot \ 231 | po/stamp-it \ 232 | po/.intltool-merge-cache \ 233 | "po/*.gmo" \ 234 | "po/*.header" \ 235 | "po/*.mo" \ 236 | "po/*.sed" \ 237 | "po/*.sin" \ 238 | po/$(GETTEXT_PACKAGE).pot \ 239 | intltool-extract.in \ 240 | intltool-merge.in \ 241 | intltool-update.in \ 242 | ; do echo "/$$x"; done; \ 243 | fi; \ 244 | if test -f $(srcdir)/configure; then \ 245 | for x in \ 246 | autom4te.cache \ 247 | configure \ 248 | config.h \ 249 | stamp-h1 \ 250 | libtool \ 251 | config.lt \ 252 | ; do echo "/$$x"; done; \ 253 | fi; \ 254 | if test "x$(DEJATOOL)" = x; then :; else \ 255 | for x in \ 256 | $(DEJATOOL) \ 257 | ; do echo "/$$x.sum"; echo "/$$x.log"; done; \ 258 | echo /site.exp; \ 259 | fi; \ 260 | if test "x$(am__dirstamp)" = x; then :; else \ 261 | echo "$(am__dirstamp)"; \ 262 | fi; \ 263 | if test "x$(LTCOMPILE)" = x -a "x$(LTCXXCOMPILE)" = x -a "x$(GTKDOC_RUN)" = x; then :; else \ 264 | for x in \ 265 | "*.lo" \ 266 | ".libs" "_libs" \ 267 | ; do echo "$$x"; done; \ 268 | fi; \ 269 | for x in \ 270 | .gitignore \ 271 | $(GITIGNOREFILES) \ 272 | $(CLEANFILES) \ 273 | $(PROGRAMS) $(check_PROGRAMS) $(EXTRA_PROGRAMS) \ 274 | $(LIBRARIES) $(check_LIBRARIES) $(EXTRA_LIBRARIES) \ 275 | $(LTLIBRARIES) $(check_LTLIBRARIES) $(EXTRA_LTLIBRARIES) \ 276 | so_locations \ 277 | $(MOSTLYCLEANFILES) \ 278 | $(TEST_LOGS) \ 279 | $(TEST_LOGS:.log=.trs) \ 280 | $(TEST_SUITE_LOG) \ 281 | $(TESTS:=.test) \ 282 | "*.gcda" \ 283 | "*.gcno" \ 284 | $(DISTCLEANFILES) \ 285 | $(am__CONFIG_DISTCLEAN_FILES) \ 286 | $(CONFIG_CLEAN_FILES) \ 287 | TAGS ID GTAGS GRTAGS GSYMS GPATH tags \ 288 | "*.tab.c" \ 289 | $(MAINTAINERCLEANFILES) \ 290 | $(BUILT_SOURCES) \ 291 | $(patsubst %.vala,%.c,$(filter %.vala,$(SOURCES))) \ 292 | $(filter %_vala.stamp,$(DIST_COMMON)) \ 293 | $(filter %.vapi,$(DIST_COMMON)) \ 294 | $(filter $(addprefix %,$(notdir $(patsubst %.vapi,%.h,$(filter %.vapi,$(DIST_COMMON))))),$(DIST_COMMON)) \ 295 | Makefile \ 296 | Makefile.in \ 297 | "*.orig" \ 298 | "*.rej" \ 299 | "*.bak" \ 300 | "*~" \ 301 | ".*.sw[nop]" \ 302 | ".dirstamp" \ 303 | ; do echo "/$$x"; done; \ 304 | for x in \ 305 | "*.$(OBJEXT)" \ 306 | $(DEPDIR) \ 307 | ; do echo "$$x"; done; \ 308 | } | \ 309 | sed "s@^/`echo "$(srcdir)" | sed 's/\(.\)/[\1]/g'`/@/@" | \ 310 | sed 's@/[.]/@/@g' | \ 311 | LC_ALL=C sort | uniq > $@.tmp && \ 312 | mv $@.tmp $@; 313 | 314 | all: $(srcdir)/.gitignore gitignore-recurse-maybe 315 | gitignore: $(srcdir)/.gitignore gitignore-recurse 316 | 317 | gitignore-recurse-maybe: 318 | @for subdir in $(DIST_SUBDIRS); do \ 319 | case " $(SUBDIRS) " in \ 320 | *" $$subdir "*) :;; \ 321 | *) test "$$subdir" = . -o -e "$$subdir/.git" || (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) gitignore || echo "Skipping $$subdir");; \ 322 | esac; \ 323 | done 324 | gitignore-recurse: 325 | @for subdir in $(DIST_SUBDIRS); do \ 326 | test "$$subdir" = . -o -e "$$subdir/.git" || (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) gitignore || echo "Skipping $$subdir"); \ 327 | done 328 | 329 | maintainer-clean: gitignore-clean 330 | gitignore-clean: 331 | -rm -f $(srcdir)/.gitignore 332 | 333 | .PHONY: gitignore-clean gitignore gitignore-recurse gitignore-recurse-maybe 334 | -------------------------------------------------------------------------------- /m4/intltool.m4: -------------------------------------------------------------------------------- 1 | ## intltool.m4 - Configure intltool for the target system. -*-Shell-script-*- 2 | ## Copyright (C) 2001 Eazel, Inc. 3 | ## Author: Maciej Stachowiak 4 | ## Kenneth Christiansen 5 | ## 6 | ## This program is free software; you can redistribute it and/or modify 7 | ## it under the terms of the GNU General Public License as published by 8 | ## the Free Software Foundation; either version 2 of the License, or 9 | ## (at your option) any later version. 10 | ## 11 | ## This program is distributed in the hope that it will be useful, but 12 | ## WITHOUT ANY WARRANTY; without even the implied warranty of 13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | ## General Public License for more details. 15 | ## 16 | ## You should have received a copy of the GNU General Public License 17 | ## along with this program; if not, write to the Free Software 18 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 | ## 20 | ## As a special exception to the GNU General Public License, if you 21 | ## distribute this file as part of a program that contains a 22 | ## configuration script generated by Autoconf, you may include it under 23 | ## the same distribution terms that you use for the rest of that program. 24 | 25 | dnl IT_PROG_INTLTOOL([MINIMUM-VERSION], [no-xml]) 26 | # serial 42 IT_PROG_INTLTOOL 27 | AC_DEFUN([IT_PROG_INTLTOOL], [ 28 | AC_PREREQ([2.50])dnl 29 | AC_REQUIRE([AM_NLS])dnl 30 | 31 | case "$am__api_version" in 32 | 1.[01234]) 33 | AC_MSG_ERROR([Automake 1.5 or newer is required to use intltool]) 34 | ;; 35 | *) 36 | ;; 37 | esac 38 | 39 | INTLTOOL_REQUIRED_VERSION_AS_INT=`echo $1 | awk -F. '{ print $ 1 * 1000 + $ 2 * 100 + $ 3; }'` 40 | INTLTOOL_APPLIED_VERSION=`intltool-update --version | head -1 | cut -d" " -f3` 41 | INTLTOOL_APPLIED_VERSION_AS_INT=`echo $INTLTOOL_APPLIED_VERSION | awk -F. '{ print $ 1 * 1000 + $ 2 * 100 + $ 3; }'` 42 | if test -n "$1"; then 43 | AC_MSG_CHECKING([for intltool >= $1]) 44 | AC_MSG_RESULT([$INTLTOOL_APPLIED_VERSION found]) 45 | test "$INTLTOOL_APPLIED_VERSION_AS_INT" -ge "$INTLTOOL_REQUIRED_VERSION_AS_INT" || 46 | AC_MSG_ERROR([Your intltool is too old. You need intltool $1 or later.]) 47 | fi 48 | 49 | AC_PATH_PROG(INTLTOOL_UPDATE, [intltool-update]) 50 | AC_PATH_PROG(INTLTOOL_MERGE, [intltool-merge]) 51 | AC_PATH_PROG(INTLTOOL_EXTRACT, [intltool-extract]) 52 | if test -z "$INTLTOOL_UPDATE" -o -z "$INTLTOOL_MERGE" -o -z "$INTLTOOL_EXTRACT"; then 53 | AC_MSG_ERROR([The intltool scripts were not found. Please install intltool.]) 54 | fi 55 | 56 | if test -z "$AM_DEFAULT_VERBOSITY"; then 57 | AM_DEFAULT_VERBOSITY=1 58 | fi 59 | AC_SUBST([AM_DEFAULT_VERBOSITY]) 60 | 61 | INTLTOOL_V_MERGE='$(INTLTOOL__v_MERGE_$(V))' 62 | INTLTOOL__v_MERGE_='$(INTLTOOL__v_MERGE_$(AM_DEFAULT_VERBOSITY))' 63 | INTLTOOL__v_MERGE_0='@echo " ITMRG " [$]@;' 64 | AC_SUBST(INTLTOOL_V_MERGE) 65 | AC_SUBST(INTLTOOL__v_MERGE_) 66 | AC_SUBST(INTLTOOL__v_MERGE_0) 67 | 68 | INTLTOOL_V_MERGE_OPTIONS='$(intltool__v_merge_options_$(V))' 69 | intltool__v_merge_options_='$(intltool__v_merge_options_$(AM_DEFAULT_VERBOSITY))' 70 | intltool__v_merge_options_0='-q' 71 | AC_SUBST(INTLTOOL_V_MERGE_OPTIONS) 72 | AC_SUBST(intltool__v_merge_options_) 73 | AC_SUBST(intltool__v_merge_options_0) 74 | 75 | INTLTOOL_DESKTOP_RULE='%.desktop: %.desktop.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 76 | INTLTOOL_DIRECTORY_RULE='%.directory: %.directory.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 77 | INTLTOOL_KEYS_RULE='%.keys: %.keys.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -k -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 78 | INTLTOOL_PROP_RULE='%.prop: %.prop.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 79 | INTLTOOL_OAF_RULE='%.oaf: %.oaf.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -o -p $(top_srcdir)/po $< [$]@' 80 | INTLTOOL_PONG_RULE='%.pong: %.pong.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 81 | INTLTOOL_SERVER_RULE='%.server: %.server.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -o -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 82 | INTLTOOL_SHEET_RULE='%.sheet: %.sheet.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 83 | INTLTOOL_SOUNDLIST_RULE='%.soundlist: %.soundlist.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 84 | INTLTOOL_UI_RULE='%.ui: %.ui.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 85 | INTLTOOL_XML_RULE='%.xml: %.xml.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 86 | if test "$INTLTOOL_APPLIED_VERSION_AS_INT" -ge 5000; then 87 | INTLTOOL_XML_NOMERGE_RULE='%.xml: %.xml.in $(INTLTOOL_MERGE) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u --no-translations $< [$]@' 88 | else 89 | INTLTOOL_XML_NOMERGE_RULE='%.xml: %.xml.in $(INTLTOOL_MERGE) ; $(INTLTOOL_V_MERGE)_it_tmp_dir=tmp.intltool.[$][$]RANDOM && mkdir [$][$]_it_tmp_dir && LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u [$][$]_it_tmp_dir $< [$]@ && rmdir [$][$]_it_tmp_dir' 90 | fi 91 | INTLTOOL_XAM_RULE='%.xam: %.xml.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 92 | INTLTOOL_KBD_RULE='%.kbd: %.kbd.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -m -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 93 | INTLTOOL_CAVES_RULE='%.caves: %.caves.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 94 | INTLTOOL_SCHEMAS_RULE='%.schemas: %.schemas.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -s -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 95 | INTLTOOL_THEME_RULE='%.theme: %.theme.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 96 | INTLTOOL_SERVICE_RULE='%.service: %.service.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -d -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 97 | INTLTOOL_POLICY_RULE='%.policy: %.policy.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*.po) ; $(INTLTOOL_V_MERGE)LC_ALL=C $(INTLTOOL_MERGE) $(INTLTOOL_V_MERGE_OPTIONS) -x -u -c $(top_builddir)/po/.intltool-merge-cache $(top_srcdir)/po $< [$]@' 98 | 99 | _IT_SUBST(INTLTOOL_DESKTOP_RULE) 100 | _IT_SUBST(INTLTOOL_DIRECTORY_RULE) 101 | _IT_SUBST(INTLTOOL_KEYS_RULE) 102 | _IT_SUBST(INTLTOOL_PROP_RULE) 103 | _IT_SUBST(INTLTOOL_OAF_RULE) 104 | _IT_SUBST(INTLTOOL_PONG_RULE) 105 | _IT_SUBST(INTLTOOL_SERVER_RULE) 106 | _IT_SUBST(INTLTOOL_SHEET_RULE) 107 | _IT_SUBST(INTLTOOL_SOUNDLIST_RULE) 108 | _IT_SUBST(INTLTOOL_UI_RULE) 109 | _IT_SUBST(INTLTOOL_XAM_RULE) 110 | _IT_SUBST(INTLTOOL_KBD_RULE) 111 | _IT_SUBST(INTLTOOL_XML_RULE) 112 | _IT_SUBST(INTLTOOL_XML_NOMERGE_RULE) 113 | _IT_SUBST(INTLTOOL_CAVES_RULE) 114 | _IT_SUBST(INTLTOOL_SCHEMAS_RULE) 115 | _IT_SUBST(INTLTOOL_THEME_RULE) 116 | _IT_SUBST(INTLTOOL_SERVICE_RULE) 117 | _IT_SUBST(INTLTOOL_POLICY_RULE) 118 | 119 | # Check the gettext tools to make sure they are GNU 120 | AC_PATH_PROG(XGETTEXT, xgettext) 121 | AC_PATH_PROG(MSGMERGE, msgmerge) 122 | AC_PATH_PROG(MSGFMT, msgfmt) 123 | AC_PATH_PROG(GMSGFMT, gmsgfmt, $MSGFMT) 124 | if test -z "$XGETTEXT" -o -z "$MSGMERGE" -o -z "$MSGFMT"; then 125 | AC_MSG_ERROR([GNU gettext tools not found; required for intltool]) 126 | fi 127 | xgversion="`$XGETTEXT --version|grep '(GNU ' 2> /dev/null`" 128 | mmversion="`$MSGMERGE --version|grep '(GNU ' 2> /dev/null`" 129 | mfversion="`$MSGFMT --version|grep '(GNU ' 2> /dev/null`" 130 | if test -z "$xgversion" -o -z "$mmversion" -o -z "$mfversion"; then 131 | AC_MSG_ERROR([GNU gettext tools not found; required for intltool]) 132 | fi 133 | 134 | AC_PATH_PROG(INTLTOOL_PERL, perl) 135 | if test -z "$INTLTOOL_PERL"; then 136 | AC_MSG_ERROR([perl not found]) 137 | fi 138 | AC_MSG_CHECKING([for perl >= 5.8.1]) 139 | $INTLTOOL_PERL -e "use 5.8.1;" > /dev/null 2>&1 140 | if test $? -ne 0; then 141 | AC_MSG_ERROR([perl 5.8.1 is required for intltool]) 142 | else 143 | IT_PERL_VERSION=`$INTLTOOL_PERL -e "printf '%vd', $^V"` 144 | AC_MSG_RESULT([$IT_PERL_VERSION]) 145 | fi 146 | if test "x$2" != "xno-xml"; then 147 | AC_MSG_CHECKING([for XML::Parser]) 148 | if `$INTLTOOL_PERL -e "require XML::Parser" 2>/dev/null`; then 149 | AC_MSG_RESULT([ok]) 150 | else 151 | AC_MSG_ERROR([XML::Parser perl module is required for intltool]) 152 | fi 153 | fi 154 | 155 | # Substitute ALL_LINGUAS so we can use it in po/Makefile 156 | AC_SUBST(ALL_LINGUAS) 157 | 158 | IT_PO_SUBDIR([po]) 159 | 160 | ]) 161 | 162 | 163 | # IT_PO_SUBDIR(DIRNAME) 164 | # --------------------- 165 | # All po subdirs have to be declared with this macro; the subdir "po" is 166 | # declared by IT_PROG_INTLTOOL. 167 | # 168 | AC_DEFUN([IT_PO_SUBDIR], 169 | [AC_PREREQ([2.53])dnl We use ac_top_srcdir inside AC_CONFIG_COMMANDS. 170 | dnl 171 | dnl The following CONFIG_COMMANDS should be executed at the very end 172 | dnl of config.status. 173 | AC_CONFIG_COMMANDS_PRE([ 174 | AC_CONFIG_COMMANDS([$1/stamp-it], [ 175 | if [ ! grep "^# INTLTOOL_MAKEFILE$" "$1/Makefile.in" > /dev/null ]; then 176 | AC_MSG_ERROR([$1/Makefile.in.in was not created by intltoolize.]) 177 | fi 178 | rm -f "$1/stamp-it" "$1/stamp-it.tmp" "$1/POTFILES" "$1/Makefile.tmp" 179 | >"$1/stamp-it.tmp" 180 | [sed '/^#/d 181 | s/^[[].*] *// 182 | /^[ ]*$/d 183 | '"s|^| $ac_top_srcdir/|" \ 184 | "$srcdir/$1/POTFILES.in" | sed '$!s/$/ \\/' >"$1/POTFILES" 185 | ] 186 | [sed '/^POTFILES =/,/[^\\]$/ { 187 | /^POTFILES =/!d 188 | r $1/POTFILES 189 | } 190 | ' "$1/Makefile.in" >"$1/Makefile"] 191 | rm -f "$1/Makefile.tmp" 192 | mv "$1/stamp-it.tmp" "$1/stamp-it" 193 | ]) 194 | ])dnl 195 | ]) 196 | 197 | # _IT_SUBST(VARIABLE) 198 | # ------------------- 199 | # Abstract macro to do either _AM_SUBST_NOTMAKE or AC_SUBST 200 | # 201 | AC_DEFUN([_IT_SUBST], 202 | [ 203 | AC_SUBST([$1]) 204 | m4_ifdef([_AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE([$1])]) 205 | ] 206 | ) 207 | 208 | # deprecated macros 209 | AU_ALIAS([AC_PROG_INTLTOOL], [IT_PROG_INTLTOOL]) 210 | # A hint is needed for aclocal from Automake <= 1.9.4: 211 | # AC_DEFUN([AC_PROG_INTLTOOL], ...) 212 | 213 | -------------------------------------------------------------------------------- /m4/glib-gettext.m4: -------------------------------------------------------------------------------- 1 | # Copyright (C) 1995-2002 Free Software Foundation, Inc. 2 | # Copyright (C) 2001-2003,2004 Red Hat, Inc. 3 | # 4 | # This file is free software, distributed under the terms of the GNU 5 | # General Public License. As a special exception to the GNU General 6 | # Public License, this file may be distributed as part of a program 7 | # that contains a configuration script generated by Autoconf, under 8 | # the same distribution terms as the rest of that program. 9 | # 10 | # This file can be copied and used freely without restrictions. It can 11 | # be used in projects which are not available under the GNU Public License 12 | # but which still want to provide support for the GNU gettext functionality. 13 | # 14 | # Macro to add for using GNU gettext. 15 | # Ulrich Drepper , 1995, 1996 16 | # 17 | # Modified to never use included libintl. 18 | # Owen Taylor , 12/15/1998 19 | # 20 | # Major rework to remove unused code 21 | # Owen Taylor , 12/11/2002 22 | # 23 | # Added better handling of ALL_LINGUAS from GNU gettext version 24 | # written by Bruno Haible, Owen Taylor 5/30/3002 25 | # 26 | # Modified to require ngettext 27 | # Matthias Clasen 08/06/2004 28 | # 29 | # We need this here as well, since someone might use autoconf-2.5x 30 | # to configure GLib then an older version to configure a package 31 | # using AM_GLIB_GNU_GETTEXT 32 | AC_PREREQ(2.53) 33 | 34 | dnl 35 | dnl We go to great lengths to make sure that aclocal won't 36 | dnl try to pull in the installed version of these macros 37 | dnl when running aclocal in the glib directory. 38 | dnl 39 | m4_copy([AC_DEFUN],[glib_DEFUN]) 40 | m4_copy([AC_REQUIRE],[glib_REQUIRE]) 41 | dnl 42 | dnl At the end, if we're not within glib, we'll define the public 43 | dnl definitions in terms of our private definitions. 44 | dnl 45 | 46 | # GLIB_LC_MESSAGES 47 | #-------------------- 48 | glib_DEFUN([GLIB_LC_MESSAGES], 49 | [AC_CHECK_HEADERS([locale.h]) 50 | if test $ac_cv_header_locale_h = yes; then 51 | AC_CACHE_CHECK([for LC_MESSAGES], am_cv_val_LC_MESSAGES, 52 | [AC_TRY_LINK([#include ], [return LC_MESSAGES], 53 | am_cv_val_LC_MESSAGES=yes, am_cv_val_LC_MESSAGES=no)]) 54 | if test $am_cv_val_LC_MESSAGES = yes; then 55 | AC_DEFINE(HAVE_LC_MESSAGES, 1, 56 | [Define if your file defines LC_MESSAGES.]) 57 | fi 58 | fi]) 59 | 60 | # GLIB_PATH_PROG_WITH_TEST 61 | #---------------------------- 62 | dnl GLIB_PATH_PROG_WITH_TEST(VARIABLE, PROG-TO-CHECK-FOR, 63 | dnl TEST-PERFORMED-ON-FOUND_PROGRAM [, VALUE-IF-NOT-FOUND [, PATH]]) 64 | glib_DEFUN([GLIB_PATH_PROG_WITH_TEST], 65 | [# Extract the first word of "$2", so it can be a program name with args. 66 | set dummy $2; ac_word=[$]2 67 | AC_MSG_CHECKING([for $ac_word]) 68 | AC_CACHE_VAL(ac_cv_path_$1, 69 | [case "[$]$1" in 70 | /*) 71 | ac_cv_path_$1="[$]$1" # Let the user override the test with a path. 72 | ;; 73 | *) 74 | IFS="${IFS= }"; ac_save_ifs="$IFS"; IFS="${IFS}:" 75 | for ac_dir in ifelse([$5], , $PATH, [$5]); do 76 | test -z "$ac_dir" && ac_dir=. 77 | if test -f $ac_dir/$ac_word; then 78 | if [$3]; then 79 | ac_cv_path_$1="$ac_dir/$ac_word" 80 | break 81 | fi 82 | fi 83 | done 84 | IFS="$ac_save_ifs" 85 | dnl If no 4th arg is given, leave the cache variable unset, 86 | dnl so AC_PATH_PROGS will keep looking. 87 | ifelse([$4], , , [ test -z "[$]ac_cv_path_$1" && ac_cv_path_$1="$4" 88 | ])dnl 89 | ;; 90 | esac])dnl 91 | $1="$ac_cv_path_$1" 92 | if test ifelse([$4], , [-n "[$]$1"], ["[$]$1" != "$4"]); then 93 | AC_MSG_RESULT([$]$1) 94 | else 95 | AC_MSG_RESULT(no) 96 | fi 97 | AC_SUBST($1)dnl 98 | ]) 99 | 100 | # GLIB_WITH_NLS 101 | #----------------- 102 | glib_DEFUN([GLIB_WITH_NLS], 103 | dnl NLS is obligatory 104 | [USE_NLS=yes 105 | AC_SUBST(USE_NLS) 106 | 107 | gt_cv_have_gettext=no 108 | 109 | CATOBJEXT=NONE 110 | XGETTEXT=: 111 | INTLLIBS= 112 | 113 | AC_CHECK_HEADER(libintl.h, 114 | [gt_cv_func_dgettext_libintl="no" 115 | libintl_extra_libs="" 116 | 117 | # 118 | # First check in libc 119 | # 120 | AC_CACHE_CHECK([for ngettext in libc], gt_cv_func_ngettext_libc, 121 | [AC_TRY_LINK([ 122 | #include 123 | ], 124 | [return !ngettext ("","", 1)], 125 | gt_cv_func_ngettext_libc=yes, 126 | gt_cv_func_ngettext_libc=no) 127 | ]) 128 | 129 | if test "$gt_cv_func_ngettext_libc" = "yes" ; then 130 | AC_CACHE_CHECK([for dgettext in libc], gt_cv_func_dgettext_libc, 131 | [AC_TRY_LINK([ 132 | #include 133 | ], 134 | [return !dgettext ("","")], 135 | gt_cv_func_dgettext_libc=yes, 136 | gt_cv_func_dgettext_libc=no) 137 | ]) 138 | fi 139 | 140 | if test "$gt_cv_func_ngettext_libc" = "yes" ; then 141 | AC_CHECK_FUNCS(bind_textdomain_codeset) 142 | fi 143 | 144 | # 145 | # If we don't have everything we want, check in libintl 146 | # 147 | if test "$gt_cv_func_dgettext_libc" != "yes" \ 148 | || test "$gt_cv_func_ngettext_libc" != "yes" \ 149 | || test "$ac_cv_func_bind_textdomain_codeset" != "yes" ; then 150 | 151 | AC_CHECK_LIB(intl, bindtextdomain, 152 | [AC_CHECK_LIB(intl, ngettext, 153 | [AC_CHECK_LIB(intl, dgettext, 154 | gt_cv_func_dgettext_libintl=yes)])]) 155 | 156 | if test "$gt_cv_func_dgettext_libintl" != "yes" ; then 157 | AC_MSG_CHECKING([if -liconv is needed to use gettext]) 158 | AC_MSG_RESULT([]) 159 | AC_CHECK_LIB(intl, ngettext, 160 | [AC_CHECK_LIB(intl, dcgettext, 161 | [gt_cv_func_dgettext_libintl=yes 162 | libintl_extra_libs=-liconv], 163 | :,-liconv)], 164 | :,-liconv) 165 | fi 166 | 167 | # 168 | # If we found libintl, then check in it for bind_textdomain_codeset(); 169 | # we'll prefer libc if neither have bind_textdomain_codeset(), 170 | # and both have dgettext and ngettext 171 | # 172 | if test "$gt_cv_func_dgettext_libintl" = "yes" ; then 173 | glib_save_LIBS="$LIBS" 174 | LIBS="$LIBS -lintl $libintl_extra_libs" 175 | unset ac_cv_func_bind_textdomain_codeset 176 | AC_CHECK_FUNCS(bind_textdomain_codeset) 177 | LIBS="$glib_save_LIBS" 178 | 179 | if test "$ac_cv_func_bind_textdomain_codeset" = "yes" ; then 180 | gt_cv_func_dgettext_libc=no 181 | else 182 | if test "$gt_cv_func_dgettext_libc" = "yes" \ 183 | && test "$gt_cv_func_ngettext_libc" = "yes"; then 184 | gt_cv_func_dgettext_libintl=no 185 | fi 186 | fi 187 | fi 188 | fi 189 | 190 | if test "$gt_cv_func_dgettext_libc" = "yes" \ 191 | || test "$gt_cv_func_dgettext_libintl" = "yes"; then 192 | gt_cv_have_gettext=yes 193 | fi 194 | 195 | if test "$gt_cv_func_dgettext_libintl" = "yes"; then 196 | INTLLIBS="-lintl $libintl_extra_libs" 197 | fi 198 | 199 | if test "$gt_cv_have_gettext" = "yes"; then 200 | AC_DEFINE(HAVE_GETTEXT,1, 201 | [Define if the GNU gettext() function is already present or preinstalled.]) 202 | GLIB_PATH_PROG_WITH_TEST(MSGFMT, msgfmt, 203 | [test -z "`$ac_dir/$ac_word -h 2>&1 | grep 'dv '`"], no)dnl 204 | if test "$MSGFMT" != "no"; then 205 | glib_save_LIBS="$LIBS" 206 | LIBS="$LIBS $INTLLIBS" 207 | AC_CHECK_FUNCS(dcgettext) 208 | MSGFMT_OPTS= 209 | AC_MSG_CHECKING([if msgfmt accepts -c]) 210 | GLIB_RUN_PROG([$MSGFMT -c -o /dev/null],[ 211 | msgid "" 212 | msgstr "" 213 | "Content-Type: text/plain; charset=UTF-8\n" 214 | "Project-Id-Version: test 1.0\n" 215 | "PO-Revision-Date: 2007-02-15 12:01+0100\n" 216 | "Last-Translator: test \n" 217 | "Language-Team: C \n" 218 | "MIME-Version: 1.0\n" 219 | "Content-Transfer-Encoding: 8bit\n" 220 | ], [MSGFMT_OPTS=-c; AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])]) 221 | AC_SUBST(MSGFMT_OPTS) 222 | AC_PATH_PROG(GMSGFMT, gmsgfmt, $MSGFMT) 223 | GLIB_PATH_PROG_WITH_TEST(XGETTEXT, xgettext, 224 | [test -z "`$ac_dir/$ac_word -h 2>&1 | grep '(HELP)'`"], :) 225 | AC_TRY_LINK(, [extern int _nl_msg_cat_cntr; 226 | return _nl_msg_cat_cntr], 227 | [CATOBJEXT=.gmo 228 | DATADIRNAME=share], 229 | [case $host in 230 | *-*-solaris*) 231 | dnl On Solaris, if bind_textdomain_codeset is in libc, 232 | dnl GNU format message catalog is always supported, 233 | dnl since both are added to the libc all together. 234 | dnl Hence, we'd like to go with DATADIRNAME=share and 235 | dnl and CATOBJEXT=.gmo in this case. 236 | AC_CHECK_FUNC(bind_textdomain_codeset, 237 | [CATOBJEXT=.gmo 238 | DATADIRNAME=share], 239 | [CATOBJEXT=.mo 240 | DATADIRNAME=lib]) 241 | ;; 242 | *-*-openbsd*) 243 | CATOBJEXT=.mo 244 | DATADIRNAME=share 245 | ;; 246 | *) 247 | CATOBJEXT=.mo 248 | DATADIRNAME=lib 249 | ;; 250 | esac]) 251 | LIBS="$glib_save_LIBS" 252 | INSTOBJEXT=.mo 253 | else 254 | gt_cv_have_gettext=no 255 | fi 256 | fi 257 | ]) 258 | 259 | if test "$gt_cv_have_gettext" = "yes" ; then 260 | AC_DEFINE(ENABLE_NLS, 1, 261 | [always defined to indicate that i18n is enabled]) 262 | fi 263 | 264 | dnl Test whether we really found GNU xgettext. 265 | if test "$XGETTEXT" != ":"; then 266 | dnl If it is not GNU xgettext we define it as : so that the 267 | dnl Makefiles still can work. 268 | if $XGETTEXT --omit-header /dev/null 2> /dev/null; then 269 | : ; 270 | else 271 | AC_MSG_RESULT( 272 | [found xgettext program is not GNU xgettext; ignore it]) 273 | XGETTEXT=":" 274 | fi 275 | fi 276 | 277 | # We need to process the po/ directory. 278 | POSUB=po 279 | 280 | AC_OUTPUT_COMMANDS( 281 | [case "$CONFIG_FILES" in *po/Makefile.in*) 282 | sed -e "/POTFILES =/r po/POTFILES" po/Makefile.in > po/Makefile 283 | esac]) 284 | 285 | dnl These rules are solely for the distribution goal. While doing this 286 | dnl we only have to keep exactly one list of the available catalogs 287 | dnl in configure.ac. 288 | for lang in $ALL_LINGUAS; do 289 | GMOFILES="$GMOFILES $lang.gmo" 290 | POFILES="$POFILES $lang.po" 291 | done 292 | 293 | dnl Make all variables we use known to autoconf. 294 | AC_SUBST(CATALOGS) 295 | AC_SUBST(CATOBJEXT) 296 | AC_SUBST(DATADIRNAME) 297 | AC_SUBST(GMOFILES) 298 | AC_SUBST(INSTOBJEXT) 299 | AC_SUBST(INTLLIBS) 300 | AC_SUBST(PO_IN_DATADIR_TRUE) 301 | AC_SUBST(PO_IN_DATADIR_FALSE) 302 | AC_SUBST(POFILES) 303 | AC_SUBST(POSUB) 304 | ]) 305 | 306 | # AM_GLIB_GNU_GETTEXT 307 | # ------------------- 308 | # Do checks necessary for use of gettext. If a suitable implementation 309 | # of gettext is found in either in libintl or in the C library, 310 | # it will set INTLLIBS to the libraries needed for use of gettext 311 | # and AC_DEFINE() HAVE_GETTEXT and ENABLE_NLS. (The shell variable 312 | # gt_cv_have_gettext will be set to "yes".) It will also call AC_SUBST() 313 | # on various variables needed by the Makefile.in.in installed by 314 | # glib-gettextize. 315 | dnl 316 | AU_DEFUN([GLIB_GNU_GETTEXT], 317 | [AC_REQUIRE([AC_PROG_CC])dnl 318 | 319 | GLIB_LC_MESSAGES 320 | GLIB_WITH_NLS 321 | 322 | if test "$gt_cv_have_gettext" = "yes"; then 323 | if test "x$ALL_LINGUAS" = "x"; then 324 | LINGUAS= 325 | else 326 | AC_MSG_CHECKING(for catalogs to be installed) 327 | NEW_LINGUAS= 328 | for presentlang in $ALL_LINGUAS; do 329 | useit=no 330 | if test "%UNSET%" != "${LINGUAS-%UNSET%}"; then 331 | desiredlanguages="$LINGUAS" 332 | else 333 | desiredlanguages="$ALL_LINGUAS" 334 | fi 335 | for desiredlang in $desiredlanguages; do 336 | # Use the presentlang catalog if desiredlang is 337 | # a. equal to presentlang, or 338 | # b. a variant of presentlang (because in this case, 339 | # presentlang can be used as a fallback for messages 340 | # which are not translated in the desiredlang catalog). 341 | case "$desiredlang" in 342 | "$presentlang"*) useit=yes;; 343 | esac 344 | done 345 | if test $useit = yes; then 346 | NEW_LINGUAS="$NEW_LINGUAS $presentlang" 347 | fi 348 | done 349 | LINGUAS=$NEW_LINGUAS 350 | AC_MSG_RESULT($LINGUAS) 351 | fi 352 | 353 | dnl Construct list of names of catalog files to be constructed. 354 | if test -n "$LINGUAS"; then 355 | for lang in $LINGUAS; do CATALOGS="$CATALOGS $lang$CATOBJEXT"; done 356 | fi 357 | fi 358 | 359 | dnl If the AC_CONFIG_AUX_DIR macro for autoconf is used we possibly 360 | dnl find the mkinstalldirs script in another subdir but ($top_srcdir). 361 | dnl Try to locate is. 362 | MKINSTALLDIRS= 363 | if test -n "$ac_aux_dir"; then 364 | MKINSTALLDIRS="$ac_aux_dir/mkinstalldirs" 365 | fi 366 | if test -z "$MKINSTALLDIRS"; then 367 | MKINSTALLDIRS="\$(top_srcdir)/mkinstalldirs" 368 | fi 369 | AC_SUBST(MKINSTALLDIRS) 370 | 371 | dnl Generate list of files to be processed by xgettext which will 372 | dnl be included in po/Makefile. 373 | test -d po || mkdir po 374 | if test "x$srcdir" != "x."; then 375 | if test "x`echo $srcdir | sed 's@/.*@@'`" = "x"; then 376 | posrcprefix="$srcdir/" 377 | else 378 | posrcprefix="../$srcdir/" 379 | fi 380 | else 381 | posrcprefix="../" 382 | fi 383 | rm -f po/POTFILES 384 | sed -e "/^#/d" -e "/^\$/d" -e "s,.*, $posrcprefix& \\\\," -e "\$s/\(.*\) \\\\/\1/" \ 385 | < $srcdir/po/POTFILES.in > po/POTFILES 386 | ], 387 | [[$0: This macro is deprecated. You should use upstream gettext instead.]]) 388 | 389 | # AM_GLIB_DEFINE_LOCALEDIR(VARIABLE) 390 | # ------------------------------- 391 | # Define VARIABLE to the location where catalog files will 392 | # be installed by po/Makefile. 393 | glib_DEFUN([GLIB_DEFINE_LOCALEDIR], 394 | [glib_REQUIRE([GLIB_GNU_GETTEXT])dnl 395 | glib_save_prefix="$prefix" 396 | glib_save_exec_prefix="$exec_prefix" 397 | glib_save_datarootdir="$datarootdir" 398 | test "x$prefix" = xNONE && prefix=$ac_default_prefix 399 | test "x$exec_prefix" = xNONE && exec_prefix=$prefix 400 | datarootdir=`eval echo "${datarootdir}"` 401 | if test "x$CATOBJEXT" = "x.mo" ; then 402 | localedir=`eval echo "${libdir}/locale"` 403 | else 404 | localedir=`eval echo "${datadir}/locale"` 405 | fi 406 | prefix="$glib_save_prefix" 407 | exec_prefix="$glib_save_exec_prefix" 408 | datarootdir="$glib_save_datarootdir" 409 | AC_DEFINE_UNQUOTED($1, "$localedir", 410 | [Define the location where the catalogs will be installed]) 411 | ]) 412 | 413 | dnl 414 | dnl Now the definitions that aclocal will find 415 | dnl 416 | ifdef(glib_configure_ac,[],[ 417 | AC_DEFUN([AM_GLIB_GNU_GETTEXT],[GLIB_GNU_GETTEXT($@)]) 418 | AC_DEFUN([AM_GLIB_DEFINE_LOCALEDIR],[GLIB_DEFINE_LOCALEDIR($@)]) 419 | ])dnl 420 | 421 | # GLIB_RUN_PROG(PROGRAM, TEST-FILE, [ACTION-IF-PASS], [ACTION-IF-FAIL]) 422 | # 423 | # Create a temporary file with TEST-FILE as its contents and pass the 424 | # file name to PROGRAM. Perform ACTION-IF-PASS if PROGRAM exits with 425 | # 0 and perform ACTION-IF-FAIL for any other exit status. 426 | AC_DEFUN([GLIB_RUN_PROG], 427 | [cat >conftest.foo <<_ACEOF 428 | $2 429 | _ACEOF 430 | if AC_RUN_LOG([$1 conftest.foo]); then 431 | m4_ifval([$3], [$3], [:]) 432 | m4_ifvaln([$4], [else $4])dnl 433 | echo "$as_me: failed input was:" >&AS_MESSAGE_LOG_FD 434 | sed 's/^/| /' conftest.foo >&AS_MESSAGE_LOG_FD 435 | fi]) 436 | 437 | -------------------------------------------------------------------------------- /m4/ltoptions.m4: -------------------------------------------------------------------------------- 1 | # Helper functions for option handling. -*- Autoconf -*- 2 | # 3 | # Copyright (C) 2004-2005, 2007-2009, 2011-2015 Free Software 4 | # Foundation, Inc. 5 | # Written by Gary V. Vaughan, 2004 6 | # 7 | # This file is free software; the Free Software Foundation gives 8 | # unlimited permission to copy and/or distribute it, with or without 9 | # modifications, as long as this notice is preserved. 10 | 11 | # serial 8 ltoptions.m4 12 | 13 | # This is to help aclocal find these macros, as it can't see m4_define. 14 | AC_DEFUN([LTOPTIONS_VERSION], [m4_if([1])]) 15 | 16 | 17 | # _LT_MANGLE_OPTION(MACRO-NAME, OPTION-NAME) 18 | # ------------------------------------------ 19 | m4_define([_LT_MANGLE_OPTION], 20 | [[_LT_OPTION_]m4_bpatsubst($1__$2, [[^a-zA-Z0-9_]], [_])]) 21 | 22 | 23 | # _LT_SET_OPTION(MACRO-NAME, OPTION-NAME) 24 | # --------------------------------------- 25 | # Set option OPTION-NAME for macro MACRO-NAME, and if there is a 26 | # matching handler defined, dispatch to it. Other OPTION-NAMEs are 27 | # saved as a flag. 28 | m4_define([_LT_SET_OPTION], 29 | [m4_define(_LT_MANGLE_OPTION([$1], [$2]))dnl 30 | m4_ifdef(_LT_MANGLE_DEFUN([$1], [$2]), 31 | _LT_MANGLE_DEFUN([$1], [$2]), 32 | [m4_warning([Unknown $1 option '$2'])])[]dnl 33 | ]) 34 | 35 | 36 | # _LT_IF_OPTION(MACRO-NAME, OPTION-NAME, IF-SET, [IF-NOT-SET]) 37 | # ------------------------------------------------------------ 38 | # Execute IF-SET if OPTION is set, IF-NOT-SET otherwise. 39 | m4_define([_LT_IF_OPTION], 40 | [m4_ifdef(_LT_MANGLE_OPTION([$1], [$2]), [$3], [$4])]) 41 | 42 | 43 | # _LT_UNLESS_OPTIONS(MACRO-NAME, OPTION-LIST, IF-NOT-SET) 44 | # ------------------------------------------------------- 45 | # Execute IF-NOT-SET unless all options in OPTION-LIST for MACRO-NAME 46 | # are set. 47 | m4_define([_LT_UNLESS_OPTIONS], 48 | [m4_foreach([_LT_Option], m4_split(m4_normalize([$2])), 49 | [m4_ifdef(_LT_MANGLE_OPTION([$1], _LT_Option), 50 | [m4_define([$0_found])])])[]dnl 51 | m4_ifdef([$0_found], [m4_undefine([$0_found])], [$3 52 | ])[]dnl 53 | ]) 54 | 55 | 56 | # _LT_SET_OPTIONS(MACRO-NAME, OPTION-LIST) 57 | # ---------------------------------------- 58 | # OPTION-LIST is a space-separated list of Libtool options associated 59 | # with MACRO-NAME. If any OPTION has a matching handler declared with 60 | # LT_OPTION_DEFINE, dispatch to that macro; otherwise complain about 61 | # the unknown option and exit. 62 | m4_defun([_LT_SET_OPTIONS], 63 | [# Set options 64 | m4_foreach([_LT_Option], m4_split(m4_normalize([$2])), 65 | [_LT_SET_OPTION([$1], _LT_Option)]) 66 | 67 | m4_if([$1],[LT_INIT],[ 68 | dnl 69 | dnl Simply set some default values (i.e off) if boolean options were not 70 | dnl specified: 71 | _LT_UNLESS_OPTIONS([LT_INIT], [dlopen], [enable_dlopen=no 72 | ]) 73 | _LT_UNLESS_OPTIONS([LT_INIT], [win32-dll], [enable_win32_dll=no 74 | ]) 75 | dnl 76 | dnl If no reference was made to various pairs of opposing options, then 77 | dnl we run the default mode handler for the pair. For example, if neither 78 | dnl 'shared' nor 'disable-shared' was passed, we enable building of shared 79 | dnl archives by default: 80 | _LT_UNLESS_OPTIONS([LT_INIT], [shared disable-shared], [_LT_ENABLE_SHARED]) 81 | _LT_UNLESS_OPTIONS([LT_INIT], [static disable-static], [_LT_ENABLE_STATIC]) 82 | _LT_UNLESS_OPTIONS([LT_INIT], [pic-only no-pic], [_LT_WITH_PIC]) 83 | _LT_UNLESS_OPTIONS([LT_INIT], [fast-install disable-fast-install], 84 | [_LT_ENABLE_FAST_INSTALL]) 85 | _LT_UNLESS_OPTIONS([LT_INIT], [aix-soname=aix aix-soname=both aix-soname=svr4], 86 | [_LT_WITH_AIX_SONAME([aix])]) 87 | ]) 88 | ])# _LT_SET_OPTIONS 89 | 90 | 91 | ## --------------------------------- ## 92 | ## Macros to handle LT_INIT options. ## 93 | ## --------------------------------- ## 94 | 95 | # _LT_MANGLE_DEFUN(MACRO-NAME, OPTION-NAME) 96 | # ----------------------------------------- 97 | m4_define([_LT_MANGLE_DEFUN], 98 | [[_LT_OPTION_DEFUN_]m4_bpatsubst(m4_toupper([$1__$2]), [[^A-Z0-9_]], [_])]) 99 | 100 | 101 | # LT_OPTION_DEFINE(MACRO-NAME, OPTION-NAME, CODE) 102 | # ----------------------------------------------- 103 | m4_define([LT_OPTION_DEFINE], 104 | [m4_define(_LT_MANGLE_DEFUN([$1], [$2]), [$3])[]dnl 105 | ])# LT_OPTION_DEFINE 106 | 107 | 108 | # dlopen 109 | # ------ 110 | LT_OPTION_DEFINE([LT_INIT], [dlopen], [enable_dlopen=yes 111 | ]) 112 | 113 | AU_DEFUN([AC_LIBTOOL_DLOPEN], 114 | [_LT_SET_OPTION([LT_INIT], [dlopen]) 115 | AC_DIAGNOSE([obsolete], 116 | [$0: Remove this warning and the call to _LT_SET_OPTION when you 117 | put the 'dlopen' option into LT_INIT's first parameter.]) 118 | ]) 119 | 120 | dnl aclocal-1.4 backwards compatibility: 121 | dnl AC_DEFUN([AC_LIBTOOL_DLOPEN], []) 122 | 123 | 124 | # win32-dll 125 | # --------- 126 | # Declare package support for building win32 dll's. 127 | LT_OPTION_DEFINE([LT_INIT], [win32-dll], 128 | [enable_win32_dll=yes 129 | 130 | case $host in 131 | *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-cegcc*) 132 | AC_CHECK_TOOL(AS, as, false) 133 | AC_CHECK_TOOL(DLLTOOL, dlltool, false) 134 | AC_CHECK_TOOL(OBJDUMP, objdump, false) 135 | ;; 136 | esac 137 | 138 | test -z "$AS" && AS=as 139 | _LT_DECL([], [AS], [1], [Assembler program])dnl 140 | 141 | test -z "$DLLTOOL" && DLLTOOL=dlltool 142 | _LT_DECL([], [DLLTOOL], [1], [DLL creation program])dnl 143 | 144 | test -z "$OBJDUMP" && OBJDUMP=objdump 145 | _LT_DECL([], [OBJDUMP], [1], [Object dumper program])dnl 146 | ])# win32-dll 147 | 148 | AU_DEFUN([AC_LIBTOOL_WIN32_DLL], 149 | [AC_REQUIRE([AC_CANONICAL_HOST])dnl 150 | _LT_SET_OPTION([LT_INIT], [win32-dll]) 151 | AC_DIAGNOSE([obsolete], 152 | [$0: Remove this warning and the call to _LT_SET_OPTION when you 153 | put the 'win32-dll' option into LT_INIT's first parameter.]) 154 | ]) 155 | 156 | dnl aclocal-1.4 backwards compatibility: 157 | dnl AC_DEFUN([AC_LIBTOOL_WIN32_DLL], []) 158 | 159 | 160 | # _LT_ENABLE_SHARED([DEFAULT]) 161 | # ---------------------------- 162 | # implement the --enable-shared flag, and supports the 'shared' and 163 | # 'disable-shared' LT_INIT options. 164 | # DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. 165 | m4_define([_LT_ENABLE_SHARED], 166 | [m4_define([_LT_ENABLE_SHARED_DEFAULT], [m4_if($1, no, no, yes)])dnl 167 | AC_ARG_ENABLE([shared], 168 | [AS_HELP_STRING([--enable-shared@<:@=PKGS@:>@], 169 | [build shared libraries @<:@default=]_LT_ENABLE_SHARED_DEFAULT[@:>@])], 170 | [p=${PACKAGE-default} 171 | case $enableval in 172 | yes) enable_shared=yes ;; 173 | no) enable_shared=no ;; 174 | *) 175 | enable_shared=no 176 | # Look at the argument we got. We use all the common list separators. 177 | lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, 178 | for pkg in $enableval; do 179 | IFS=$lt_save_ifs 180 | if test "X$pkg" = "X$p"; then 181 | enable_shared=yes 182 | fi 183 | done 184 | IFS=$lt_save_ifs 185 | ;; 186 | esac], 187 | [enable_shared=]_LT_ENABLE_SHARED_DEFAULT) 188 | 189 | _LT_DECL([build_libtool_libs], [enable_shared], [0], 190 | [Whether or not to build shared libraries]) 191 | ])# _LT_ENABLE_SHARED 192 | 193 | LT_OPTION_DEFINE([LT_INIT], [shared], [_LT_ENABLE_SHARED([yes])]) 194 | LT_OPTION_DEFINE([LT_INIT], [disable-shared], [_LT_ENABLE_SHARED([no])]) 195 | 196 | # Old names: 197 | AC_DEFUN([AC_ENABLE_SHARED], 198 | [_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[shared]) 199 | ]) 200 | 201 | AC_DEFUN([AC_DISABLE_SHARED], 202 | [_LT_SET_OPTION([LT_INIT], [disable-shared]) 203 | ]) 204 | 205 | AU_DEFUN([AM_ENABLE_SHARED], [AC_ENABLE_SHARED($@)]) 206 | AU_DEFUN([AM_DISABLE_SHARED], [AC_DISABLE_SHARED($@)]) 207 | 208 | dnl aclocal-1.4 backwards compatibility: 209 | dnl AC_DEFUN([AM_ENABLE_SHARED], []) 210 | dnl AC_DEFUN([AM_DISABLE_SHARED], []) 211 | 212 | 213 | 214 | # _LT_ENABLE_STATIC([DEFAULT]) 215 | # ---------------------------- 216 | # implement the --enable-static flag, and support the 'static' and 217 | # 'disable-static' LT_INIT options. 218 | # DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. 219 | m4_define([_LT_ENABLE_STATIC], 220 | [m4_define([_LT_ENABLE_STATIC_DEFAULT], [m4_if($1, no, no, yes)])dnl 221 | AC_ARG_ENABLE([static], 222 | [AS_HELP_STRING([--enable-static@<:@=PKGS@:>@], 223 | [build static libraries @<:@default=]_LT_ENABLE_STATIC_DEFAULT[@:>@])], 224 | [p=${PACKAGE-default} 225 | case $enableval in 226 | yes) enable_static=yes ;; 227 | no) enable_static=no ;; 228 | *) 229 | enable_static=no 230 | # Look at the argument we got. We use all the common list separators. 231 | lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, 232 | for pkg in $enableval; do 233 | IFS=$lt_save_ifs 234 | if test "X$pkg" = "X$p"; then 235 | enable_static=yes 236 | fi 237 | done 238 | IFS=$lt_save_ifs 239 | ;; 240 | esac], 241 | [enable_static=]_LT_ENABLE_STATIC_DEFAULT) 242 | 243 | _LT_DECL([build_old_libs], [enable_static], [0], 244 | [Whether or not to build static libraries]) 245 | ])# _LT_ENABLE_STATIC 246 | 247 | LT_OPTION_DEFINE([LT_INIT], [static], [_LT_ENABLE_STATIC([yes])]) 248 | LT_OPTION_DEFINE([LT_INIT], [disable-static], [_LT_ENABLE_STATIC([no])]) 249 | 250 | # Old names: 251 | AC_DEFUN([AC_ENABLE_STATIC], 252 | [_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[static]) 253 | ]) 254 | 255 | AC_DEFUN([AC_DISABLE_STATIC], 256 | [_LT_SET_OPTION([LT_INIT], [disable-static]) 257 | ]) 258 | 259 | AU_DEFUN([AM_ENABLE_STATIC], [AC_ENABLE_STATIC($@)]) 260 | AU_DEFUN([AM_DISABLE_STATIC], [AC_DISABLE_STATIC($@)]) 261 | 262 | dnl aclocal-1.4 backwards compatibility: 263 | dnl AC_DEFUN([AM_ENABLE_STATIC], []) 264 | dnl AC_DEFUN([AM_DISABLE_STATIC], []) 265 | 266 | 267 | 268 | # _LT_ENABLE_FAST_INSTALL([DEFAULT]) 269 | # ---------------------------------- 270 | # implement the --enable-fast-install flag, and support the 'fast-install' 271 | # and 'disable-fast-install' LT_INIT options. 272 | # DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. 273 | m4_define([_LT_ENABLE_FAST_INSTALL], 274 | [m4_define([_LT_ENABLE_FAST_INSTALL_DEFAULT], [m4_if($1, no, no, yes)])dnl 275 | AC_ARG_ENABLE([fast-install], 276 | [AS_HELP_STRING([--enable-fast-install@<:@=PKGS@:>@], 277 | [optimize for fast installation @<:@default=]_LT_ENABLE_FAST_INSTALL_DEFAULT[@:>@])], 278 | [p=${PACKAGE-default} 279 | case $enableval in 280 | yes) enable_fast_install=yes ;; 281 | no) enable_fast_install=no ;; 282 | *) 283 | enable_fast_install=no 284 | # Look at the argument we got. We use all the common list separators. 285 | lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, 286 | for pkg in $enableval; do 287 | IFS=$lt_save_ifs 288 | if test "X$pkg" = "X$p"; then 289 | enable_fast_install=yes 290 | fi 291 | done 292 | IFS=$lt_save_ifs 293 | ;; 294 | esac], 295 | [enable_fast_install=]_LT_ENABLE_FAST_INSTALL_DEFAULT) 296 | 297 | _LT_DECL([fast_install], [enable_fast_install], [0], 298 | [Whether or not to optimize for fast installation])dnl 299 | ])# _LT_ENABLE_FAST_INSTALL 300 | 301 | LT_OPTION_DEFINE([LT_INIT], [fast-install], [_LT_ENABLE_FAST_INSTALL([yes])]) 302 | LT_OPTION_DEFINE([LT_INIT], [disable-fast-install], [_LT_ENABLE_FAST_INSTALL([no])]) 303 | 304 | # Old names: 305 | AU_DEFUN([AC_ENABLE_FAST_INSTALL], 306 | [_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[fast-install]) 307 | AC_DIAGNOSE([obsolete], 308 | [$0: Remove this warning and the call to _LT_SET_OPTION when you put 309 | the 'fast-install' option into LT_INIT's first parameter.]) 310 | ]) 311 | 312 | AU_DEFUN([AC_DISABLE_FAST_INSTALL], 313 | [_LT_SET_OPTION([LT_INIT], [disable-fast-install]) 314 | AC_DIAGNOSE([obsolete], 315 | [$0: Remove this warning and the call to _LT_SET_OPTION when you put 316 | the 'disable-fast-install' option into LT_INIT's first parameter.]) 317 | ]) 318 | 319 | dnl aclocal-1.4 backwards compatibility: 320 | dnl AC_DEFUN([AC_ENABLE_FAST_INSTALL], []) 321 | dnl AC_DEFUN([AM_DISABLE_FAST_INSTALL], []) 322 | 323 | 324 | # _LT_WITH_AIX_SONAME([DEFAULT]) 325 | # ---------------------------------- 326 | # implement the --with-aix-soname flag, and support the `aix-soname=aix' 327 | # and `aix-soname=both' and `aix-soname=svr4' LT_INIT options. DEFAULT 328 | # is either `aix', `both' or `svr4'. If omitted, it defaults to `aix'. 329 | m4_define([_LT_WITH_AIX_SONAME], 330 | [m4_define([_LT_WITH_AIX_SONAME_DEFAULT], [m4_if($1, svr4, svr4, m4_if($1, both, both, aix))])dnl 331 | shared_archive_member_spec= 332 | case $host,$enable_shared in 333 | power*-*-aix[[5-9]]*,yes) 334 | AC_MSG_CHECKING([which variant of shared library versioning to provide]) 335 | AC_ARG_WITH([aix-soname], 336 | [AS_HELP_STRING([--with-aix-soname=aix|svr4|both], 337 | [shared library versioning (aka "SONAME") variant to provide on AIX, @<:@default=]_LT_WITH_AIX_SONAME_DEFAULT[@:>@.])], 338 | [case $withval in 339 | aix|svr4|both) 340 | ;; 341 | *) 342 | AC_MSG_ERROR([Unknown argument to --with-aix-soname]) 343 | ;; 344 | esac 345 | lt_cv_with_aix_soname=$with_aix_soname], 346 | [AC_CACHE_VAL([lt_cv_with_aix_soname], 347 | [lt_cv_with_aix_soname=]_LT_WITH_AIX_SONAME_DEFAULT) 348 | with_aix_soname=$lt_cv_with_aix_soname]) 349 | AC_MSG_RESULT([$with_aix_soname]) 350 | if test aix != "$with_aix_soname"; then 351 | # For the AIX way of multilib, we name the shared archive member 352 | # based on the bitwidth used, traditionally 'shr.o' or 'shr_64.o', 353 | # and 'shr.imp' or 'shr_64.imp', respectively, for the Import File. 354 | # Even when GNU compilers ignore OBJECT_MODE but need '-maix64' flag, 355 | # the AIX toolchain works better with OBJECT_MODE set (default 32). 356 | if test 64 = "${OBJECT_MODE-32}"; then 357 | shared_archive_member_spec=shr_64 358 | else 359 | shared_archive_member_spec=shr 360 | fi 361 | fi 362 | ;; 363 | *) 364 | with_aix_soname=aix 365 | ;; 366 | esac 367 | 368 | _LT_DECL([], [shared_archive_member_spec], [0], 369 | [Shared archive member basename, for filename based shared library versioning on AIX])dnl 370 | ])# _LT_WITH_AIX_SONAME 371 | 372 | LT_OPTION_DEFINE([LT_INIT], [aix-soname=aix], [_LT_WITH_AIX_SONAME([aix])]) 373 | LT_OPTION_DEFINE([LT_INIT], [aix-soname=both], [_LT_WITH_AIX_SONAME([both])]) 374 | LT_OPTION_DEFINE([LT_INIT], [aix-soname=svr4], [_LT_WITH_AIX_SONAME([svr4])]) 375 | 376 | 377 | # _LT_WITH_PIC([MODE]) 378 | # -------------------- 379 | # implement the --with-pic flag, and support the 'pic-only' and 'no-pic' 380 | # LT_INIT options. 381 | # MODE is either 'yes' or 'no'. If omitted, it defaults to 'both'. 382 | m4_define([_LT_WITH_PIC], 383 | [AC_ARG_WITH([pic], 384 | [AS_HELP_STRING([--with-pic@<:@=PKGS@:>@], 385 | [try to use only PIC/non-PIC objects @<:@default=use both@:>@])], 386 | [lt_p=${PACKAGE-default} 387 | case $withval in 388 | yes|no) pic_mode=$withval ;; 389 | *) 390 | pic_mode=default 391 | # Look at the argument we got. We use all the common list separators. 392 | lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, 393 | for lt_pkg in $withval; do 394 | IFS=$lt_save_ifs 395 | if test "X$lt_pkg" = "X$lt_p"; then 396 | pic_mode=yes 397 | fi 398 | done 399 | IFS=$lt_save_ifs 400 | ;; 401 | esac], 402 | [pic_mode=m4_default([$1], [default])]) 403 | 404 | _LT_DECL([], [pic_mode], [0], [What type of objects to build])dnl 405 | ])# _LT_WITH_PIC 406 | 407 | LT_OPTION_DEFINE([LT_INIT], [pic-only], [_LT_WITH_PIC([yes])]) 408 | LT_OPTION_DEFINE([LT_INIT], [no-pic], [_LT_WITH_PIC([no])]) 409 | 410 | # Old name: 411 | AU_DEFUN([AC_LIBTOOL_PICMODE], 412 | [_LT_SET_OPTION([LT_INIT], [pic-only]) 413 | AC_DIAGNOSE([obsolete], 414 | [$0: Remove this warning and the call to _LT_SET_OPTION when you 415 | put the 'pic-only' option into LT_INIT's first parameter.]) 416 | ]) 417 | 418 | dnl aclocal-1.4 backwards compatibility: 419 | dnl AC_DEFUN([AC_LIBTOOL_PICMODE], []) 420 | 421 | ## ----------------- ## 422 | ## LTDL_INIT Options ## 423 | ## ----------------- ## 424 | 425 | m4_define([_LTDL_MODE], []) 426 | LT_OPTION_DEFINE([LTDL_INIT], [nonrecursive], 427 | [m4_define([_LTDL_MODE], [nonrecursive])]) 428 | LT_OPTION_DEFINE([LTDL_INIT], [recursive], 429 | [m4_define([_LTDL_MODE], [recursive])]) 430 | LT_OPTION_DEFINE([LTDL_INIT], [subproject], 431 | [m4_define([_LTDL_MODE], [subproject])]) 432 | 433 | m4_define([_LTDL_TYPE], []) 434 | LT_OPTION_DEFINE([LTDL_INIT], [installable], 435 | [m4_define([_LTDL_TYPE], [installable])]) 436 | LT_OPTION_DEFINE([LTDL_INIT], [convenience], 437 | [m4_define([_LTDL_TYPE], [convenience])]) 438 | -------------------------------------------------------------------------------- /src/bayes-storage-memory.c: -------------------------------------------------------------------------------- 1 | /* bayes-storage-memory.c 2 | * 3 | * Copyright (C) 2012 Christian Hergert 4 | * 5 | * This file is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2.1 of the License, or (at your option) any later version. 9 | * 10 | * This file is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "bayes-storage-memory.h" 20 | #include "bayes-storage-memory-private.h" 21 | #include 22 | #include 23 | 24 | /** 25 | * SECTION:bayes-storage-memory 26 | * @title: BayesStorageMemory 27 | * @short_description: Storage of training data in memory. 28 | * 29 | * #BayesStorageMemory is an implementation of #BayesStorage that 30 | * stores the tokens and their associated counts in memory using 31 | * #GHashTable. It is mean for smaller data sets. It can be serialized 32 | * and deserialized from JSON format. 33 | */ 34 | 35 | static void bayes_storage_init (BayesStorageInterface *iface); 36 | 37 | enum { 38 | PROP_0, 39 | 40 | PROP_NAMES, 41 | PROP_CORPUS, 42 | 43 | N_PROPERTIES 44 | }; 45 | 46 | static GParamSpec *obj_properties[N_PROPERTIES] = { NULL }; 47 | 48 | static JsonSerializableIface *serializable_iface = NULL; 49 | 50 | static void json_serializable_iface_init (JsonSerializableIface *iface); 51 | 52 | G_DEFINE_TYPE_EXTENDED (BayesStorageMemory, 53 | bayes_storage_memory, 54 | G_TYPE_OBJECT, 55 | 0, 56 | G_IMPLEMENT_INTERFACE (BAYES_TYPE_STORAGE, bayes_storage_init); 57 | G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE, json_serializable_iface_init)) 58 | 59 | static void 60 | bayes_tokens_free (gpointer data) 61 | { 62 | BayesTokens *tokens = data; 63 | 64 | if (tokens != NULL) 65 | { 66 | g_hash_table_unref (tokens->tokens); 67 | g_free (tokens); 68 | } 69 | } 70 | 71 | static gpointer 72 | bayes_tokens_copy (gpointer data) 73 | { 74 | BayesTokens *old_tokens = data; 75 | BayesTokens *tokens; 76 | 77 | if (old_tokens == NULL) 78 | return NULL; 79 | 80 | tokens = g_new0 (BayesTokens, 1); 81 | tokens->tokens = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); 82 | 83 | GHashTableIter iter; 84 | gchar *key; 85 | guint *val; 86 | 87 | g_hash_table_iter_init (&iter, old_tokens->tokens); 88 | while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&val)) 89 | g_hash_table_insert (tokens->tokens, g_strdup (key), g_memdup (val, sizeof (*val))); 90 | 91 | tokens->count = old_tokens->count; 92 | 93 | return tokens; 94 | } 95 | 96 | static void 97 | bayes_tokens_inc (BayesTokens *tokens, 98 | const gchar *token, 99 | guint count) 100 | { 101 | guint *token_count; 102 | 103 | /* 104 | * Get the container for the token count or create it if necessary. 105 | */ 106 | if (!(token_count = g_hash_table_lookup (tokens->tokens, token))) 107 | { 108 | token_count = g_new0 (guint, 1); 109 | g_hash_table_insert (tokens->tokens, g_strdup (token), token_count); 110 | } 111 | 112 | /* 113 | * Increment the count of the token. 114 | */ 115 | *token_count += count; 116 | tokens->count += count; 117 | } 118 | 119 | /* 120 | * converts BayesTokens to { "tokens": {}, "count": (int) } 121 | */ 122 | static JsonNode * 123 | bayes_tokens_serialize (gconstpointer boxed) { 124 | const BayesTokens *tokens = boxed; 125 | JsonObject *object; 126 | JsonNode *node; 127 | 128 | if (boxed == NULL) 129 | return json_node_new (JSON_NODE_NULL); 130 | 131 | object = json_object_new (); 132 | node = json_node_new (JSON_NODE_OBJECT); 133 | 134 | /* 135 | * serialize HashTable member 136 | */ 137 | GHashTableIter iter; 138 | gchar *token; /* key */ 139 | guint *count; /* value */ 140 | JsonObject *table_object; 141 | 142 | table_object = json_object_new (); 143 | g_hash_table_iter_init (&iter, tokens->tokens); 144 | while (g_hash_table_iter_next (&iter, (gpointer *) &token, (gpointer *)&count)) 145 | json_object_set_int_member (table_object, token, *count); 146 | 147 | json_object_set_object_member (object, "tokens", table_object); 148 | 149 | /* 150 | * serialize token count 151 | */ 152 | json_object_set_int_member (object, "count", tokens->count); 153 | 154 | json_node_take_object (node, object); 155 | 156 | return node; 157 | } 158 | 159 | static void 160 | bayes_tokens_hashtable_foreach (JsonObject *table_object, 161 | const gchar *name, 162 | JsonNode *node, 163 | gpointer _tokens) 164 | { 165 | BayesTokens *tokens = _tokens; 166 | 167 | bayes_tokens_inc (tokens, name, (guint) json_node_get_int (node)); 168 | } 169 | 170 | /* 171 | * { "tokens": {}, "count": (uint) } 172 | */ 173 | static gpointer 174 | bayes_tokens_deserialize (JsonNode *node) { 175 | JsonObject *object; 176 | BayesTokens *tokens; 177 | 178 | if (json_node_get_node_type (node) != JSON_NODE_OBJECT) 179 | return NULL; 180 | 181 | object = json_node_get_object (node); 182 | 183 | tokens = g_new0(BayesTokens, 1); 184 | tokens->tokens = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); 185 | 186 | /* 187 | * deserialize HashTable member 188 | */ 189 | JsonObject *table_object; 190 | 191 | table_object = json_object_get_object_member (object, "tokens"); 192 | json_object_foreach_member (table_object, bayes_tokens_hashtable_foreach, tokens); 193 | 194 | /* 195 | * unnecessary to deserialize token count 196 | */ 197 | 198 | return tokens; 199 | } 200 | 201 | GType 202 | bayes_tokens_get_type (void) 203 | { 204 | static GType type = 0; 205 | 206 | if (G_UNLIKELY (type == 0)) { 207 | type = g_boxed_type_register_static ("BayesTokens", bayes_tokens_copy, bayes_tokens_free); 208 | 209 | /* 210 | * register transform functions 211 | */ 212 | json_boxed_register_serialize_func (type, JSON_NODE_OBJECT, bayes_tokens_serialize); 213 | json_boxed_register_deserialize_func (type, JSON_NODE_OBJECT, bayes_tokens_deserialize); 214 | } 215 | 216 | return type; 217 | } 218 | 219 | 220 | BayesStorageMemory * 221 | bayes_storage_memory_new (void) 222 | { 223 | return g_object_new (BAYES_TYPE_STORAGE_MEMORY, NULL); 224 | } 225 | 226 | BayesStorageMemory * 227 | bayes_storage_memory_new_from_file (const gchar *filename, 228 | GError **error) 229 | { 230 | JsonParser *parser = json_parser_new (); 231 | JsonNode *node; 232 | GObject *obj; 233 | 234 | if (!json_parser_load_from_file (parser, filename, error)) 235 | return NULL; 236 | 237 | node = json_parser_get_root (parser); 238 | obj = json_gobject_deserialize (BAYES_TYPE_STORAGE_MEMORY, node); 239 | 240 | return BAYES_STORAGE_MEMORY (obj); 241 | } 242 | 243 | BayesStorageMemory * 244 | bayes_storage_memory_new_from_stream (GInputStream *stream, 245 | GCancellable *cancellable, 246 | GError **error) 247 | { 248 | JsonParser *parser = json_parser_new (); 249 | JsonNode *node; 250 | GObject *obj; 251 | 252 | if (!json_parser_load_from_stream (parser, stream, NULL, error)) 253 | return NULL; 254 | 255 | node = json_parser_get_root (parser); 256 | obj = json_gobject_deserialize (BAYES_TYPE_STORAGE_MEMORY, node); 257 | 258 | return BAYES_STORAGE_MEMORY (obj); 259 | } 260 | 261 | gboolean 262 | bayes_storage_memory_save_to_file (BayesStorageMemory *self, 263 | const gchar *filename, 264 | GError **error) 265 | { 266 | JsonGenerator *json_gen; 267 | JsonNode *root; 268 | gboolean ret; 269 | 270 | json_gen = json_generator_new (); 271 | root = json_gobject_serialize (G_OBJECT (self)); 272 | json_generator_set_root (json_gen, root); 273 | 274 | json_node_free (root); 275 | 276 | ret = json_generator_to_file (json_gen, filename, error); 277 | 278 | g_object_unref (json_gen); 279 | 280 | return ret; 281 | } 282 | 283 | 284 | static void 285 | bayes_storage_memory_add_token_count (BayesStorage *storage, 286 | const gchar *name, 287 | const gchar *token, 288 | guint count) 289 | { 290 | BayesStorageMemory *self = (BayesStorageMemory *)storage; 291 | BayesTokens *tokens; 292 | 293 | g_assert (BAYES_IS_STORAGE_MEMORY (self)); 294 | g_assert (name); 295 | g_assert (token); 296 | 297 | /* 298 | * Get the classification hashtable or create it if necessary. 299 | */ 300 | if (!(tokens = g_hash_table_lookup (self->names, name))) 301 | { 302 | tokens = g_new0 (BayesTokens, 1); 303 | tokens->tokens = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); 304 | g_hash_table_insert (self->names, g_strdup (name), tokens); 305 | } 306 | 307 | bayes_tokens_inc (tokens, token, count); 308 | bayes_tokens_inc (self->corpus, token, count); 309 | } 310 | 311 | static guint 312 | bayes_storage_memory_get_token_count (BayesStorage *storage, 313 | const gchar *name, 314 | const gchar *token) 315 | { 316 | BayesStorageMemory *self = (BayesStorageMemory *)storage; 317 | BayesTokens *tokens; 318 | guint *token_count; 319 | 320 | g_assert (BAYES_IS_STORAGE_MEMORY (self)); 321 | 322 | tokens = name ? g_hash_table_lookup(self->names, name) : self->corpus; 323 | 324 | if (tokens != NULL) 325 | { 326 | if (!token) 327 | return tokens->count; 328 | else if ((token_count = g_hash_table_lookup(tokens->tokens, token))) 329 | return *token_count; 330 | } 331 | 332 | return 0; 333 | } 334 | 335 | static gdouble 336 | bayes_storage_memory_get_token_probability (BayesStorage *storage, 337 | const gchar *name, 338 | const gchar *token) 339 | { 340 | BayesStorageMemory *self = (BayesStorageMemory *)storage; 341 | gdouble pool_count; 342 | gdouble them_count; 343 | gdouble tot_count; 344 | gdouble this_count; 345 | gdouble other_count; 346 | gdouble good_metric; 347 | gdouble bad_metric; 348 | gdouble f; 349 | BayesTokens *tokens; 350 | 351 | g_assert (BAYES_IS_STORAGE_MEMORY (self)); 352 | g_assert (name); 353 | g_assert (token); 354 | 355 | if (!(tokens = g_hash_table_lookup(self->names, name))) 356 | return 0.0; 357 | 358 | pool_count = tokens->count; 359 | them_count = MAX (self->corpus->count - pool_count, 1); 360 | this_count = bayes_storage_memory_get_token_count (storage, name, token); 361 | tot_count = bayes_storage_memory_get_token_count (storage, NULL, token); 362 | other_count = tot_count - this_count; 363 | good_metric = (!pool_count) ? 1.0 : MIN (1.0, other_count / pool_count); 364 | bad_metric = MIN (1.0, this_count / them_count); 365 | f = bad_metric / (good_metric + bad_metric); 366 | 367 | if (ABS (f - 0.5) >= 0.1) 368 | return MAX (0.0001, MIN (0.9999, f)); 369 | 370 | return 0.0; 371 | } 372 | 373 | static gchar ** 374 | bayes_storage_memory_get_names (BayesStorage *storage) 375 | { 376 | BayesStorageMemory *self = (BayesStorageMemory *)storage; 377 | GHashTableIter iter; 378 | GPtrArray *ret; 379 | gchar *key; 380 | 381 | g_assert (BAYES_IS_STORAGE_MEMORY (self)); 382 | 383 | ret = g_ptr_array_new (); 384 | g_hash_table_iter_init (&iter, self->names); 385 | while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL)) 386 | g_ptr_array_add (ret, g_strdup (key)); 387 | g_ptr_array_add (ret, NULL); 388 | 389 | return (gchar **)g_ptr_array_free(ret, FALSE); 390 | } 391 | 392 | static void 393 | bayes_storage_hashtable_deserialize_foreach (JsonObject *table_object, 394 | const gchar *member_name, 395 | JsonNode *member_node, 396 | gpointer data) 397 | { 398 | GHashTable *table = data; 399 | BayesTokens *obj; 400 | 401 | obj = json_boxed_deserialize (BAYES_TYPE_TOKENS, member_node); 402 | g_assert (obj != NULL); 403 | 404 | g_hash_table_insert (table, g_strdup (member_name), obj); 405 | } 406 | 407 | static gboolean 408 | bayes_storage_memory_deserialize_property (JsonSerializable *serializable, 409 | const gchar *prop_name, 410 | GValue *value, 411 | GParamSpec *pspec, 412 | JsonNode *prop_node) 413 | { 414 | if (g_strcmp0 (prop_name, "names") == 0) { 415 | GHashTable *table; 416 | JsonObject *obj_table; 417 | 418 | table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, bayes_tokens_free); 419 | obj_table = json_node_get_object (prop_node); 420 | 421 | /* 422 | * deserialize each key=>val pair 423 | */ 424 | json_object_foreach_member (obj_table, bayes_storage_hashtable_deserialize_foreach, table); 425 | 426 | // g_value_init (value, G_TYPE_HASH_TABLE); 427 | g_assert (G_VALUE_TYPE (value) == G_TYPE_HASH_TABLE); 428 | g_value_set_boxed (value, table); 429 | 430 | return TRUE; 431 | } else if (g_strcmp0 (prop_name, "corpus") == 0) { 432 | gpointer boxed; 433 | 434 | // g_value_init (value, BAYES_TYPE_TOKENS); 435 | g_assert (G_VALUE_TYPE (value) == BAYES_TYPE_TOKENS); 436 | boxed = json_boxed_deserialize (BAYES_TYPE_TOKENS, prop_node); 437 | g_assert (boxed != NULL); 438 | 439 | g_value_set_boxed (value, boxed); 440 | return TRUE; 441 | } else 442 | return serializable_iface->deserialize_property (serializable, prop_name, 443 | value, pspec, prop_node); 444 | } 445 | 446 | static JsonNode * 447 | bayes_storage_memory_serialize_property (JsonSerializable *serializable, 448 | const gchar *prop_name, 449 | const GValue *value, 450 | GParamSpec *pspec) 451 | { 452 | JsonNode *node = NULL; 453 | 454 | // { "names": {}, "corpus": {} } 455 | if (g_strcmp0 (prop_name, "names") == 0) { 456 | GHashTable *boxed; // HashTable 457 | JsonObject *table_obj; // serialized hashtable 458 | JsonNode *val; // BayesTokens struct 459 | 460 | table_obj = json_object_new (); 461 | node = json_node_new (JSON_NODE_OBJECT); 462 | 463 | boxed = g_value_get_boxed (value); 464 | 465 | // serialize all entries 466 | GHashTableIter iter; 467 | gchar *name; 468 | BayesTokens *tokens; 469 | 470 | g_hash_table_iter_init (&iter, boxed); 471 | while (g_hash_table_iter_next (&iter, (gpointer *)&name, (gpointer *)&tokens)) { 472 | val = json_boxed_serialize (BAYES_TYPE_TOKENS, tokens); 473 | json_object_set_member (table_obj, name, val); 474 | } 475 | 476 | json_node_take_object (node, table_obj); 477 | } else if (g_strcmp0 (prop_name, "corpus") == 0) { 478 | BayesTokens *boxed; 479 | 480 | boxed = g_value_get_boxed (value); 481 | node = json_boxed_serialize (BAYES_TYPE_TOKENS, boxed); 482 | } else 483 | node = serializable_iface->serialize_property (serializable, prop_name, value, pspec); 484 | 485 | return node; 486 | } 487 | 488 | static void 489 | bayes_storage_memory_get_property (GObject *object, 490 | guint prop_id, 491 | GValue *value, 492 | GParamSpec *pspec) 493 | { 494 | BayesStorageMemory *self = (BayesStorageMemory *) object; 495 | 496 | switch (prop_id) { 497 | case PROP_NAMES: 498 | g_value_set_boxed (value, self->names); 499 | break; 500 | case PROP_CORPUS: 501 | g_value_set_boxed (value, self->corpus); 502 | break; 503 | default: 504 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 505 | } 506 | } 507 | 508 | static void 509 | bayes_storage_memory_set_property (GObject *object, 510 | guint prop_id, 511 | const GValue *value, 512 | GParamSpec *pspec) 513 | { 514 | BayesStorageMemory *self = (BayesStorageMemory *) object; 515 | 516 | switch (prop_id) { 517 | case PROP_NAMES: 518 | self->names = g_value_dup_boxed (value); 519 | g_object_notify_by_pspec (object, obj_properties [PROP_NAMES]); 520 | break; 521 | case PROP_CORPUS: 522 | self->corpus = g_value_dup_boxed (value); 523 | g_object_notify_by_pspec (object, obj_properties [PROP_CORPUS]); 524 | break; 525 | default: 526 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 527 | } 528 | } 529 | 530 | static void 531 | bayes_storage_memory_finalize (GObject *object) 532 | { 533 | BayesStorageMemory *self = (BayesStorageMemory *)object; 534 | 535 | g_hash_table_unref (self->names); 536 | bayes_tokens_free (self->corpus); 537 | 538 | G_OBJECT_CLASS (bayes_storage_memory_parent_class)->finalize (object); 539 | } 540 | 541 | static void 542 | bayes_storage_memory_class_init (BayesStorageMemoryClass *klass) 543 | { 544 | GObjectClass *object_class = G_OBJECT_CLASS(klass); 545 | 546 | object_class->finalize = bayes_storage_memory_finalize; 547 | 548 | object_class->set_property = bayes_storage_memory_set_property; 549 | object_class->get_property = bayes_storage_memory_get_property; 550 | 551 | /** 552 | * BayesStorageMemory:names: (type GLib.HashTable(utf8,Bayes.Tokens)) 553 | * 554 | * The list of class names and their tokens. 555 | */ 556 | obj_properties[PROP_NAMES] = 557 | g_param_spec_boxed ("names", "Names", 558 | "A table of classes", 559 | G_TYPE_HASH_TABLE, 560 | G_PARAM_READWRITE | G_PARAM_PRIVATE | 561 | G_PARAM_STATIC_STRINGS); 562 | /** 563 | * BayesStorageMemory:corpus: 564 | * 565 | * Corpus 566 | */ 567 | obj_properties[PROP_CORPUS] = 568 | g_param_spec_boxed ("corpus", "Corpus", 569 | "All tokens among classes", 570 | BAYES_TYPE_TOKENS, 571 | G_PARAM_READWRITE | G_PARAM_PRIVATE | 572 | G_PARAM_STATIC_STRINGS); 573 | g_object_class_install_properties (object_class, 574 | N_PROPERTIES, obj_properties); 575 | } 576 | 577 | static GParamSpec * 578 | bayes_storage_memory_find_property (JsonSerializable *serializable, 579 | const gchar *name) 580 | { 581 | BayesStorageMemory *self; 582 | GObjectClass *klass; 583 | 584 | self = BAYES_STORAGE_MEMORY (serializable); 585 | g_assert (self != NULL); 586 | 587 | klass = G_OBJECT_GET_CLASS (self); 588 | return g_object_class_find_property (klass, name); 589 | } 590 | 591 | static void 592 | bayes_storage_memory_init (BayesStorageMemory *self) 593 | { 594 | BayesTokens *tokens; 595 | 596 | self->names = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, bayes_tokens_free); 597 | tokens = g_new0(BayesTokens, 1); 598 | tokens->tokens = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); 599 | self->corpus = tokens; 600 | } 601 | 602 | static void 603 | bayes_storage_init (BayesStorageInterface *iface) 604 | { 605 | iface->add_token_count = bayes_storage_memory_add_token_count; 606 | iface->get_names = bayes_storage_memory_get_names; 607 | iface->get_token_count = bayes_storage_memory_get_token_count; 608 | iface->get_token_probability = bayes_storage_memory_get_token_probability; 609 | } 610 | 611 | static void json_serializable_iface_init (JsonSerializableIface *iface) { 612 | serializable_iface = g_type_default_interface_peek (JSON_TYPE_SERIALIZABLE); 613 | 614 | iface->serialize_property = bayes_storage_memory_serialize_property; 615 | iface->deserialize_property = bayes_storage_memory_deserialize_property; 616 | iface->find_property = bayes_storage_memory_find_property; 617 | } 618 | --------------------------------------------------------------------------------