簡體   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