File size: 41,029 Bytes
8029d8e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"test = ''' Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
"\n",
"Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
"\n",
"If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
"\n",
"You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
"\n",
"Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
"\n",
"Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
"\n",
"Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
"\n",
"If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
"\n",
"You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
"\n",
"Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
"\n",
"Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
"\n",
"Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
"\n",
"If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
"\n",
"You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
"\n",
"Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
"\n",
"Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
"\n",
"Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
"\n",
"If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
"\n",
"You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
"\n",
"Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
"\n",
"\n",
"\n",
" '''\n",
"test3 = ['[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to.\\nJoy:I am Joy, your AI therapist. How are you feeling today?', \"\\n\\nPatient:im doing okay\\nJoy:That's good to hear. Is there anything in particular that you would like to talk about today?\", '\\n\\nPatient:no not really\\nJoy:No problem. Is there anything on your mind that you would like to share? I am here to listen and offer support.', \"\\n\\nPatient:maybe actually. I am afraid my spouse cheats on me\\nJoy:I'm sorry to hear that. What has been making you feel this way?\", '\\n\\nPatient:she is writing text messages constantly with people i dont know\\nJoy:That can certainly be concerning. Have you tried talking to your spouse about this?', '\\n\\nPatient:yea then she gets defensive and angry\\nJoy:I understand that this must be difficult for you. Are there any specific strategies that have helped when conversations with your spouse become heated?', \"\\n\\nPatient:i dont know\\nJoy:It's okay if you don't know right away. What do you feel comfortable doing in these situations? Are there any strategies that have helped in the past when things become tense between you and your spouse?\", '\\n\\nPatient:i just stopped talking to her or change the subject\\nJoy:It sounds like you are doing your best to find a way to manage this. Is there anything that has helped make the situation more comfortable for you?', '\\n\\nPatient:trying not thinking about it\\nJoy:It sounds like you are doing your best to handle this difficult situation. Do you think it would help to talk to a professional or find additional support?', \"\\n\\nPatient:no i havent\\nJoy:That's understandable. Is there someone who you feel comfortable talking to about this? How does having a conversation with them make you feel?\", \"\\n\\nPatient:i havent talked with anyone about this\\nJoy:That's understandable. Having someone to talk to can be beneficial in difficult times like this. Is there anyone who you trust that you feel comfortable speaking with about your concerns?\", '\\n\\nPatient:there is no one, except maybe my mom\\nJoy:Talking to your mom can be a great start. Is there anything in particular that you want to share with her about what is going on? How does the idea of having this conversation make you feel?', '\\n\\nPatient:yea like I said, about this topic that im not very comfortable with, do you still remember which?\\nJoy:Yes, you mentioned that your spouse may be cheating on you. How would you like to approach this conversation with your mom?', \"\\n\\nPatient:tell her about my suspicision i guess\\nJoy:It can be difficult to bring up a topic like this, but it's important that you have an open conversation with her. Would you like any advice on how to start the conversation?\", \"\\n\\nPatient:yes please\\nJoy:It can help to start by letting your mom know why you decided to talk to her about this. Sharing how you are feeling and asking for her advice might be a good way to open the conversation. I'm here if you need anything else before or after speaking with your mom.\"]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def clean_chat_log(chat_log):\n",
" chat_log = ' '.join(chat_log)\n",
" # find the first /n\n",
" first_newline = chat_log.find('\\n')\n",
" chat_log = chat_log[first_newline:]\n",
" # remove all \\n\n",
" chat_log = chat_log.replace('\\n', ' ')\n",
" return chat_log"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to. Joy:I am Joy, your AI therapist. How are you feeling today?',\n",
" \" Patient:im doing okay Joy:That's good to hear. Is there anything in particular that you would like to talk about today?\",\n",
" ' Patient:no not really Joy:No problem. Is there anything on your mind that you would like to share? I am here to listen and offer support.',\n",
" \" Patient:maybe actually. I am afraid my spouse cheats on me Joy:I'm sorry to hear that. What has been making you feel this way?\",\n",
" ' Patient:she is writing text messages constantly with people i dont know Joy:That can certainly be concerning. Have you tried talking to your spouse about this?',\n",
" ' Patient:yea then she gets defensive and angry Joy:I understand that this must be difficult for you. Are there any specific strategies that have helped when conversations with your spouse become heated?',\n",
" \" Patient:i dont know Joy:It's okay if you don't know right away. What do you feel comfortable doing in these situations? Are there any strategies that have helped in the past when things become tense between you and your spouse?\",\n",
" ' Patient:i just stopped talking to her or change the subject Joy:It sounds like you are doing your best to find a way to manage this. Is there anything that has helped make the situation more comfortable for you?',\n",
" ' Patient:trying not thinking about it Joy:It sounds like you are doing your best to handle this difficult situation. Do you think it would help to talk to a professional or find additional support?',\n",
" \" Patient:no i havent Joy:That's understandable. Is there someone who you feel comfortable talking to about this? How does having a conversation with them make you feel?\",\n",
" \" Patient:i havent talked with anyone about this Joy:That's understandable. Having someone to talk to can be beneficial in difficult times like this. Is there anyone who you trust that you feel comfortable speaking with about your concerns?\",\n",
" ' Patient:there is no one, except maybe my mom Joy:Talking to your mom can be a great start. Is there anything in particular that you want to share with her about what is going on? How does the idea of having this conversation make you feel?',\n",
" ' Patient:yea like I said, about this topic that im not very comfortable with, do you still remember which? Joy:Yes, you mentioned that your spouse may be cheating on you. How would you like to approach this conversation with your mom?',\n",
" \" Patient:tell her about my suspicision i guess Joy:It can be difficult to bring up a topic like this, but it's important that you have an open conversation with her. Would you like any advice on how to start the conversation?\",\n",
" \" Patient:yes please Joy:It can help to start by letting your mom know why you decided to talk to her about this. Sharing how you are feeling and asking for her advice might be a good way to open the conversation. I'm here if you need anything else before or after speaking with your mom.\"]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def remove_backslashN(chat_log):\n",
" chat_log = [i.replace('\\n', ' ') for i in chat_log]\n",
" return chat_log\n",
"\n",
"remove_backslashN(test3)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
]
},
{
"data": {
"text/plain": [
"682"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(tokenizer(test, padding=True, truncation=True, return_tensors=\"pt\")[0])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of the model checkpoint at cardiffnlp/twitter-roberta-base-sentiment-latest were not used when initializing RobertaForSequenceClassification: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
"- This IS expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
"- This IS NOT expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
"Some weights of the model checkpoint at cardiffnlp/twitter-roberta-base-sentiment-latest were not used when initializing RobertaForSequenceClassification: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
"- This IS expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
"- This IS NOT expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
]
}
],
"source": [
"from transformers import AutoModelForSequenceClassification\n",
"from transformers import TFAutoModelForSequenceClassification\n",
"from transformers import AutoTokenizer, AutoConfig\n",
"from transformers import pipeline\n",
"MODEL = \"cardiffnlp/twitter-roberta-base-sentiment-latest\"\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
"config = AutoConfig.from_pretrained(MODEL)\n",
"model = AutoModelForSequenceClassification.from_pretrained(MODEL)\n",
"\n",
"sentiment_task = pipeline(\"sentiment-analysis\", model='cardiffnlp/twitter-roberta-base-sentiment-latest', tokenizer='cardiffnlp/twitter-roberta-base-sentiment-latest')\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"tokenized = tokenizer(['this is great', 'this sucks'], return_tensors='pt', truncation=True, padding=True)\n",
"output = model(**tokenized)\n",
"scores = output[0][0].detach().numpy()\n",
"scores = np.exp(scores) / np.sum(np.exp(scores), axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SequenceClassifierOutput(loss=None, logits=tensor([[-2.1332, -0.7378, 2.8549],\n",
" [ 1.6230, -0.1552, -1.7155]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None)"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1) positive 0.9668\n",
"2) neutral 0.0266\n",
"3) negative 0.0066\n"
]
}
],
"source": [
"ranking = np.argsort(scores)\n",
"ranking = ranking[::-1]\n",
"for i in range(scores.shape[0]):\n",
" l = config.id2label[ranking[i]]\n",
" s = scores[ranking[i]]\n",
" print(f\"{i+1}) {l} {np.round(float(s), 4)}\")"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.6931471805599453\n",
"2.4849066497880004\n",
"3.091042453358316\n",
"3.4657359027997265\n",
"3.7376696182833684\n",
"3.9512437185814275\n",
"4.127134385045092\n",
"4.276666119016055\n",
"4.406719247264253\n",
"4.5217885770490405\n"
]
}
],
"source": [
"# import natural log function\n",
"from math import log\n",
"\n",
"for i in range(2, 100, 10):\n",
" print(log(i))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\" .J.o.y.:.I. .a.m. .J.o.y.,. .y.o.u.r. .A.I. .t.h.e.r.a.p.i.s.t... .H.o.w. .a.r.e. .y.o.u. .f.e.e.l.i.n.g. .t.o.d.a.y.?. . . .P.a.t.i.e.n.t.:.i.m. .d.o.i.n.g. .o.k.a.y. .J.o.y.:.T.h.a.t.'.s. .g.o.o.d. .t.o. .h.e.a.r... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .i.n. .p.a.r.t.i.c.u.l.a.r. .t.h.a.t. .y.o.u. .w.o.u.l.d. .l.i.k.e. .t.o. .t.a.l.k. .a.b.o.u.t. .t.o.d.a.y.?. . . .P.a.t.i.e.n.t.:.n.o. .n.o.t. .r.e.a.l.l.y. .J.o.y.:.N.o. .p.r.o.b.l.e.m... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .o.n. .y.o.u.r. .m.i.n.d. .t.h.a.t. .y.o.u. .w.o.u.l.d. .l.i.k.e. .t.o. .s.h.a.r.e.?. .I. .a.m. .h.e.r.e. .t.o. .l.i.s.t.e.n. .a.n.d. .o.f.f.e.r. .s.u.p.p.o.r.t... . . .P.a.t.i.e.n.t.:.m.a.y.b.e. .a.c.t.u.a.l.l.y... .I. .a.m. .a.f.r.a.i.d. .m.y. .s.p.o.u.s.e. .c.h.e.a.t.s. .o.n. .m.e. .J.o.y.:.I.'.m. .s.o.r.r.y. .t.o. .h.e.a.r. .t.h.a.t... .W.h.a.t. .h.a.s. .b.e.e.n. .m.a.k.i.n.g. .y.o.u. .f.e.e.l. .t.h.i.s. .w.a.y.?. . . .P.a.t.i.e.n.t.:.s.h.e. .i.s. .w.r.i.t.i.n.g. .t.e.x.t. .m.e.s.s.a.g.e.s. .c.o.n.s.t.a.n.t.l.y. .w.i.t.h. .p.e.o.p.l.e. .i. .d.o.n.t. .k.n.o.w. .J.o.y.:.T.h.a.t. .c.a.n. .c.e.r.t.a.i.n.l.y. .b.e. .c.o.n.c.e.r.n.i.n.g... .H.a.v.e. .y.o.u. .t.r.i.e.d. .t.a.l.k.i.n.g. .t.o. .y.o.u.r. .s.p.o.u.s.e. .a.b.o.u.t. .t.h.i.s.?. . . .P.a.t.i.e.n.t.:.y.e.a. .t.h.e.n. .s.h.e. .g.e.t.s. .d.e.f.e.n.s.i.v.e. .a.n.d. .a.n.g.r.y. .J.o.y.:.I. .u.n.d.e.r.s.t.a.n.d. .t.h.a.t. .t.h.i.s. .m.u.s.t. .b.e. .d.i.f.f.i.c.u.l.t. .f.o.r. .y.o.u... .A.r.e. .t.h.e.r.e. .a.n.y. .s.p.e.c.i.f.i.c. .s.t.r.a.t.e.g.i.e.s. .t.h.a.t. .h.a.v.e. .h.e.l.p.e.d. .w.h.e.n. .c.o.n.v.e.r.s.a.t.i.o.n.s. .w.i.t.h. .y.o.u.r. .s.p.o.u.s.e. .b.e.c.o.m.e. .h.e.a.t.e.d.?. . . .P.a.t.i.e.n.t.:.i. .d.o.n.t. .k.n.o.w. .J.o.y.:.I.t.'.s. .o.k.a.y. .i.f. .y.o.u. .d.o.n.'.t. .k.n.o.w. .r.i.g.h.t. .a.w.a.y... .W.h.a.t. .d.o. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .d.o.i.n.g. .i.n. .t.h.e.s.e. .s.i.t.u.a.t.i.o.n.s.?. .A.r.e. .t.h.e.r.e. .a.n.y. .s.t.r.a.t.e.g.i.e.s. .t.h.a.t. .h.a.v.e. .h.e.l.p.e.d. .i.n. .t.h.e. .p.a.s.t. .w.h.e.n. .t.h.i.n.g.s. .b.e.c.o.m.e. .t.e.n.s.e. .b.e.t.w.e.e.n. .y.o.u. .a.n.d. .y.o.u.r. .s.p.o.u.s.e.?. . . .P.a.t.i.e.n.t.:.i. .j.u.s.t. .s.t.o.p.p.e.d. .t.a.l.k.i.n.g. .t.o. .h.e.r. .o.r. .c.h.a.n.g.e. .t.h.e. .s.u.b.j.e.c.t. .J.o.y.:.I.t. .s.o.u.n.d.s. .l.i.k.e. .y.o.u. .a.r.e. .d.o.i.n.g. .y.o.u.r. .b.e.s.t. .t.o. .f.i.n.d. .a. .w.a.y. .t.o. .m.a.n.a.g.e. .t.h.i.s... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .t.h.a.t. .h.a.s. .h.e.l.p.e.d. .m.a.k.e. .t.h.e. .s.i.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n.y.o.n.e. .w.h.o. .y.o.u. .t.r.u.s.t. .t.h.a.t. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .s.p.e.a.k.i.n.g. .w.i.t.h. .a.b.o.u.t. .y.o.u.r. .c.o.n.c.e.r.n.s.?. . . .P.a.t.i.e.n.t.:.t.h.e.r.e. .i.s. .n.o. .o.n.e.,. .e.x.c.e.p.t. .m.a.y.b.e. .m.y. .m.o.m. .J.o.y.:.T.a.l.k.i.n.g. .t.o. .y.o.u.r. .m.o.m. .c.a.n. .b.e. .a. .g.r.e.a.t. .s.t.a.r.t... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .i.n. .p.a.r.t.i.c.u.l.a.r. .t.h.a.t. .y.o.u. .w.a.n.t. .t.o. .s.h.a.r.e. .w.i.t.h. .h.e.r. .a.b.o.u.t. .w.h.a.t. .i.s. .g.o.i.n.g. .o.n.?. .H.o.w. .d.o.e.s. .t.h.e. .i.d.e.a. .o.f. .h.a.v.i.n.g. .t.h.i.s. .c.o.n.v.e.r.s.a.t.i.o.n. .m.a.k.e. .y.o.u. .f.e.e.l.?. . . .P.a.t.i.e.n.t.:.y.e.a. .l.i.k.e. .I. .s.a.i.d.,. .a.b.o.u.t. .t.h.i.s. .t.o.p.i.c. .t.h.a.t. .i.m. .n.o.t. .v.e.r.y. .c.o.m.f.o.r.t.a.b.l.e. .w.i.t.h.,. .d.o. .y.o.u. .s.t.i.l.l. .r.e.m.e.m.b.e.r. .w.h.i.c.h.?. .J.o.y.:.Y.e.s.,. .y.o.u. .m.e.n.t.i.o.n.e.d. .t.h.a.t. .y.o.u.r. .s.p.o.u.s.e. .m.a.y. .b.e. .c.h.e.a.t.i.n.g. .o.n. .y.o.u... .H.o.w. .w.o.u.l.d. .y.o.u. .l.i.k.e. .t.o. .a.p.p.r.o.a.c.h. .t.h.i.s. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .y.o.u.r. .m.o.m.?. . . .P.a.t.i.e.n.t.:.t.e.l.l. .h.e.r. .a.b.o.u.t. .m.y. .s.u.s.p.i.c.i.s.i.o.n. .i. .g.u.e.s.s. .J.o.y.:.I.t. .c.a.n. .b.e. .d.i.f.f.i.c.u.l.t. .t.o. .b.r.i.n.g. .u.p. .a. .t.o.p.i.c. .l.i.k.e. .t.h.i.s.,. .b.u.t. .i.t.'.s. .i.m.p.o.r.t.a.n.t. .t.h.a.t. .y.o.u. .h.a.v.e. .a.n. .o.p.e.n. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .h.e.r... .W.o.u.l.d. .y.o.u. .l.i.k.e. .a.n.y. .a.d.v.i.c.e. .o.n. .h.o.w. .t.o. .s.t.a.r.t. .t.h.e. .c.o.n.v.e.r.s.a.t.i.o.n.?. . . .P.a.t.i.e.n.t.:.y.e.s. .p.l.e.a.s.e. .J.o.y.:.I.t. .c.a.n. .h.e.l.p. .t.o. .s.t.a.r.t. .b.y. .l.e.t.t.i.n.g. .y.o.u.r. .m.o.m. .k.n.o.w. .w.h.y. .y.o.u. .d.e.c.i.d.e.d. .t.o. .t.a.l.k. .t.o. .h.e.r. .a.b.o.u.t. .t.h.i.s... .S.h.a.r.i.n.g. .h.o.w. .y.o.u. .a.r.e. .f.e.e.l.i.n.g. .a.n.d. .a.s.k.i.n.g. .f.o.r. .h.e.r. .a.d.v.i.c.e. .m.i.g.h.t. .b.e. .a. .g.o.o.d. .w.a.y. .t.o. .o.p.e.n. .t.h.e. .c.o.n.v.e.r.s.a.t.i.o.n... .I.'.m. .h.e.r.e. .i.f. .y.o.u. .n.e.e.d. .a.n.y.t.h.i.n.g. .e.l.s.e. .b.e.f.o.r.e. .o.r. .a.f.t.e.r. .s.p.e.a.k.i.n.g. .w.i.t.h. .y.o.u.r. .m.o.m..\"]\n"
]
}
],
"source": [
"def split_input_string(input_string: list, tokenizer, max_length):\n",
" # Initialize the list to store the split strings\n",
" split_strings = []\n",
"\n",
" # Split the input string into sentences\n",
" sentences = input_string\n",
" #sentences = input_string.split(\".\")\n",
"\n",
" # Loop through each sentence\n",
" i = 0\n",
" while i < len(sentences):\n",
" # Initialize the current string with the first sentence\n",
" curr_string = sentences[i]\n",
"\n",
" # Keep adding sentences until the current string is longer than the maximum length\n",
" i += 1\n",
" while i < len(sentences) and len(tokenizer.encode(curr_string + \".\" + sentences[i], max_length=max_length)) <= max_length:\n",
" curr_string += \".\" + sentences[i]\n",
" i += 1\n",
"\n",
" # Add the current string to the list of split strings\n",
" split_strings.append(curr_string)\n",
"\n",
" # Return the list of split strings\n",
" return split_strings\n",
"\n",
"# Example usage\n",
"input_string = \"This is a sample input string. It can be quite long. This function splits the input string into parts, each of which is no longer than the maximum length defined by the tokenizer and each of which ends on a full stop.\"\n",
"max_length = 512\n",
"split_strings = split_input_string(test3_string, tokenizer, max_length)\n",
"print(split_strings)\n"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(split_strings)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"import re \n",
"\n",
"def split(input: str) -> list:\n",
" tokenized = tokenizer(test, return_tensors='pt')\n",
" parts = (len(tokenized[0]) // 500) + 1\n",
" words_each_part = len(input.split()) // parts\n",
" print(words_each_part)\n",
" sentences = re.split(r'(?<=[.!?]) +', input)\n",
" # count the number of words in each sentence\n",
" words = [len(i.split()) for i in sentences]\n",
" print(words)\n",
"\n",
" return parts, sentences"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'input_ids': [0, 7568, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252,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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
"tokens = tokenizer(test)\n",
"tokens"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ĠCancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'ĊĊ', 'ĊĊ', 'Ġ']\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[55], line 26\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[39m# break\u001b[39;00m\n\u001b[1;32m 17\u001b[0m \u001b[39m# end += 1\u001b[39;00m\n\u001b[1;32m 18\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[39m# start = end + 1\u001b[39;00m\n\u001b[1;32m 22\u001b[0m \u001b[39m# end = start\u001b[39;00m\n\u001b[1;32m 24\u001b[0m \u001b[39mreturn\u001b[39;00m tokenized_parts\n\u001b[0;32m---> 26\u001b[0m tokenize_long_sentence(test, \u001b[39m500\u001b[39;49m)\n",
"Cell \u001b[0;32mIn[55], line 13\u001b[0m, in \u001b[0;36mtokenize_long_sentence\u001b[0;34m(long_sentence, max_tokens)\u001b[0m\n\u001b[1;32m 10\u001b[0m end \u001b[39m=\u001b[39m \u001b[39m0\u001b[39m\n\u001b[1;32m 12\u001b[0m \u001b[39mwhile\u001b[39;00m end \u001b[39m<\u001b[39m \u001b[39mlen\u001b[39m(tokens):\n\u001b[0;32m---> 13\u001b[0m \u001b[39mwhile\u001b[39;00m end \u001b[39m<\u001b[39m \u001b[39mlen\u001b[39m(tokens) \u001b[39mand\u001b[39;00m end \u001b[39m-\u001b[39;49m start \u001b[39m<\u001b[39;49m\u001b[39m=\u001b[39;49m max_tokens:\n\u001b[1;32m 14\u001b[0m \u001b[39mif\u001b[39;00m tokens[end] \u001b[39min\u001b[39;00m [\u001b[39m\"\u001b[39m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m?\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m!\u001b[39m\u001b[39m\"\u001b[39m]:\n\u001b[1;32m 15\u001b[0m \u001b[39mprint\u001b[39m(tokens[end])\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"from transformers import AutoTokenizer\n",
"\n",
"def tokenize_long_sentence(long_sentence, max_tokens):\n",
" tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
" tokens = tokenizer.tokenize(long_sentence)\n",
" print(tokens)\n",
" tokenized_parts = []\n",
"\n",
" start = 0\n",
" end = 0\n",
"\n",
" while end < len(tokens):\n",
" while end < len(tokens) and end - start <= max_tokens:\n",
" if tokens[end] in [\".\", \"?\", \"!\"]:\n",
" print(tokens[end])\n",
" # break\n",
" # end += 1\n",
"\n",
" # part = tokens[start:end + 1]\n",
" # tokenized_parts.append(part)\n",
" # start = end + 1\n",
" # end = start\n",
"\n",
" return tokenized_parts\n",
"\n",
"tokenize_long_sentence(test, 500)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "chatbot",
"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.8.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "9798c78c52b861f9442ea63a21901b586ae2f2169fa92d94b4091cc5bab62e04"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|