community: Add PHP language parser to document_loaders (#19850)

**Description:**
Added a PHP language parser to document_loaders
**Issue:** N/A
**Dependencies:** N/A
**Twitter handle:** N/A

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
david02871
2024-04-08 16:30:28 +01:00
committed by GitHub
parent 2f03bc397e
commit e1a24d09c5
4 changed files with 164 additions and 1 deletions

View File

@@ -612,6 +612,63 @@
"haskell_docs = haskell_splitter.create_documents([HASKELL_CODE])\n",
"haskell_docs"
]
},
{
"cell_type": "markdown",
"id": "4a11f7cd-cd85-430c-b307-5b5b5f07f8db",
"metadata": {},
"source": [
"## PHP\n",
"Here's an example using the PHP text splitter:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "90c66e7e-87a5-4a81-bece-7949aabf2369",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Document(page_content='<?php\\nnamespace foo;'),\n",
" Document(page_content='class Hello {'),\n",
" Document(page_content='public function __construct() { }\\n}'),\n",
" Document(page_content='function hello() {\\n echo \"Hello World!\";\\n}'),\n",
" Document(page_content='interface Human {\\n public function breath();\\n}'),\n",
" Document(page_content='trait Foo { }\\nenum Color\\n{\\n case Red;'),\n",
" Document(page_content='case Blue;\\n}')]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PHP_CODE = \"\"\"<?php\n",
"namespace foo;\n",
"class Hello {\n",
" public function __construct() { }\n",
"}\n",
"function hello() {\n",
" echo \"Hello World!\";\n",
"}\n",
"interface Human {\n",
" public function breath();\n",
"}\n",
"trait Foo { }\n",
"enum Color\n",
"{\n",
" case Red;\n",
" case Blue;\n",
"}\"\"\"\n",
"php_splitter = RecursiveCharacterTextSplitter.from_language(\n",
" language=Language.PHP, chunk_size=50, chunk_overlap=0\n",
")\n",
"haskell_docs = php_splitter.create_documents([PHP_CODE])\n",
"haskell_docs"
]
}
],
"metadata": {
@@ -630,7 +687,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.4"
}
},
"nbformat": 4,