split_portion: Break Text Into Ordered Word Chunks

Description Usage Arguments Value Examples

View source: R/split_portion.R

Description

Some visualizations and algorithms require text to be broken into chunks of ordered words. split_portion breaks text, optionally by grouping variables, into equal chunks. The chunk size can be specified by giving number of words to be in each chunk or the number of chunks.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
split_portion(
  text.var,
  grouping.var = NULL,
  n.words,
  n.chunks,
  as.string = TRUE,
  rm.unequal = FALSE,
  as.table = TRUE,
  ...
)

Arguments

text.var

The text variable

grouping.var

The grouping variables. Default NULL generates one word list for all text. Also takes a single grouping variable or a list of 1 or more grouping variables.

n.words

An integer specifying the number of words in each chunk (must specify n.chunks or n.words).

n.chunks

An integer specifying the number of chunks (must specify n.chunks or n.words).

as.string

logical. If TRUE the chunks are returned as a single string. If FALSE the chunks are returned as a vector of single words.

rm.unequal

logical. If TRUE final chunks that are unequal in length to the other chunks are removed.

as.table

logical. If TRUE the list output is coerced to data.table or tibble.

...

Ignored.

Value

Returns a list or data.table of text chunks.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
with(DATA, split_portion(state, n.chunks = 10))
with(DATA, split_portion(state, n.words = 10))
with(DATA, split_portion(state, n.chunks = 10, as.string=FALSE))
with(DATA, split_portion(state, n.chunks = 10, rm.unequal=TRUE))
with(DATA, split_portion(state, person, n.chunks = 10))
with(DATA, split_portion(state, list(sex, adult), n.words = 10))
with(DATA, split_portion(state, person, n.words = 10, rm.unequal=TRUE))

## Bigger data
with(hamlet, split_portion(dialogue, person, n.chunks = 10))
with(hamlet, split_portion(dialogue, list(act, scene, person), n.chunks = 10))
with(hamlet, split_portion(dialogue, person, n.words = 300))
with(hamlet, split_portion(dialogue, list(act, scene, person), n.words = 300))

Example output

    all index                     text.var
 1: all     1     Computer is fun. Not too
 2: all     2       fun. No it's not, it's
 3: all     3     dumb. What should we do?
 4: all     4       You liar, it stinks! I
 5: all     5    am telling the truth! How
 6: all     6     can we be certain? There
 7: all     7        is no way. I distrust
 8: all     8    you. What are you talking
 9: all     9     about? Shall we move on?
10: all    10 Good then. I'm hungry. Let's
11: all    11            eat. You already?
   all index                                              text.var
1: all     1       Computer is fun. Not too fun. No it's not, it's
2: all     2       dumb. What should we do? You liar, it stinks! I
3: all     3    am telling the truth! How can we be certain? There
4: all     4       is no way. I distrust you. What are you talking
5: all     5 about? Shall we move on? Good then. I'm hungry. Let's
6: all     6                                     eat. You already?
$all
$all$`1`
[1] "Computer" "is"       "fun."     "Not"      "too"     

$all$`2`
[1] "fun." "No"   "it's" "not," "it's"

$all$`3`
[1] "dumb."  "What"   "should" "we"     "do?"   

$all$`4`
[1] "You"     "liar,"   "it"      "stinks!" "I"      

$all$`5`
[1] "am"      "telling" "the"     "truth!"  "How"    

$all$`6`
[1] "can"      "we"       "be"       "certain?" "There"   

$all$`7`
[1] "is"       "no"       "way."     "I"        "distrust"

$all$`8`
[1] "you."    "What"    "are"     "you"     "talking"

$all$`9`
[1] "about?" "Shall"  "we"     "move"   "on?"   

$all$`10`
[1] "Good"    "then."   "I'm"     "hungry." "Let's"  

$all$`11`
[1] "eat."     "You"      "already?"


    all index                     text.var
 1: all     1     Computer is fun. Not too
 2: all     2       fun. No it's not, it's
 3: all     3     dumb. What should we do?
 4: all     4       You liar, it stinks! I
 5: all     5    am telling the truth! How
 6: all     6     can we be certain? There
 7: all     7        is no way. I distrust
 8: all     8    you. What are you talking
 9: all     9     about? Shall we move on?
