{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "528" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "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", "\n", "len(test)\n", "# count the number of words in test string\n", "\n", "test2 = test.split()\n", "len(test2)" ] }, { "cell_type": "code", "execution_count": 73, "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" ] } ], "source": [ "from transformers import AutoModelForSequenceClassification\n", "from transformers import TFAutoModelForSequenceClassification\n", "from transformers import AutoTokenizer, AutoConfig\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", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2865\n" ] } ], "source": [ "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.\"]\n", "len(test3)\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\n", "\n", "cleaned = clean_chat_log(test3)\n", "print(len(cleaned))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "147" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(tokenizer(test3, padding=True, truncation=True, return_tensors=\"pt\")[0])" ] }, { "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": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0986122886681098" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log(3)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"[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:feeling good\\nJoy:That's great to hear. What have you been up to lately that has made you feel good?.\\n\\nPatient:I am slowly but surely getting my masters degree. \\nJoy:It sounds like you are making great progress towards achieving your goal. How does it feel to be closer to reaching that milestone?.\\n\\nPatient:feels good\\nJoy:I'm glad to hear that. What have you been doing to stay motivated and keep moving forward?.\\n\\nPatient:just doing alot of work and keep the focus\\nJoy:That's great. It takes a lot of dedication and hard work to stay on track with your goals. Are there any areas where you feel like you could use some extra help or support?.\\n\\nPatient:yea, im still struggling with some subjects. I wish there were better ressources\\nJoy:It can be difficult to find the resources you need. I'm glad that you are taking initiative and seeking help. Have you considered talking to your professor or classmates? They may have some suggestions on how to get the support you need..\\n\\nPatient:yea its hard to ask the prof. they seem \\nJoy:I understand. It can be intimidating to reach out for help, especially when it comes to academics. You may find it helpful to practice talking through what you want to say with a friend or family member first. Having someone else's perspective can be really helpful in finding the right words to use when approaching your professor..\\n\\nPatient:yea its hard to ask the prof. they seem offended if I ask them for details. Like how could you not know this. Classmates are helpful but only in a short run. There is no one to sit down and discuss about the details. it seems everyone is just out for himself more or less. or just get the necessary thing done\\nJoy:It sounds like you feel overwhelmed and discouraged by the lack of support available. It can be difficult to find someone who is willing to take time out of their day to help you understand something more thoroughly. Have you considered online resources such as tutorials or forums? You could also look into tutoring services that may provide more individualized guidance..\\n\\nPatient:yea online tutorials are usually nice. tutoring services are expensive, good ones are hard to find and the stuff I am working on is quite obscure. \\nJoy:I understand that it can be difficult to find the resources you need, especially if they are expensive or hard to come by. Have you talked to any friends or family members who may have been in a similar situation and know of some good resources? They may be able to point you in the right direction..\\n\\nPatient:yea family is quite supportive. But in the end I still have to do the hard work. There isnt much help they can offer in terms of understanding the subject\\nJoy:Yes, it sounds like you're feeling overwhelmed by the amount of effort and understanding that goes into mastering the material. Have you considered taking a break from studying to recharge? It may help to take a step back and come back to the material with a fresh perspective..\\n\\nPatient:you are right. but it seems i cant afford any long breaks. then i will just fall behind\\nJoy:I understand. It can be difficult to take a break when you are feeling so much pressure to succeed. Instead of taking an extended break, maybe you could try breaking up your study sessions into smaller chunks that are more manageable. That way, you can take regular short breaks throughout the day to help reset and refocus before getting back to studying..\\n\\nPatient:ok\\nJoy:Is there anything else I can help you with today?\"]\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, 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, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6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 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 }