如何把这些词变成句子

时间:2018-12-22 11:05:27

标签: python nlp

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

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

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

5 个答案:

答案 0 :(得分:1)

尝试以下代码:

import pika
import SimilarURLs


data = ''

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()


def rabit_mq_start(Parametter):
    channel.queue_declare(queue='req')
    a = (take(datas=Parametter.decode()))
    channel.basic_publish(exchange='',
                          routing_key='req',
                          body=str(a))
    print(" [x] Sent {}".format(a))
    return a


channel.start_consuming()


def take(datas):
    returns = SimilarURLs.start(data=datas)
    return returns

希望这会有所帮助! :)

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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)

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

您可以尝试以下方法:

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

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