mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 01:45:27 +03:00
11 KiB
11 KiB
In [1]:
from langchain.text_splitter import CharacterTextSplitter, NLTKTextSplitter, SpacyTextSplitter
# This is a long document we can split up.
with open('../state_of_the_union.txt') as f:
state_of_the_union = f.read()In [2]:
text_splitter = CharacterTextSplitter(
separator = "\n\n",
chunk_size = 1000,
chunk_overlap = 200,
length_function = len,
)In [3]:
texts = text_splitter.split_text(state_of_the_union)
texts[0]Out [3]:
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix 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. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \n\nGroups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland. '
In [4]:
from transformers import GPT2TokenizerFast
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
In [5]:
text_splitter = CharacterTextSplitter.from_huggingface_tokenizer(tokenizer, chunk_size=100, chunk_overlap=0)
texts = text_splitter.split_text(state_of_the_union)In [6]:
print(texts[0])Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. Last year COVID-19 kept us apart. This year we are finally together again. Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. With a duty to one another to the American people to the Constitution. And with an unwavering resolve that freedom will always triumph over tyranny.
In [7]:
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=100, chunk_overlap=0)
texts = text_splitter.split_text(state_of_the_union)In [8]:
print(texts[0])Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. Last year COVID-19 kept us apart. This year we are finally together again. Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. With a duty to one another to the American people to the Constitution. And with an unwavering resolve that freedom will always triumph over tyranny.
In [14]:
text_splitter = NLTKTextSplitter(chunk_size=1000)In [16]:
texts = text_splitter.split_text(state_of_the_union)
texts[0]Out [16]:
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\n\nMembers of Congress and the Cabinet.\n\nJustices of the Supreme Court.\n\nMy fellow Americans.\n\nLast year COVID-19 kept us apart.\n\nThis year we are finally together again.\n\nTonight, we meet as Democrats Republicans and Independents.\n\nBut most importantly as Americans.\n\nWith a duty to one another to the American people to the Constitution.\n\nAnd with an unwavering resolve that freedom will always triumph over tyranny.\n\nSix 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\nBut he badly miscalculated.\n\nHe thought he could roll into Ukraine and the world would roll over.\n\nInstead he met a wall of strength he never imagined.\n\nHe met the Ukrainian people.\n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\n\nGroups of citizens blocking tanks with their bodies.\n\nEveryone from students to retirees teachers turned soldiers defending their homeland.'
In [17]:
text_splitter = SpacyTextSplitter(chunk_size=1000)In [19]:
texts = text_splitter.split_text(state_of_the_union)
texts[0]Out [19]:
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman.\n\nMembers of Congress and the Cabinet.\n\nJustices of the Supreme Court.\n\nMy fellow Americans. \n\n\n\nLast year COVID-19 kept us apart.\n\nThis year we are finally together again.\n\n\n\n\n\nTonight, we meet as Democrats Republicans and Independents.\n\nBut most importantly as Americans.\n\n\n\n\n\nWith a duty to one another to the American people to the Constitution. \n\n\n\nAnd with an unwavering resolve that freedom will always triumph over tyranny.\n\n\n\n\n\nSix 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\nBut he badly miscalculated.\n\n\n\n\n\nHe thought he could roll into Ukraine and the world would roll over.\n\nInstead he met a wall of strength he never imagined.\n\n\n\n\n\nHe met the Ukrainian people.\n\n\n\n\n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\n\n\n\n\n\nGroups of citizens blocking tanks with their bodies.\n\nEveryone from students to retirees teachers turned soldiers defending their homeland.'
In [ ]: