Function Repository Resource:

TextTake

Source Notebook

Take a section of text specified by a number and textual unit

Contributed by: Katja Della Libera

ResourceFunction["TextTake"][text,unit]

takes the first element of type unit in text.

ResourceFunction["TextTake"][text,n,unit]

takes the first n elements of type unit in text.

ResourceFunction["TextTake"][text,-n,unit]

takes the last n elements of type unit in text.

Details and Options

ResourceFunction["TextTake"] functions like StringTake but instead of characters, the function takes the following values for unit:
"Paragraph"take paragraphs of text
"Sentence"take sentences of text
"Word"take words of text
"Character"take characters of text
ResourceFunction["TextTake"] maintains the punctuation of the text.

Examples

Basic Examples (2) 

Take the first 10 words of the Gettysburg Address:

In[1]:=
ResourceFunction["TextTake"][
 ResourceData["Gettysburg Address"], 10, "Word"]
Out[1]=

Take the last 10 words of the Gettysburg Address:

In[2]:=
ResourceFunction["TextTake"][
 ResourceData["Gettysburg Address"], -10, "Word"]
Out[2]=

Scope (2) 

Take two sentences from a sample text:

In[3]:=
ResourceFunction[
 "TextTake"]["This is a short sentence. This is a slightly longer sentence. This function could be very useful." , 2, "Sentence"]
Out[3]=

Get two paragraphs of the resource data object Lorem Ipsum:

In[4]:=
ResourceFunction["TextTake"][
 ResourceData["Lorem Ipsum"], 2, "Paragraph"]
Out[4]=

Properties and Relations (2) 

TextTake with unit set to "Character" is equivalent to StringTake and is merely included for completeness:

In[5]:=
StringTake[ResourceData["Lorem Ipsum"], 20]
Out[5]=
In[6]:=
ResourceFunction["TextTake"][
 ResourceData["Lorem Ipsum"], 20, "Character"]
Out[6]=

TextTake matches the same elements as TextCases, but returns a single String of text which also includes text found between the cases such as whitespace and punctuation:

In[7]:=
myText = "This is a short sentence. This is a slightly longer sentence. This function could be very useful.";
In[8]:=
ResourceFunction["TextTake"][myText, 10, "Word"]
Out[8]=
In[9]:=
TextCases[myText, "Word", 10]
Out[9]=

Version History

  • 1.0.0 – 17 April 2020

Related Resources

License Information