简体   繁体   中英

how to make these words into sentence

I lemmatised several sentences, and it turns out the results like this,this is for the first two sentences.

['She', 'be', 'start', 'on', 'Levofloxacin', 'but', 'the', 'patient', 'become', 'hypotensive', 'at', 'that', 'point', 'with', 'blood', 'pressure', 'of', '70/45', 'and', 'receive', 'a', 'normal', 'saline', 'bolus', 'to', 'boost', 'her', 'blood', 'pressure', 'to', '99/60', ';', 'however', 'the', 'patient', 'be', 'admit', 'to', 'the', 'Medical', 'Intensive', 'Care', 'Unit', 'for', 'overnight', 'observation', 'because', 'of', 'her', 'somnolence', 'and', 'hypotension', '.', '11', '.', 'History', 'of', 'hemoptysis', ',', 'on', 'Coumadin', '.', 'There', 'be', 'ST', 'scoop', 'in', 'the', 'lateral', 'lead', 'consistent', 'with', 'Dig', 'vs.', 'a', 'question', 'of', 'chronic', 'ischemia', 'change', '.']

which all the words are generated together like a list. but i need them to be like sentence by sentence, the output format would be better like this:

['She be start on Levofloxacin but the patient become hypotensive at that point with blood pressure of 70/45 and receive a normal saline bolus to boost her blood pressure to 99/60 ; however the patient be admit to the Medical Intensive Care Unit for overnight observation because of her somnolence and hypotension .','11 . History of hemoptysis , on Coumadin .','There be ST scoop in the lateral lead consistent with Dig vs. a question of chronic ischemia change .'] 

can anyone help me please? thanks a lot

Try this code:

final = []
sentence = []
for word in words:
    if word in ['.']: # and whatever other punctuation marks you want to use.
        sentence.append(word)
        final.append(' '.join(sentence))
        sentence = []
    else:
        sentence.append(word)

 print (final)

Hope this helps! :)

A good starting point might be str.join() :

>>> wordsList = ['She', 'be', 'start', 'on', 'Levofloxacin']
>>> ' '.join(wordsList)
'She be start on Levofloxacin'

You can try string concatenation by looping through the list

list1 = ['She', 'be', 'start', 'on', 'Levofloxacin', 'but', 'the', 
'patient', 'become', 'hypotensive', 'at', 'that', 'point', 'with', 'blood', 
'pressure', 'of', '70/45', 'and', 'receive', 'a', 'normal', 'saline', 
'bolus', 'to', 'boost', 'her', 'blood', 'pressure', 'to', '99/60', ';', 
'however', 'the', 'patient', 'be', 'admit', 'to', 'the', 'Medical', 
'Intensive', 'Care', 'Unit', 'for', 'overnight', 'observation', 'because', 
'of', 'her', 'somnolence', 'and', 'hypotension', '.', '11', '.', 'History', 
'of', 'hemoptysis', ',', 'on', 'Coumadin', '.', 'There', 'be', 'ST', 
'scoop', 'in', 'the', 'lateral', 'lead', 'consistent', 'with', 'Dig', 'vs.', 
'a', 'question', 'of', 'chronic', 'ischemia', 'change', '.']
list2 = []
string = ""
for element in list1:
  if(string == "" or element == "."):
    string = string + element
  else:
    string = string + " " + element
list2.append(string)
print(list2)
words=['She', 'be', 'start', 'on', 'Levofloxacin', 'but', 'the', 'patient', 'become', 'hypotensive', 'at', 'that', 'point', 'with', 'blood', 'pressure', 'of', '70/45', 'and', 'receive', 'a', 'normal', 'saline', 'bolus', 'to', 'boost', 'her', 'blood', 'pressure', 'to', '99/60', ';', 'however', 'the', 'patient', 'be', 'admit', 'to', 'the', 'Medical', 'Intensive', 'Care', 'Unit', 'for', 'overnight', 'observation', 'because', 'of', 'her', 'somnolence', 'and', 'hypotension', '.', '11', '.', 'History', 'of', 'hemoptysis', ',', 'on', 'Coumadin', '.', 'There', 'be', 'ST', 'scoop', 'in', 'the', 'lateral', 'lead', 'consistent', 'with', 'Dig', 'vs.', 'a', 'question', 'of', 'chronic', 'ischemia', 'change', '.']

def Wordify(words,sen_lim):

   Array=[]
   word=""
   sen_len=0

   for w in words:

       word+=w+" "
       if(w.isalnum()):

           sen_len+=1

       if(w=="." and sen_len>sen_lim):

           Array.append(word)
           word=""
           sen_len=0

    return(Array)

print(Wordify(words,5))

Basically you append the characters to a new string and separate out sentence if there is a period , but also ensure that the current sentence has a minimum number of words. This ensures sentences that like "11." are avoided.

sen_lim

is a parameter you could tune according to your convenience.

You could try this:

# list of words.
words = ['This', 'is', 'a', 'sentence', '.']

def sentence_from_list(words):
    sentence = ""
    # iterate the list and append to the string.
    for word in words:
        sentence += word + " "
    result = [sentence]
    # print the result. 
    print result

sentence_from_list(words)

You may need to delete the last space, just before the '.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM