简体   繁体   English

如何把这些词变成句子

[英]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() : 一个很好的起点可能是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." 这样可以确保像“ 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 '.' 您可能需要删除“。”之前的最后一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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