10: all    10 Good then. I'm hungry. Let's
        person index                     text.var
 1:       greg     1                      No it's
 2:       greg     2                    not, it's
 3:       greg     3                      dumb. I
 4:       greg     4                   am telling
 5:       greg     5                   the truth!
 6:       greg     6                     There is
 7:       greg     7                      no way.
 8:       greg     8                  I'm hungry.
 9:       greg     9                   Let's eat.
10:       greg    10                 You already?
11: researcher    11 Shall we move on? Good then.
12:      sally     1                          How
13:      sally     2                          can
14:      sally     3                           we
15:      sally     4                           be
16:      sally     5                     certain?
17:      sally     6                         What
18:      sally     7                          are
19:      sally     8                          you
20:      sally     9                      talking
21:      sally    10                       about?
22:        sam     1                     Computer
23:        sam     2                           is
24:        sam     3                         fun.
25:        sam     4                          Not
26:        sam     5                          too
27:        sam     6                         fun.
28:        sam     7                          You
29:        sam     8                        liar,
30:        sam     9                           it
31:        sam    10                      stinks!
32:        sam    11              I distrust you.
33:    teacher    11           What should we do?
        person index                     text.var
   sex adult index                                           text.var
1:   f     0     1 How can we be certain? What are you talking about?
2:   f     1     1                       Shall we move on? Good then.
3:   m     0     1    Computer is fun. Not too fun. No it's not, it's
4:   m     0     2 dumb. You liar, it stinks! I am telling the truth!
5:   m     0     3 There is no way. I distrust you. I'm hungry. Let's
6:   m     0     4                                  eat. You already?
7:   m     1     1                                 What should we do?
   person index                                             text.var
1:   greg     1      No it's not, it's dumb. I am telling the truth!
2:   greg     2 There is no way. I'm hungry. Let's eat. You already?
3:    sam     1   Computer is fun. Not too fun. You liar, it stinks!
        person index
  1:       All     1
  2:       All     2
  3:       All     3
  4:       All     4
  5:       All     5
 ---                
357: Voltimand     7
358: Voltimand     8
359: Voltimand     9
360: Voltimand    10
361: Voltimand    11
                                                                                        text.var
  1:                                                                                         Our
  2:                                                                                        duty
  3:                                                                                          to
  4:                                                                                        your
  5:                                                                                     honour.
 ---                                                                                            
357:   give the assay of arms against your majesty. Whereon old Norway, overcome with joy, Gives
358:    him three thousand crowns in annual fee, And his commission to employ those soldiers, So
359: levied as before, against the Polack: With an entreaty, herein further shown, That it might
360:   please you to give quiet pass Through your dominions for this enterprise, On such regards
361:                                            of safety and allowance As therein are set down.
       act    scene         person index
   1: Act1  Scene I       Bernardo     1
   2: Act1  Scene I       Bernardo     2
   3: Act1  Scene I       Bernardo     3
   4: Act1  Scene I       Bernardo     4
   5: Act1  Scene I       Bernardo     5
  ---                                   
1127: Act5 Scene II Queen Gertrude     7
1128: Act5 Scene II Queen Gertrude     8
1129: Act5 Scene II Queen Gertrude     9
1130: Act5 Scene II Queen Gertrude    10
1131: Act5 Scene II Queen Gertrude    11
                                                                                                      text.var
   1:            Who's there? Long live the king! He. 'Tis now struck twelve; get thee to bed, Francisco. Have
   2:               you had quiet guard? Well, good night. If you do meet Horatio and Marcellus, The rivals of
   3: my watch, bid them make haste. Say, What, is Horatio there? Welcome, Horatio: welcome, good Marcellus. I
   4:                  have seen nothing. Sit down awhile; And let us once again assail your ears, That are so
   5:                fortified against our story What we have two nights seen. NA In the same figure, like the
  ---                                                                                                         
1127:                                                                                    Come, let me wipe thy
1128:                                                                                 face. No, no, the drink,
1129:                                                                          the drink,--O my dear Hamlet,--
1130:                                                                                  The drink, the drink! I
1131:                                                                                             am poison'd.
           person index
  1:          All     1
  2:     Bernardo     1
  3:      Captain     1
  4:    Cornelius     1
  5:        Danes     1
 ---                   
