### Downloading, Compiling, and Installing Boost Source: https://github.com/kpu/kenlm/blob/master/lm/builder/README.md These commands download, extract, compile, and install Boost from source. This is an alternative to using a package manager and allows for more control over the Boost version and installation location. It downloads version 1.52.0. ```bash wget http://sourceforge.net/projects/boost/files/boost/1.52.0/boost_1_52_0.tar.gz/download -O boost_1_52_0.tar.gz tar -xvzf boost_1_52_0.tar.gz cd boost_1_52_0 ./bootstrap.sh ./b2 sudo ./b2 install ``` -------------------------------- ### Installing the kenlm_util Library Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code defines the installation rules for the kenlm_util library. It specifies where the library and its headers should be installed. The TARGETS keyword specifies the target to install, EXPORT specifies the export set, and DESTINATION specifies the installation directory. ```cmake install( TARGETS kenlm_util EXPORT kenlmTargets RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib INCLUDES DESTINATION include ) ``` -------------------------------- ### Building KenLM using vcpkg Source: https://github.com/kpu/kenlm/blob/master/README.md Downloads, installs, and integrates KenLM using the vcpkg dependency manager. This involves cloning the vcpkg repository, bootstrapping vcpkg, integrating the installation, and installing the kenlm package. ```bash git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ./vcpkg install kenlm ``` -------------------------------- ### Installing kenlm Target Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet configures the installation process for the kenlm library, specifying the destination directories for runtime, library, archive, and include files. ```cmake install( TARGETS kenlm EXPORT kenlmTargets RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib INCLUDES DESTINATION include ) ``` -------------------------------- ### Installing kenlm Builder Library Source: https://github.com/kpu/kenlm/blob/master/lm/builder/CMakeLists.txt This snippet configures the installation of the kenlm_builder library, specifying the destination directories for runtime binaries, libraries, archives, and include files. It also exports the kenlmTargets for use by other CMake projects. ```cmake install( TARGETS kenlm_builder EXPORT kenlmTargets RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib INCLUDES DESTINATION include ) ``` -------------------------------- ### Installing Boost Dependencies on Ubuntu Source: https://github.com/kpu/kenlm/blob/master/lm/builder/README.md This command installs the Boost library (version 1.48 or later) and its development files on Ubuntu. Boost is a required dependency for building KenLM. ```bash sudo apt-get install libboost1.48-all-dev ``` -------------------------------- ### Installation Targets for kenlm_interpolate Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet defines the installation targets for the `kenlm_interpolate` library, specifying where the library, headers, and executables should be installed. ```cmake install( TARGETS kenlm_interpolate EXPORT kenlmTargets RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib INCLUDES DESTINATION include ) ``` -------------------------------- ### Installing Headers Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Installs header files from specified subdirectories to the include directory. This makes the header files available for other projects to include. ```cmake foreach(SUBDIR IN ITEMS util util/double-conversion util/stream lm lm/builder lm/common lm/filter lm/interpolate) file(GLOB HEADERS ${CMAKE_CURRENT_LIST_DIR}/${SUBDIR}/*.h ${CMAKE_CURRENT_LIST_DIR}/${SUBDIR}/*.hh) install(FILES ${HEADERS} DESTINATION include/kenlm/${SUBDIR} COMPONENT headers) endforeach(SUBDIR) ``` -------------------------------- ### Installing Targets Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Installs the kenlmTargets export set, which contains information about the project's targets for use by other CMake projects. This allows other projects to easily link against kenlm. ```cmake install(EXPORT kenlmTargets FILE kenlmTargets.cmake NAMESPACE kenlm:: DESTINATION share/kenlm/cmake ) ``` -------------------------------- ### Installing KenLM via pip Source: https://github.com/kpu/kenlm/blob/master/README.md Installs the KenLM language model toolkit from the master branch of the GitHub repository using pip. The MAX_ORDER environment variable controls the maximum order with which KenLM was built. ```bash pip install https://github.com/kpu/kenlm/archive/master.zip ``` -------------------------------- ### Creating kenlm Builder Library Source: https://github.com/kpu/kenlm/blob/master/lm/builder/CMakeLists.txt This snippet creates the kenlm_builder library from the source files defined in KENLM_BUILDER_SOURCE. It then links the library with its dependencies (kenlm, kenlm_util, Threads) and sets the include directories for installation. ```cmake add_library(kenlm_builder ${KENLM_BUILDER_SOURCE}) target_link_libraries(kenlm_builder PUBLIC kenlm kenlm_util Threads::Threads) # Since headers are relative to `include/kenlm` at install time, not just `include` target_include_directories(kenlm_builder PUBLIC $) ``` -------------------------------- ### Installing the kenlm Filter Library Source: https://github.com/kpu/kenlm/blob/master/lm/filter/CMakeLists.txt This snippet defines the installation rules for the kenlm_filter library. It specifies the destination directories for runtime binaries, libraries, archives, and include files. It also exports the kenlmTargets for use by other CMake projects. ```cmake install( TARGETS kenlm_filter EXPORT kenlmTargets RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib INCLUDES DESTINATION include ) ``` -------------------------------- ### Creating kenlm Library Target Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet creates the kenlm library target, sets properties for position-independent code, links necessary libraries, and defines include directories for installation. ```cmake add_library(kenlm ${KENLM_LM_SOURCE} ${KENLM_LM_COMMON_SOURCE}) set_target_properties(kenlm PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(kenlm PUBLIC kenlm_util Threads::Threads) # Since headers are relative to `include/kenlm` at install time, not just `include` target_include_directories(kenlm PUBLIC $) ``` -------------------------------- ### Configuring Package Configuration File Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Configures and installs the package configuration file. This file is used by other CMake projects to find and use the kenlm library. ```cmake # Config include(CMakePackageConfigHelpers) # generate the config file that is includes the exports configure_package_config_file(${PROJECT_SOURCE_DIR}/cmake/kenlmConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/kenlmConfig.cmake" INSTALL_DESTINATION share/kenlm/cmake NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO ) # install the configuration file install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kenlmConfig.cmake DESTINATION share/kenlm/cmake ) ``` -------------------------------- ### Creating and Linking the kenlm Filter Library Source: https://github.com/kpu/kenlm/blob/master/lm/filter/CMakeLists.txt This snippet creates the kenlm_filter library from the specified source files and links it with the kenlm_util library. It also sets the include directories for the library, ensuring that headers are correctly located relative to `include/kenlm` at install time. ```cmake add_library(kenlm_filter ${KENLM_FILTER_SOURCE}) target_link_libraries(kenlm_filter PUBLIC kenlm_util) # Since headers are relative to `include/kenlm` at install time, not just `include` target_include_directories(kenlm_filter PUBLIC $) ``` -------------------------------- ### Installing kenlm_python Module Source: https://github.com/kpu/kenlm/blob/master/python/CMakeLists.txt This snippet installs the kenlm_python module to the determined Python site-packages directory. This makes the module available for import in Python scripts. ```cmake install(TARGETS kenlm_python DESTINATION ${PYTHON_SITE_PACKAGES}) ``` -------------------------------- ### Building KenLM with bjam Source: https://github.com/kpu/kenlm/blob/master/lm/builder/README.md This command builds the KenLM tool using the bjam build system. bjam and boost-build are required and might be packaged separately from Boost. ```bash bjam ``` -------------------------------- ### Creating the kenlm_util Library Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This command creates the kenlm_util library, linking the source files defined in KENLM_UTIL_SOURCE, KENLM_UTIL_DOUBLECONVERSION_SOURCE, and KENLM_UTIL_STREAM_SOURCE. It also sets the include directories for the library, making the headers available to other parts of the project. The PUBLIC keyword makes the include directory available to downstream projects. ```cmake add_library(kenlm_util ${KENLM_UTIL_DOUBLECONVERSION_SOURCE} ${KENLM_UTIL_STREAM_SOURCE} ${KENLM_UTIL_SOURCE}) # Since headers are relative to `include/kenlm` at install time, not just `include` target_include_directories(kenlm_util PUBLIC $) ``` -------------------------------- ### Setting Up Boost Tests Source: https://github.com/kpu/kenlm/blob/master/lm/builder/CMakeLists.txt This snippet sets up Boost-based tests if the BUILD_TESTING option is enabled. It lists the test files and links them against the necessary libraries (kenlm_builder, kenlm, kenlm_util, Threads). The AddTests macro is assumed to be defined elsewhere. ```cmake if(BUILD_TESTING) # Explicitly list the Boost test files to be compiled set(KENLM_BOOST_TESTS_LIST adjust_counts_test corpus_count_test ) AddTests(TESTS ${KENLM_BOOST_TESTS_LIST} LIBRARIES kenlm_builder kenlm kenlm_util Threads::Threads) endif() ``` -------------------------------- ### Running lmplz to Build Language Model Source: https://github.com/kpu/kenlm/blob/master/lm/builder/README.md This command runs the lmplz tool to build a 5-gram language model from the input text. The input text is read from standard input, and the resulting ARPA file is written to text.arpa. ```bash bin/lmplz -o 5 text.arpa ``` -------------------------------- ### Compiling KenLM with CMake Source: https://github.com/kpu/kenlm/blob/master/README.md These commands create a build directory, navigate into it, use CMake to generate build files, and then compile the project using make with 4 parallel jobs. This is the standard method for building KenLM. ```bash mkdir -p build cd build cmake .. make -j 4 ``` -------------------------------- ### Adding Tests with Libraries and Arguments Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet adds tests using a custom function `AddTests`, linking them with specified libraries and providing test arguments. ```cmake if(BUILD_TESTING) set(KENLM_BOOST_TESTS_LIST left_test partial_test) AddTests(TESTS ${KENLM_BOOST_TESTS_LIST} LIBRARIES ${LM_LIBS} TEST_ARGS ${CMAKE_CURRENT_SOURCE_DIR}/test.arpa) # model_test requires an extra command line parameter KenLMAddTest(TEST model_test LIBRARIES ${LM_LIBS} TEST_ARGS ${CMAKE_CURRENT_SOURCE_DIR}/test.arpa ${CMAKE_CURRENT_SOURCE_DIR}/test_nounk.arpa) endif() ``` -------------------------------- ### Adding Unit Tests Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code adds unit tests for the kenlm_util library, but only if testing is enabled (BUILD_TESTING). It defines a list of test executables and links them with the kenlm_util and Threads libraries. It also adds a special test case for file_piece_test, which requires an extra command-line parameter. ```cmake if(BUILD_TESTING) set(KENLM_BOOST_TESTS_LIST bit_packing_test integer_to_string_test joint_sort_test multi_intersection_test pcqueue_test probing_hash_table_test read_compressed_test sized_iterator_test sorted_uniform_test string_stream_test tokenize_piece_test ) AddTests(TESTS ${KENLM_BOOST_TESTS_LIST} LIBRARIES kenlm_util Threads::Threads) # file_piece_test requires an extra command line parameter KenLMAddTest(TEST file_piece_test LIBRARIES kenlm_util Threads::Threads TEST_ARGS ${CMAKE_CURRENT_SOURCE_DIR}/file_piece.cc) endif() ``` -------------------------------- ### Finding Boost Components Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Finds the Boost library with a minimum version of 1.41.0 and requires specific components: program_options, system, thread, and unit_test_framework. Boost is a collection of C++ libraries. ```cmake # We need boost find_package(Boost 1.41.0 REQUIRED COMPONENTS program_options system thread unit_test_framework ) ``` -------------------------------- ### Setting Libraries for Linking Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet defines the libraries to be linked with the executables. ```cmake set(LM_LIBS kenlm kenlm_util Threads::Threads) ``` -------------------------------- ### Including CMake Helper Functions Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Includes a CMake script containing helper functions for the kenlm project. This allows for reusable CMake code to be organized and included in the main CMakeLists.txt file. ```cmake # Add our CMake helper functions include(cmake/KenLMFunctions.cmake) ``` -------------------------------- ### Creating and Linking the kenlm_interpolate Library Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet creates the `kenlm_interpolate` library from the defined source files and links it with the Eigen3 library. It also sets the include directories for the library. ```cmake add_library(kenlm_interpolate ${KENLM_INTERPOLATE_SOURCE}) target_link_libraries(kenlm_interpolate PUBLIC kenlm Eigen3::Eigen) # Since headers are relative to `include/kenlm` at install time, not just `include` target_include_directories(kenlm_interpolate PUBLIC $) ``` -------------------------------- ### Setting Executable Files Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet defines the list of executable files to be compiled. ```cmake set(EXE_LIST query fragment build_binary kenlm_benchmark ) ``` -------------------------------- ### Adding Executables with Libraries Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet uses a custom function `AddExes` to create executables and link them with the specified libraries. ```cmake AddExes(EXES ${EXE_LIST} LIBRARIES ${LM_LIBS}) ``` -------------------------------- ### Finding and Linking ZLIB Dependency Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code finds the ZLIB library and, if found, sets compiler flags and links the library to kenlm_util. It also includes the ZLIB include directory. The PRIVATE keyword ensures that ZLIB is only linked to kenlm_util and not to downstream projects. ```cmake set(READ_COMPRESSED_FLAGS) find_package(ZLIB) if (ZLIB_FOUND) set(READ_COMPRESSED_FLAGS "${READ_COMPRESSED_FLAGS} -DHAVE_ZLIB") target_link_libraries(kenlm_util PRIVATE ${ZLIB_LIBRARIES}) include_directories(${ZLIB_INCLUDE_DIR}) endif() ``` -------------------------------- ### Setting Source Files for kenlm_util Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake command sets the source files that will be compiled into the kenlm_util library. It explicitly lists the .cc files that constitute the utility library. The WIN32 conditional adds getopt.c to the source list when building on Windows. ```cmake set(KENLM_UTIL_SOURCE bit_packing.cc ersatz_progress.cc exception.cc file.cc file_piece.cc float_to_string.cc integer_to_string.cc mmap.cc murmur_hash.cc parallel_read.cc pool.cc read_compressed.cc scoped.cc spaces.cc string_piece.cc usage.cc ) if (WIN32) set(KENLM_UTIL_SOURCE ${KENLM_UTIL_SOURCE} getopt.c) endif() ``` -------------------------------- ### Configuring Boost Tests for KenLM Source: https://github.com/kpu/kenlm/blob/master/util/stream/CMakeLists.txt This snippet configures Boost tests for the KenLM library if the BUILD_TESTING option is enabled. It defines a list of test files and uses the AddTests macro to add them to the test suite, linking against necessary libraries. ```cmake if(BUILD_TESTING) # Explicitly list the Boost test files to be compiled set(KENLM_BOOST_TESTS_LIST io_test sort_test stream_test rewindable_stream_test ) AddTests(TESTS ${KENLM_BOOST_TESTS_LIST} LIBRARIES kenlm_util ${Boost_LIBRARIES} Threads::Threads) endif() ``` -------------------------------- ### Finding and Linking LibLZMA Dependency Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code finds the LibLZMA library and, if found, sets compiler flags and links the library to kenlm_util. It also includes the LibLZMA include directories. The PRIVATE keyword ensures that LibLZMA is only linked to kenlm_util and not to downstream projects. ```cmake find_package(LibLZMA) if (LIBLZMA_FOUND) set(READ_COMPRESSED_FLAGS "${READ_COMPRESSED_FLAGS} -DHAVE_XZLIB") target_link_libraries(kenlm_util PRIVATE ${LIBLZMA_LIBRARIES}) include_directories(${LIBLZMA_INCLUDE_DIRS}) endif() ``` -------------------------------- ### Finding and Linking BZip2 Dependency Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code finds the BZip2 library and, if found, sets compiler flags and links the library to kenlm_util. It also includes the BZip2 include directory. The PRIVATE keyword ensures that BZip2 is only linked to kenlm_util and not to downstream projects. ```cmake find_package(BZip2) if (BZIP2_FOUND) set(READ_COMPRESSED_FLAGS "${READ_COMPRESSED_FLAGS} -DHAVE_BZLIB") target_link_libraries(kenlm_util PRIVATE ${BZIP2_LIBRARIES}) include_directories(${BZIP2_INCLUDE_DIR}) endif() ``` -------------------------------- ### Adding Tests for kenlm_interpolate Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet adds tests for the `kenlm_interpolate` library, linking them with the library and the Threads library. It also includes a special case for `tune_instances_test` which requires an extra command line argument. ```cmake if(BUILD_TESTING) AddTests(TESTS backoff_reunification_test bounded_sequence_encoding_test merge_vocab_test normalize_test tune_derivatives_test LIBRARIES ${KENLM_INTERPOLATE_LIBS} Threads::Threads) # tune_instances_test needs an extra command line parameter KenLMAddTest(TEST tune_instances_test LIBRARIES ${KENLM_INTERPOLATE_LIBS} TEST_ARGS -- ${CMAKE_CURRENT_SOURCE_DIR}/../common/test_data) endif() ``` -------------------------------- ### Setting Source Files for kenlm Library Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet defines the source files that comprise the kenlm library. These files are then used to create the library target. ```cmake set(KENLM_LM_SOURCE bhiksha.cc binary_format.cc config.cc lm_exception.cc model.cc quantize.cc read_arpa.cc search_hashed.cc search_trie.cc sizes.cc trie.cc trie_sort.cc value_build.cc virtual_interface.cc vocab.cc ) ``` -------------------------------- ### Configuring Static Linking Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Configures the project to build static executables. It sets Boost to use static libraries, sets global properties for static linking, and adjusts compiler flags for MSVC to use the /MT flag instead of /MD. ```cmake if (FORCE_STATIC) #presumably overkill, is there a better way? #http://cmake.3232098.n2.nabble.com/Howto-compile-static-executable-td5580269.html set(Boost_USE_STATIC_LIBS ON) set_property(GLOBAL PROPERTY LINK_SEARCH_START_STATIC ON) set_property(GLOBAL PROPERTY LINK_SEARCH_END_STATIC ON) set(BUILD_SHARED_LIBRARIES OFF) if (MSVC) set(flag_vars CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO) foreach(flag_var ${flag_vars}) if(${flag_var} MATCHES "/MD") string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") endif(${flag_var} MATCHES "/MD") endforeach(flag_var) else (MSVC) if (NOT CMAKE_C_COMPILER_ID MATCHES ".*Clang") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++ -static") endif () set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") endif () #Annoyingly the exectuables say "File not found" unless these are set set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS) set(CMAKE_SHARED_LIBRARY_C_FLAGS) set(CMAKE_SHARED_LIBRARY_CXX_FLAGS) set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS) set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS) endif () ``` -------------------------------- ### Defining Compile Definitions for kenlm Source: https://github.com/kpu/kenlm/blob/master/lm/CMakeLists.txt This snippet defines a compile-time definition for the kenlm library, setting the maximum order. ```cmake target_compile_definitions(kenlm PUBLIC -DKENLM_MAX_ORDER=${KENLM_MAX_ORDER}) ``` -------------------------------- ### Setting Include Directories Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Adds the Boost include directories to the project's include path. This allows the compiler to find the Boost header files. ```cmake # Define where include files live include_directories(${Boost_INCLUDE_DIRS}) ``` -------------------------------- ### Adding Executables lmplz and count_ngrams Source: https://github.com/kpu/kenlm/blob/master/lm/builder/CMakeLists.txt This snippet adds the lmplz and count_ngrams executables, linking them against the kenlm_builder, kenlm, kenlm_util, and Threads libraries. The AddExes macro is assumed to be defined elsewhere. ```cmake AddExes(EXES lmplz LIBRARIES kenlm_builder kenlm kenlm_util Threads::Threads) AddExes(EXES count_ngrams LIBRARIES kenlm_builder kenlm kenlm_util Threads::Threads) ``` -------------------------------- ### Defining Build Options Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Defines various build options for the project, such as building static executables, compiling tests, enabling Python bindings, and building a standalone Python library. These options can be toggled during the CMake configuration process. ```cmake option(FORCE_STATIC "Build static executables" OFF) option(COMPILE_TESTS "Compile tests" OFF) option(ENABLE_PYTHON "Build Python bindings" OFF) option(BUILD_PYTHON_STANDALONE "Build standalone C++ lib with Python install" OFF) ``` -------------------------------- ### Defining Executables and Linking Libraries Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet defines the executables that will be built and links them with the `kenlm_interpolate` library. ```cmake set(KENLM_INTERPOLATE_EXES interpolate streaming_example) set(KENLM_INTERPOLATE_LIBS kenlm_interpolate) AddExes(EXES ${KENLM_INTERPOLATE_EXES} LIBRARIES ${KENLM_INTERPOLATE_LIBS}) ``` -------------------------------- ### Setting Executable Output Path Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Sets the output path for executables to the 'bin' directory within the project's binary directory. This ensures that all compiled executables are placed in a consistent location. ```cmake # Compile all executables into bin/ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) ``` -------------------------------- ### Basic KenLM Usage in Python Source: https://github.com/kpu/kenlm/blob/master/README.md Demonstrates basic usage of the KenLM language model in Python. It loads a KenLM model from a file and scores a sentence, including beginning-of-sentence (bos) and end-of-sentence (eos) markers. ```python import kenlm model = kenlm.Model('lm/test.arpa') print(model.score('this is a sentence .', bos = True, eos = True)) ``` -------------------------------- ### Adding Executables (Non-Windows) Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code adds executables, specifically probing_hash_table_benchmark, but only if the target platform is not Windows (WIN32). It links the executable with the kenlm_util and Threads libraries. ```cmake if (NOT WIN32) AddExes(EXES probing_hash_table_benchmark LIBRARIES kenlm_util Threads::Threads) endif() ``` -------------------------------- ### Adding Subdirectories for Dependencies Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt These commands add the double-conversion and stream subdirectories to the build process. This is necessary to include these dependencies as part of the kenlm_util library. These subdirectories likely contain their own CMakeLists.txt files to define their build processes. ```cmake add_subdirectory(double-conversion) add_subdirectory(stream) ``` -------------------------------- ### Setting Library Output Path Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Sets the output path for libraries to the 'lib' directory within the project's binary directory. This ensures that all compiled libraries are placed in a consistent location. ```cmake # Compile all libraries into lib/ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) ``` -------------------------------- ### Setting Source File List with CMake Source: https://github.com/kpu/kenlm/blob/master/util/double-conversion/CMakeLists.txt This CMake command sets the list of source files for the double conversion utility. It uses the set command to define the KENLM_UTIL_DOUBLECONVERSION_SOURCE variable, listing all relevant .cc files. The PARENT_SCOPE option makes this variable available to the parent directory's CMake configuration. ```CMake set(KENLM_UTIL_DOUBLECONVERSION_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/bignum-dtoa.cc ${CMAKE_CURRENT_SOURCE_DIR}/bignum.cc ${CMAKE_CURRENT_SOURCE_DIR}/cached-powers.cc ${CMAKE_CURRENT_SOURCE_DIR}/fast-dtoa.cc ${CMAKE_CURRENT_SOURCE_DIR}/fixed-dtoa.cc ${CMAKE_CURRENT_SOURCE_DIR}/strtod.cc ${CMAKE_CURRENT_SOURCE_DIR}/double-to-string.cc ${CMAKE_CURRENT_SOURCE_DIR}/string-to-double.cc PARENT_SCOPE) ``` -------------------------------- ### Creating and Configuring kenlm_python Module Source: https://github.com/kpu/kenlm/blob/master/python/CMakeLists.txt This snippet creates a shared library named kenlm_python from the specified source files. It sets the output name to kenlm and removes the default prefix. Platform-specific suffixes are applied for Apple (.so) and Windows (.pyd). ```cmake add_library(kenlm_python MODULE kenlm.cpp score_sentence.cc) set_target_properties(kenlm_python PROPERTIES OUTPUT_NAME kenlm) set_target_properties(kenlm_python PROPERTIES PREFIX "") if(APPLE) set_target_properties(kenlm_python PROPERTIES SUFFIX ".so") elif(WIN32) set_target_properties(kenlm_python PROPERTIES SUFFIX ".pyd") endif() ``` -------------------------------- ### Windows-Specific Boost Configuration Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Configures Boost library usage on Windows. It disables static linking, turns off auto-linking features, and explicitly defines dynamic linking for specific Boost libraries to avoid header configuration issues. ```cmake if (WIN32) set(Boost_USE_STATIC_LIBS OFF) # The auto-linking feature has problems with USE_STATIC_LIBS off, so we use # BOOST_ALL_NO_LIB to turn it off. # Several boost libraries headers aren't configured correctly if # USE_STATIC_LIBS is off, so we explicitly say they are dynamic with the # remaining definitions. add_definitions(-DBOOST_ALL_NO_LIB -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_IOSTREAMS_DYN_LINK -DBOOST_THREAD_DYN_LINK) endif( ) ``` -------------------------------- ### Defining Source Files for kenlm_interpolate Library Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet defines the source files that will be compiled into the `kenlm_interpolate` library. These files implement various interpolation-related functionalities. ```cmake set(KENLM_INTERPOLATE_SOURCE backoff_reunification.cc bounded_sequence_encoding.cc merge_probabilities.cc merge_vocab.cc normalize.cc pipeline.cc split_worker.cc tune_derivatives.cc tune_instances.cc tune_weights.cc universal_vocab.cc) ``` -------------------------------- ### Estimating Language Models with lmplz Source: https://github.com/kpu/kenlm/blob/master/README.md This command uses the lmplz tool to estimate an unpruned language model with modified Kneser-Ney smoothing. The -o 5 option specifies the maximum order of n-grams to be considered (in this case, 5). The input text is read from text.arpa ``` -------------------------------- ### Setting Source Files for kenlm Builder Source: https://github.com/kpu/kenlm/blob/master/lm/builder/CMakeLists.txt This snippet explicitly lists the source files that will be included in the kenlm builder library. The CMAKE_CURRENT_SOURCE_DIR variable is used to ensure correct paths, especially when referenced by parent CMake files. ```cmake set(KENLM_BUILDER_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/adjust_counts.cc ${CMAKE_CURRENT_SOURCE_DIR}/corpus_count.cc ${CMAKE_CURRENT_SOURCE_DIR}/initial_probabilities.cc ${CMAKE_CURRENT_SOURCE_DIR}/interpolate.cc ${CMAKE_CURRENT_SOURCE_DIR}/output.cc ${CMAKE_CURRENT_SOURCE_DIR}/pipeline.cc ) ``` -------------------------------- ### Setting C++ Standard Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Sets the C++ standard for the project. This ensures that the compiler uses the specified C++ standard (C++11 in this case) and enforces standard compliance. ```cmake set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) ``` -------------------------------- ### Setting Minimum CMake Version Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Specifies the minimum required version of CMake for the project. This ensures that the CMake version used to build the project is compatible with the commands and features used in the CMakeLists.txt file. ```cmake cmake_minimum_required(VERSION 3.10) ``` -------------------------------- ### Eigen3 Dependency Check and Include Directories Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet checks for the Eigen3 library (version 3.1.0 or higher) and includes its directories. It's a prerequisite for the kenlm interpolation module. ```cmake if(ENABLE_INTERPOLATE) find_package(Eigen3 3.1.0 CONFIG REQUIRED) include_directories(${EIGEN3_INCLUDE_DIR}) ``` -------------------------------- ### Enabling Testing Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Includes CTest and enables testing if the COMPILE_TESTS option is enabled. This allows unit tests to be compiled and run as part of the build process. ```cmake if (COMPILE_TESTS) # Tell cmake that we want unit tests to be compiled include(CTest) enable_testing() endif() ``` -------------------------------- ### Adding Test for model_buffer Source: https://github.com/kpu/kenlm/blob/master/lm/common/CMakeLists.txt This CMake command adds a test case for the model_buffer component. It uses the KenLMAddTest macro, specifying the test name, the libraries to link against (kenlm), and the test arguments (the path to the test data). This test is only included if the BUILD_TESTING option is enabled. ```cmake if(BUILD_TESTING) KenLMAddTest(TEST model_buffer_test LIBRARIES kenlm TEST_ARGS ${CMAKE_CURRENT_SOURCE_DIR}/test_data) endif() ``` -------------------------------- ### Adding Subdirectories Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Adds subdirectories to the build process. This allows the project to be organized into modules, each with its own CMakeLists.txt file. ```cmake # Process subdirectories add_subdirectory(util) add_subdirectory(lm) if(ENABLE_PYTHON) add_subdirectory(python) endif() ``` -------------------------------- ### Finding Python Dependencies with CMake Source: https://github.com/kpu/kenlm/blob/master/python/CMakeLists.txt This snippet uses CMake's find_package command to locate the Python interpreter and libraries. It sets the required version and includes the necessary directories for compilation. ```cmake find_package(PythonInterp REQUIRED) find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT REQUIRED) include_directories(${PYTHON_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}) ``` -------------------------------- ### Finding Threads Library Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Finds the Threads library and sets a preference for Pthreads. This is necessary for multi-threaded applications. ```cmake set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) ``` -------------------------------- ### Defining CMake Project Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Defines the CMake project name. This sets the project's name to 'kenlm', which is used in various build processes and configurations. ```cmake # Define a single cmake project project(kenlm) ``` -------------------------------- ### Setting Target Properties and Linking Libraries Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code sets the POSITION_INDEPENDENT_CODE property to ON for the kenlm_util library, which is required for shared libraries. It also links the library to Boost, Threads, and RT (if available). The PUBLIC keyword makes Boost available to downstream projects, while PRIVATE makes Threads and RT only available to kenlm_util. ```cmake set_target_properties(kenlm_util PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(kenlm_util PUBLIC # Boost is required for building binaries and tests "$" PRIVATE Threads::Threads ${RT}) ``` -------------------------------- ### Setting Maximum Ngram Order Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Sets the maximum supported ngram order for the kenlm project. This value is cached and can be configured by the user. ```cmake set(KENLM_MAX_ORDER 6 CACHE STRING "Maximum supported ngram order") ``` -------------------------------- ### Setting Source Files for kenlm Filter Source: https://github.com/kpu/kenlm/blob/master/lm/filter/CMakeLists.txt This snippet defines the source files that make up the kenlm_filter library. It uses the CMAKE_CURRENT_SOURCE_DIR variable to ensure correct paths, especially when referenced from parent directories. The source files include arpa_io.cc, phrase.cc, and vocab.cc. ```cmake set(KENLM_FILTER_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/arpa_io.cc ${CMAKE_CURRENT_SOURCE_DIR}/phrase.cc ${CMAKE_CURRENT_SOURCE_DIR}/vocab.cc ) ``` -------------------------------- ### Setting Source Files for KenLM Library Source: https://github.com/kpu/kenlm/blob/master/lm/common/CMakeLists.txt This CMake command sets the source files for the KenLM library. It uses the CMAKE_CURRENT_SOURCE_DIR variable to prefix the file paths, ensuring correct paths when referenced by parent CMake files. The PARENT_SCOPE option makes the variable available in the parent directory. ```cmake set(KENLM_LM_COMMON_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/model_buffer.cc ${CMAKE_CURRENT_SOURCE_DIR}/print.cc ${CMAKE_CURRENT_SOURCE_DIR}/renumber.cc ${CMAKE_CURRENT_SOURCE_DIR}/size_option.cc PARENT_SCOPE) ``` -------------------------------- ### Finding Eigen3 Package Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Finds the Eigen3 library with a minimum version of 3.1.0. Eigen3 is a C++ template library for linear algebra. The CONFIG option specifies that CMake should use the package's configuration file to find it. ```cmake find_package(Eigen3 3.1.0 CONFIG) ``` -------------------------------- ### Adding Executables and Linking Libraries Source: https://github.com/kpu/kenlm/blob/master/lm/filter/CMakeLists.txt This snippet adds executables named filter and phrase_table_vocab, linking them with the kenlm_filter and kenlm libraries. The AddExes macro is assumed to be defined elsewhere in the CMake project. ```cmake AddExes(EXES filter phrase_table_vocab LIBRARIES kenlm_filter kenlm) ``` -------------------------------- ### Building Standalone Python Library Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Includes a CMake script to build a standalone C++ library for Python if the BUILD_PYTHON_STANDALONE option is enabled. This library can be dlopened by other Python packages. ```cmake if (BUILD_PYTHON_STANDALONE) # Build a shared lib artifact in the same way the cpython setup.py builds # and return. That lib can be dlopened by other Python packages that need # cpp artifacts and can't use the bits compiled by setuptools Extension include(${CMAKE_CURRENT_LIST_DIR}/python/BuildStandalone.cmake) return() endif() ``` -------------------------------- ### Setting KenLM Utility Stream Source Files Source: https://github.com/kpu/kenlm/blob/master/util/stream/CMakeLists.txt This snippet sets the source files for the KenLM utility stream library. The PARENT_SCOPE option makes the variable available to parent CMake files, and CMAKE_CURRENT_SOURCE_DIR ensures correct paths. ```cmake set(KENLM_UTIL_STREAM_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/chain.cc ${CMAKE_CURRENT_SOURCE_DIR}/count_records.cc ${CMAKE_CURRENT_SOURCE_DIR}/io.cc ${CMAKE_CURRENT_SOURCE_DIR}/line_input.cc ${CMAKE_CURRENT_SOURCE_DIR}/multi_progress.cc ${CMAKE_CURRENT_SOURCE_DIR}/rewindable_stream.cc PARENT_SCOPE) ``` -------------------------------- ### Setting Default Build Type Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Sets the default build type to 'Release' if no build type is specified. This ensures that the project is built with optimizations enabled by default. ```cmake if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() ``` -------------------------------- ### Conditional Interpolation Program Build Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Conditionally enables the building of an interpolation program based on whether Eigen3 is found and the platform is not Windows. This uses the CMakeDependentOption module to manage the dependency. ```cmake include(CMakeDependentOption) cmake_dependent_option(ENABLE_INTERPOLATE "Build interpolation program (depends on Eigen3)" ON "EIGEN3_FOUND AND NOT WIN32" OFF) ``` -------------------------------- ### Checking for clock_gettime Function Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code checks for the existence of the clock_gettime function in librt and libc on UNIX systems. If found, it defines the HAVE_CLOCKGETTIME macro, which can be used to conditionally compile code that uses this function. This allows the code to use high-resolution timers when available. ```cmake if(UNIX) include(CheckLibraryExists) check_library_exists(rt clock_gettime "clock_gettime from librt" HAVE_CLOCKGETTIME_RT) if (HAVE_CLOCKGETTIME_RT) set(RT rt) else() check_library_exists(c clock_gettime "clock_gettime from the libc" HAVE_CLOCKGETTIME) endif() if (HAVE_CLOCKGETTIME_RT OR HAVE_CLOCKGETTIME) add_definitions(-DHAVE_CLOCKGETTIME) endif() endif() ``` -------------------------------- ### Linking kenlm_python Module with Dependencies Source: https://github.com/kpu/kenlm/blob/master/python/CMakeLists.txt This snippet links the kenlm_python module with the kenlm library. On Windows, it also links with the Python libraries. On Apple, it sets a linker flag to allow undefined dynamic lookups. ```cmake target_link_libraries(kenlm_python PUBLIC kenlm) if(WIN32) target_link_libraries(kenlm_python PUBLIC ${PYTHON_LIBRARIES}) elif(APPLE) set_target_properties(kenlm_python PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") endif() ``` -------------------------------- ### Adding CMake Module Path Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Appends a directory containing CMake modules to the CMAKE_MODULE_PATH. This allows CMake to find custom modules in the specified directory. ```cmake # And our helper modules list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) ``` -------------------------------- ### Setting Compile Flags for read_compressed.cc Source: https://github.com/kpu/kenlm/blob/master/util/CMakeLists.txt This CMake code sets compile flags for the read_compressed.cc, read_compressed_test.cc and file_piece_test.cc source files based on whether ZLIB, BZip2, or LibLZMA were found. These flags are used to enable or disable compression-related features in the code. The PROPERTIES COMPILE_FLAGS option adds the specified flags to the compilation command for the specified source files. ```cmake if (NOT "${READ_COMPRESSED_FLAGS}" STREQUAL "") set_source_files_properties(read_compressed.cc PROPERTIES COMPILE_FLAGS ${READ_COMPRESSED_FLAGS}) set_source_files_properties(read_compressed_test.cc PROPERTIES COMPILE_FLAGS ${READ_COMPRESSED_FLAGS}) set_source_files_properties(file_piece_test.cc PROPERTIES COMPILE_FLAGS ${READ_COMPRESSED_FLAGS}) endif() ``` -------------------------------- ### OpenMP Dependency Check and Linking Source: https://github.com/kpu/kenlm/blob/master/lm/interpolate/CMakeLists.txt This snippet checks for the OpenMP library and links it to the `kenlm_interpolate` library if found. OpenMP is used for parallel processing. ```cmake find_package(OpenMP) if (OPENMP_CXX_FOUND) target_link_libraries(kenlm_interpolate PUBLIC OpenMP::OpenMP_CXX) endif() ``` -------------------------------- ### Setting Python Site-Packages Directory Source: https://github.com/kpu/kenlm/blob/master/python/CMakeLists.txt This snippet determines the correct Python site-packages directory based on the operating system and Python version. It sets the PYTHON_SITE_PACKAGES variable accordingly. ```cmake if (WIN32) set (PYTHON_SITE_PACKAGES Lib/site-packages) else () set (PYTHON_SITE_PACKAGES lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages) endif () ``` -------------------------------- ### Filtering ARPA Files with filter Source: https://github.com/kpu/kenlm/blob/master/README.md This command runs the filter tool to remove entries from an ARPA or count file that will never be queried. The filter criterion can be corpus-level vocabulary, sentence-level vocabulary, or sentence-level phrases. ```bash bin/filter ``` -------------------------------- ### Suppressing MSVC Warnings Source: https://github.com/kpu/kenlm/blob/master/CMakeLists.txt Suppresses specific warnings (4716) in MSVC compilers. This can be used to reduce noise in the build output by ignoring known or irrelevant warnings. ```cmake if(MSVC) set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /w34716") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w34716") endif() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.