RAKE split sentences function on a Python dictionary

时间:2018-07-24 10:23:32

标签: python regex csv nlp nltk

How would I be able to apply this function to just the values within a python dictionary:

def split_sentences(text):
"""
Utility function to return a list of sentences.
@param text The text that must be split in to sentences.
"""
sentence_delimiters = re.compile(u'[\\[\\]\n.!?,;:\t\\-\\"\\(\\)\\\'\u2019\u2013]')

sentences = (sentence_delimiters.split(text))
return sentences

The code I have used to create the dictionary from a CSV file input:

with open('second_table.csv', mode='r') as infile:
    #Read in the csv file
    reader = csv.reader(infile)
    #Skip the headers
    next(reader, None)
    #Iterates through each row to get the key value pairs
    mydict = {rows[0]:rows[1] for rows in reader}

The python dictionary looks like so:

{'INC000007581947': '$BREM - CATIAV5 - Catia does not start',
 'INC000007581991': '$SPAI - REACT - react',
 'INC000007582037': 'access request',
 'INC000007582095': '$HAMB - DVOBROWSER - ACCESS RIGHTS',
 'INC000007582136': 'SIGLUM issue by opening a REACT request'}

2 个答案:

答案 0 :(得分:1)

mydict.values()为您提供字典中的所有值。然后,您可以遍历它们并使用您的函数。

for value in mydict.values():
    split_sentences(value)

答案 1 :(得分:1)

有不同的解决方案,具体取决于您是要创建新词典还是只是更新现有词典。

要更新字典值:

mydict.update({k : split_sentences(v) for k, v in mydict.items()})

要创建新词典:

new_dict = {k : split_sentences(v) for k, v in mydict.items()}