mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-11-04 10:10:09 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			117 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
{
 | 
						||
 "cells": [
 | 
						||
  {
 | 
						||
   "cell_type": "markdown",
 | 
						||
   "id": "ea2973ac",
 | 
						||
   "metadata": {},
 | 
						||
   "source": [
 | 
						||
    "# NLTK Text Splitter\n",
 | 
						||
    "Rather than just splitting on \"\\n\\n\", we can use NLTK to split based on tokenizers.\n",
 | 
						||
    "\n",
 | 
						||
    "1. How the text is split: by NLTK\n",
 | 
						||
    "2. How the chunk size is measured: by length function passed in (defaults to number of characters)"
 | 
						||
   ]
 | 
						||
  },
 | 
						||
  {
 | 
						||
   "cell_type": "code",
 | 
						||
   "execution_count": 1,
 | 
						||
   "id": "aed17ddf",
 | 
						||
   "metadata": {},
 | 
						||
   "outputs": [],
 | 
						||
   "source": [
 | 
						||
    "# This is a long document we can split up.\n",
 | 
						||
    "with open('../../../state_of_the_union.txt') as f:\n",
 | 
						||
    "    state_of_the_union = f.read()"
 | 
						||
   ]
 | 
						||
  },
 | 
						||
  {
 | 
						||
   "cell_type": "code",
 | 
						||
   "execution_count": 2,
 | 
						||
   "id": "20fa9c23",
 | 
						||
   "metadata": {},
 | 
						||
   "outputs": [],
 | 
						||
   "source": [
 | 
						||
    "from langchain.text_splitter import NLTKTextSplitter\n",
 | 
						||
    "text_splitter = NLTKTextSplitter(chunk_size=1000)"
 | 
						||
   ]
 | 
						||
  },
 | 
						||
  {
 | 
						||
   "cell_type": "code",
 | 
						||
   "execution_count": 3,
 | 
						||
   "id": "5ea10835",
 | 
						||
   "metadata": {},
 | 
						||
   "outputs": [
 | 
						||
    {
 | 
						||
     "name": "stdout",
 | 
						||
     "output_type": "stream",
 | 
						||
     "text": [
 | 
						||
      "Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\n",
 | 
						||
      "\n",
 | 
						||
      "Members of Congress and the Cabinet.\n",
 | 
						||
      "\n",
 | 
						||
      "Justices of the Supreme Court.\n",
 | 
						||
      "\n",
 | 
						||
      "My fellow Americans.\n",
 | 
						||
      "\n",
 | 
						||
      "Last year COVID-19 kept us apart.\n",
 | 
						||
      "\n",
 | 
						||
      "This year we are finally together again.\n",
 | 
						||
      "\n",
 | 
						||
      "Tonight, we meet as Democrats Republicans and Independents.\n",
 | 
						||
      "\n",
 | 
						||
      "But most importantly as Americans.\n",
 | 
						||
      "\n",
 | 
						||
      "With a duty to one another to the American people to the Constitution.\n",
 | 
						||
      "\n",
 | 
						||
      "And with an unwavering resolve that freedom will always triumph over tyranny.\n",
 | 
						||
      "\n",
 | 
						||
      "Six days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways.\n",
 | 
						||
      "\n",
 | 
						||
      "But he badly miscalculated.\n",
 | 
						||
      "\n",
 | 
						||
      "He thought he could roll into Ukraine and the world would roll over.\n",
 | 
						||
      "\n",
 | 
						||
      "Instead he met a wall of strength he never imagined.\n",
 | 
						||
      "\n",
 | 
						||
      "He met the Ukrainian people.\n",
 | 
						||
      "\n",
 | 
						||
      "From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\n",
 | 
						||
      "\n",
 | 
						||
      "Groups of citizens blocking tanks with their bodies.\n"
 | 
						||
     ]
 | 
						||
    }
 | 
						||
   ],
 | 
						||
   "source": [
 | 
						||
    "texts = text_splitter.split_text(state_of_the_union)\n",
 | 
						||
    "print(texts[0])"
 | 
						||
   ]
 | 
						||
  }
 | 
						||
 ],
 | 
						||
 "metadata": {
 | 
						||
  "kernelspec": {
 | 
						||
   "display_name": "Python 3 (ipykernel)",
 | 
						||
   "language": "python",
 | 
						||
   "name": "python3"
 | 
						||
  },
 | 
						||
  "language_info": {
 | 
						||
   "codemirror_mode": {
 | 
						||
    "name": "ipython",
 | 
						||
    "version": 3
 | 
						||
   },
 | 
						||
   "file_extension": ".py",
 | 
						||
   "mimetype": "text/x-python",
 | 
						||
   "name": "python",
 | 
						||
   "nbconvert_exporter": "python",
 | 
						||
   "pygments_lexer": "ipython3",
 | 
						||
   "version": "3.9.1"
 | 
						||
  },
 | 
						||
  "vscode": {
 | 
						||
   "interpreter": {
 | 
						||
    "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
 | 
						||
   }
 | 
						||
  }
 | 
						||
 },
 | 
						||
 "nbformat": 4,
 | 
						||
 "nbformat_minor": 5
 | 
						||
}
 |