117:  Rosencrantz     2
118:  Rosencrantz     3
119: Second Clown     1
120:      Servant     1
121:    Voltimand     1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    text.var
  1:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   Our duty to your honour. Lights, lights, lights! NA Treason! treason!
  2:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Who's there? Long live the king! He. 'Tis now struck twelve; get thee to bed, Francisco. Have you had quiet guard? Well, good night. If you do meet Horatio and Marcellus, The rivals of my watch, bid them make haste. Say, What, is Horatio there? Welcome, Horatio: welcome, good Marcellus. I have seen nothing. Sit down awhile; And let us once again assail your ears, That are so fortified against our story What we have two nights seen. NA In the same figure, like the king that's dead. Looks it not like the king? mark it, Horatio. It would be spoke to. See, it stalks away! How now, Horatio! you tremble and look pale: Is not this something more than fantasy? What think you on't? I think it be no other but e'en so: Well may it sort that this portentous figure Comes armed through our watch; so like the king That was and is the question of these wars. 'Tis here! It was about to speak, when the cock crew. We do, my lord. Arm'd, my lord. My lord, from head to foot. Longer, longer.
  3:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             I will do't, my lord. They are of Norway, sir. Against some part of Poland. The nephews to old Norway, Fortinbras. Truly to speak, and with no addition, We go to gain a little patch of ground That hath in it no profit but the name. To pay five ducats, five, I would not farm it; Nor will it yield to Norway or the Pole A ranker rate, should it be sold in fee. Yes, it is already garrison'd. God be wi' you, sir.
  4:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NA
  5:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   No, let's come in. We will, we will. Let her come in.
 ---                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
117: second time come to them; for they say an old man is twice a child. Good my lord! He does confess he feels himself distracted; But from what cause he will by no means speak. Most like a gentleman. Niggard of question; but, of our demands, Most free in his reply. Madam, it so fell out, that certain players We o'er-raught on the way: of these we told him; And there did seem in him a kind of joy To hear of it: they are about the court, And, as I think, they have already order This night to play before him. We shall, my lord. NA Ay, my lord; they stay upon your patience. Then thus she says; your behavior hath struck her into amazement and admiration. She desires to speak with you in her closet, ere you go to bed. My lord, you once did love me. Good my lord, what is your cause of distemper? you do, surely, bar the door upon your own liberty, if you deny your griefs to your friend. How can that be, when you have the voice of the king himself for your succession in Denmark? The single and peculiar life is bound, With all the strength and armour of the mind, To keep itself from noyance; but much more That spirit upon whose weal depend and rest The lives of many. The cease of majesty Dies not alone; but, like a gulf, doth draw What's near it with it: it is a massy wheel, Fix'd on the summit of the highest mount, To whose huge spokes ten thousand lesser things Are mortised and adjoin'd; which, when it falls, Each small annexment, petty consequence, Attends the boisterous ruin. Never alone Did the king sigh, but with a general groan. NA NA What
118:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     have you done, my lord, with the dead body? Tell us where 'tis, that we may take it thence And bear it to the chapel. Believe what? Take you me for a sponge, my lord? I understand you not, my lord. My lord, you must tell us where the body is, and go with us to the king. Where the dead body is bestow'd, my lord, We cannot get from him. Without, my lord; guarded, to know your pleasure. Ho, Guildenstern! bring in my lord. Wilt please you go, my lord?
119:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           I tell thee she is: and therefore make her grave straight: the crowner hath sat on her, and finds it Christian burial. Why, 'tis found so. NA But is this law? Will you ha' the truth on't? If this had not been a gentlewoman, she should have been buried out o' Christian burial. Was he a gentleman? Why, he had none. Go to. The gallows-maker; for that frame outlives a thousand tenants. 'Who builds stronger than a mason, a shipwright, or a carpenter? Marry, now I can tell. Mass, I cannot tell.
120:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Sailors, sir: they say they have letters for you.
121:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   In that and all things will we show our duty. Most fair return of greetings and desires. Upon our first, he sent out to suppress His nephew's levies; which to him appear'd To be a preparation 'gainst the Polack; But, better look'd into, he truly found It was against your highness: whereat grieved, That so his sickness, age and impotence Was falsely borne in hand, sends out arrests On Fortinbras; which he, in brief, obeys; Receives rebuke from Norway, and in fine Makes vow before his uncle never more To give the assay of arms against your majesty. Whereon old Norway, overcome with joy, Gives him three thousand crowns in annual fee, And his commission to employ those soldiers, So levied as before, against the Polack: With an entreaty, herein further shown, That it might please you to give quiet pass Through your dominions for this enterprise, On such regards of safety and allowance As therein are set down.
      act    scene            person index
  1: Act1  Scene I          Bernardo     1
  2: Act1  Scene I         Francisco     1
  3: Act1  Scene I           Horatio     1
  4: Act1  Scene I           Horatio     2
  5: Act1  Scene I           Horatio     3
 ---                                      
