繁体   English   中英

如何把这些词变成句子

[英]how to make these words into sentence

我对几个句子进行了词素化,结果是这样的,这是前两个句子的结果。

['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', '.']

所有单词都像列表一样生成。 但我需要它们像逐句,输出格式会像这样更好:

['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 .'] 

有人可以帮我吗? 非常感谢

试试这个代码:

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)

希望这可以帮助! :)

一个很好的起点可能是str.join()

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

您可以通过遍历列表来尝试字符串连接

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))

基本上,您将字符附加到新字符串上,如果有句点,则将句子分隔开,但还要确保当前句子的单词数最少。 这样可以确保像“ 11”这样的句子。 避免。

sen_lim

是您可以根据自己的方便进行调整的参数。

您可以尝试以下方法:

# 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)

您可能需要删除“。”之前的最后一个空格。

暂无
暂无

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

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