{"id":6145,"library":"pyobjc-framework-naturallanguage","title":"PyObjC NaturalLanguage Framework","description":"PyObjC is a bridge between Python and Objective-C, enabling Python scripts to leverage Apple's Cocoa frameworks. This particular library, `pyobjc-framework-naturallanguage` (version 12.1), provides Python wrappers for the macOS NaturalLanguage framework, allowing applications to access native natural language processing capabilities on macOS. PyObjC releases generally align with macOS SDK updates.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macos","cocoa","natural-language-processing","nlp","pyobjc","apple"],"install":[{"cmd":"pip install pyobjc-framework-naturallanguage","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"The core bridge between Python and Objective-C, required for all PyObjC framework wrappers.","package":"pyobjc-core","optional":false},{"reason":"Requires Python 3.10 or newer.","package":"Python","optional":false}],"imports":[{"symbol":"NaturalLanguage","correct":"import NaturalLanguage"},{"symbol":"NLTokenizer","correct":"from NaturalLanguage import NLTokenizer"},{"symbol":"NLLanguageRecognizer","correct":"from NaturalLanguage import NLLanguageRecognizer"},{"symbol":"NLTokenUnit","correct":"from NaturalLanguage import NLTokenUnit"}],"quickstart":{"code":"import NaturalLanguage\nfrom Foundation import NSMakeRange\n\ntext_to_analyze = \"The quick brown fox jumps over the lazy dog. This is a second sentence.\"\n\n# --- Tokenization Example ---\n# Create an NLTokenizer instance for word units\ntokenizer = NaturalLanguage.NLTokenizer.alloc().initWithUnit_(NaturalLanguage.NLTokenUnitWord)\n\n# Set the string to be tokenized\ntokenizer.setString_(text_to_analyze)\n\ntokens = []\n# Enumerate tokens using a Python callable as the Objective-C block\ndef token_block_handler(token_range, flags):\n    start = token_range.location\n    length = token_range.length\n    token_text = text_to_analyze[start : start + length]\n    tokens.append(token_text)\n    return True  # Return True to continue enumeration\n\ntokenizer.enumerateTokensInRange_usingBlock_(\n    NSMakeRange(0, len(text_to_analyze)),\n    token_block_handler\n)\n\nprint(f\"Original text: '{text_to_analyze}'\")\nprint(f\"Tokens (words): {tokens}\")\n\n# --- Language Recognition Example ---\nlang_recognizer = NaturalLanguage.NLLanguageRecognizer.alloc().init()\nlang_recognizer.processString_(text_to_analyze)\ndominant_language = lang_recognizer.dominantLanguage()\nprint(f\"Dominant language: {dominant_language}\")\n","lang":"python","description":"This quickstart demonstrates basic text tokenization and language recognition using the NaturalLanguage framework. It first tokenizes a sentence into individual words and then identifies the dominant language of the text."},"warnings":[{"fix":"Upgrade Python to 3.10 or later before upgrading PyObjC to versions 11.0 or higher.","message":"PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 previously dropped support for Python 3.8. Users upgrading must ensure their environment uses Python 3.10 or newer.","severity":"breaking","affected_versions":">=11.0"},{"fix":"Ensure the application is run on a macOS environment. Consider OS-agnostic alternatives if cross-platform compatibility is required.","message":"This library wraps macOS-specific frameworks and will only function on macOS. It is not compatible with other operating systems.","severity":"gotcha","affected_versions":"all"},{"fix":"Review classes with custom `__new__` methods and their interaction with `__init__`. Adapt to PyObjC's specific rules or consider alternative initialization patterns.","message":"PyObjC 10.3 introduced breaking changes to how `__init__` is handled in Python classes with custom `__new__` implementations. While 10.3.1 partially reverted this to allow `__init__` when a *user* implements `__new__`, classes relying on PyObjC's provided `__new__` still cannot use `__init__`.","severity":"breaking","affected_versions":">=10.3, <11.0"},{"fix":"Test existing code thoroughly, especially where `alloc()` and `init_()` are used directly, to ensure correct reference counting behavior. Consult PyObjC's ARC documentation.","message":"PyObjC 11.1 updated its Automatic Reference Counting (ARC) model for Objective-C initializer methods (those prefixed with 'init'). These methods now correctly model the behavior of 'stealing a reference to self and returning a new reference', which may affect memory management and require review of manual `alloc()`/`init_()` patterns.","severity":"breaking","affected_versions":">=11.1"},{"fix":"Ensure each `NLTokenizer` instance is confined to a single thread or dispatch queue, or create separate instances for each concurrent task.","message":"Apple's `NLTokenizer` documentation specifies that instances should be used on one thread or dispatch queue at a time. Failing to adhere to this can lead to unpredictable behavior or crashes in multi-threaded Python applications using this wrapper.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}