171: Act5 Scene II              Lord     1
172: Act5 Scene II             Osric     1
173: Act5 Scene II             Osric     2
174: Act5 Scene II Prince Fortinbras     1
175: Act5 Scene II    Queen Gertrude     1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 text.var
  1:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         Who's there? Long live the king! He. 'Tis now struck twelve; get thee to bed, Francisco. Have you had quiet guard? Well, good night. If you do meet Horatio and Marcellus, The rivals of my watch, bid them make haste. Say, What, is Horatio there? Welcome, Horatio: welcome, good Marcellus. I have seen nothing. Sit down awhile; And let us once again assail your ears, That are so fortified against our story What we have two nights seen. NA In the same figure, like the king that's dead. Looks it not like the king? mark it, Horatio. It would be spoke to. See, it stalks away! How now, Horatio! you tremble and look pale: Is not this something more than fantasy? What think you on't? I think it be no other but e'en so: Well may it sort that this portentous figure Comes armed through our watch; so like the king That was and is the question of these wars. 'Tis here! It was about to speak, when the cock crew.
  2:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Nay, answer me: stand, and unfold yourself. Bernardo? You come most carefully upon your hour. For this relief much thanks: 'tis bitter cold, And I am sick at heart. Not a mouse stirring. I think I hear them. Stand, ho! Who's there? Give you good night. Bernardo has my place. Give you good night.
  3:                      Friends to this ground. A piece of him. Tush, tush, 'twill not appear. Well, sit we down, And let us hear Bernardo speak of this. Most like: it harrows me with fear and wonder. What art thou that usurp'st this time of night, Together with that fair and warlike form In which the majesty of buried Denmark Did sometimes march? by heaven I charge thee, speak! Stay! speak, speak! I charge thee, speak! Before my God, I might not this believe Without the sensible and true avouch Of mine own eyes. As thou art to thyself: Such was the very armour he had on When he the ambitious Norway combated; So frown'd he once, when, in an angry parle, He smote the sledded Polacks on the ice. 'Tis strange. In what particular thought to work I know not; But in the gross and scope of my opinion, This bodes some strange eruption to our state. That can I; At least, the whisper goes so. Our last king, Whose image even but now appear'd to us, Was, as you know, by Fortinbras of Norway, Thereto prick'd on by a most emulate pride, Dared to the combat; in which our valiant Hamlet-- For so this side of our known world esteem'd him-- Did slay this Fortinbras; who by a seal'd compact, Well ratified by law and heraldry, Did forfeit, with his life, all those his lands Which he stood seized of, to the conqueror: Against the which, a moiety competent Was gaged by our king; which had return'd To the inheritance of Fortinbras, Had he been vanquisher; as, by the same covenant, And carriage of the article design'd, His fell to Hamlet. Now, sir, young Fortinbras, Of unimproved mettle hot and full, Hath in the skirts of Norway here and there Shark'd
  4:                                                        up a list of lawless resolutes, For food and diet, to some enterprise That hath a stomach in't; which is no other-- As it doth well appear unto our state-- But to recover of us, by strong hand And terms compulsatory, those foresaid lands So by his father lost: and this, I take it, Is the main motive of our preparations, The source of this our watch and the chief head Of this post-haste and romage in the land. A mote it is to trouble the mind's eye. In the most high and palmy state of Rome, A little ere the mightiest Julius fell, The graves stood tenantless and the sheeted dead Did squeak and gibber in the Roman streets: As stars with trains of fire and dews of blood, Disasters in the sun; and the moist star Upon whose influence Neptune's empire stands Was sick almost to doomsday with eclipse: And even the like precurse of fierce events, As harbingers preceding still the fates And prologue to the omen coming on, Have heaven and earth together demonstrated Unto our climatures and countrymen. -- But soft, behold! lo, where it comes again! I'll cross it, though it blast me. Stay, illusion! If thou hast any sound, or use of voice, Speak to me: If there be any good thing to be done, That may to thee do ease and grace to me, Speak to me: If thou art privy to thy country's fate, Which, happily, foreknowing may avoid, O, speak! Or if thou hast uphoarded in thy life Extorted treasure in the womb of earth, For which, they say, you spirits oft walk in death, Speak of it: stay, and speak! Stop it, Marcellus. Do, if it will not stand. 'Tis here! And then it started like a guilty thing
  5:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Upon a fearful summons. I have heard, The cock, that is the trumpet to the morn, Doth with his lofty and shrill-sounding throat Awake the god of day; and, at his warning, Whether in sea or fire, in earth or air, The extravagant and erring spirit hies To his confine: and of the truth herein This present object made probation. So have I heard and do in part believe it. But, look, the morn, in russet mantle clad, Walks o'er the dew of yon high eastward hill: Break we our watch up; and by my advice, Let us impart what we have seen to-night Unto young Hamlet; for, upon my life, This spirit, dumb to us, will speak to him. Do you consent we shall acquaint him with it, As needful in our loves, fitting our duty?
 ---                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
171:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             My lord, his majesty commended him to you by young Osric, who brings back to him that you attend him in the hall: he sends to know if your pleasure hold to play with Laertes, or that you will take longer time. The king and queen and all are coming down. The queen desires you to use some gentle entertainment to Laertes before you fall to play.
172: Your lordship is right welcome back to Denmark. Sweet lord, if your lordship were at leisure, I should impart a thing to you from his majesty. I thank your lordship, it is very hot. It is indifferent cold, my lord, indeed. Exceedingly, my lord; it is very sultry,--as 'twere,--I cannot tell how. Nay, good my lord; for mine ease, in good faith. Sir, here is newly come to court Laertes; believe me, an absolute gentleman, full of most excellent differences, of very soft society and great showing: indeed, to speak feelingly of him, he is the card or calendar of gentry, for you shall find in him the continent of what part a gentleman would see. Your lordship speaks most infallibly of him. Sir? Of Laertes? NA NA I mean, sir, for his weapon; but in the imputation laid on him by them, in his meed he's unfellowed. Rapier and dagger. The king, sir, hath wagered with him six Barbary horses: against the which he has imponed, as I take it, six French rapiers and poniards, with their assigns, as girdle, hangers, and so: three of the carriages, in faith, are very dear to fancy, very responsive to the hilts, most delicate carriages, and of very liberal conceit. The carriages, sir, are the hangers. The king, sir, hath laid, that in a dozen passes between yourself and him, he shall not exceed you three hits: he hath laid on twelve for nine; and it would come to immediate trial, if your lordship would vouchsafe the answer. I mean, my lord, the opposition of your person in trial. Shall I re-deliver you e'en so? I commend my duty to your lordship. Ay, my good lord. A hit, a very palpable hit. Nothing, neither way. Look to the queen there, ho! How
173:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            is't, Laertes? Young Fortinbras, with conquest come from Poland, To the ambassadors of England gives This warlike volley.
174:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Where is this sight? This quarry cries on havoc. O proud death, What feast is toward in thine eternal cell, That thou so many princes at a shot So bloodily hast struck? Let us haste to hear it, And call the noblest to the audience. For me, with sorrow I embrace my fortune: I have some rights of memory in this kingdom, Which now to claim my vantage doth invite me. Let four captains Bear Hamlet, like a soldier, to the stage; For he was likely, had he been put on, To have proved most royally: and, for his passage, The soldiers' music and the rites of war Speak loudly for him. Take up the bodies: such a sight as this Becomes the field, but here shows much amiss. Go, bid the soldiers shoot.
175:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   He's fat, and scant of breath. Here, Hamlet, take my napkin, rub thy brows; The queen carouses to thy fortune, Hamlet. I will, my lord; I pray you, pardon me. Come, let me wipe thy face. No, no, the drink, the drink,--O my dear Hamlet,-- The drink, the drink! I am poison'd.

textshape documentation built on May 29, 2021, 1:07 a.m.