我刚接触Python,只是碰巧我需要从一些科学论文中提取一些信息。

如果给出类似以下内容的纯文本:


介绍
一些长篇著作
方法
一些长篇著作
结果
一些长篇著作


我怎样才能像下面这样在字典中放入论文?

paper_1 = {
           'Introduction': some long writings,
           'Methodology': some long writings,
           'Results': some long writings
          }


非常感谢:-)



尝试之后,我运行了一些代码,但无法正常运行:

text = 'introduction This is the FIRST part.' \
       'Methodologies This is the SECOND part.' \
       'results This is the THIRD part.'

import re
from re import finditer

d={}
first =[]
second =[]
title_list=[]
all =[]

for match in finditer("Methodology|results|methodologies|introduction|", text, re.IGNORECASE):
    if match.group() is not '':
        title = match.group()
        location = match.span()
        first.append(location[0])
        second.append(location[1])
        title_list.append(title)

all.append(first)
all.append(second)

a=[]
for i in range(2):
    j = i+1
    section = text[all[1][i]:all[0][j]]
    a.append(section)

for i in zip(title_list, a):
    d[i[0]] = i[1]
print (d)


这将产生以下结果:

{
'introduction': ' This is the FIRST part.',
'Methodologies': ' This is the SECOND part.'
}


然而,

i)它不能提取最后一位,这是结果部分。

ii)。在循环中,我给range()函数输入2,因为我知道只有3个部分(简介,方法和结果),但是在某些论文中,人们会添加更多的部分,我如何自动将正确的值分配给范围()?例如,某些论文可能包含以下部分:


介绍
一些长篇著作
关于某事的一般背景
一些长篇著作
某种章节标题
一些长篇著作
方法
一些长篇著作
结果
一些长篇著作


iii)。有没有一种更有效的方法可以在每个循环中构建字典?因此,我不需要使用第二个循环。



30-03-2018更新:

代码更新如下:

def section_detection(text):
    title_list=[]
    all =[[],[]]
    dic={}
    count = 0
    pattern = '\d\. [A-Z][a-z]*'

    for match in finditer(pattern, text, re.IGNORECASE):
        if match.group() is not '':
            all[0].append(match.span()[0])
            all[1].append(match.span()[1])
            title_list.append(match.group())
            count += 1

    for i in range(count):
        j = i+1
        try:
            dic[title_list[i]]=text[all[1][i]:all[0][j]]
        except IndexError:
            dic[title_list[i]]=text[all[1][i]:]

    return dic


如果执行如下:

import re
from re import finditer
text = '1. introduction This is the FIRST part.' \
       '2. Methodologies This is the SECOND part.' \
       '3. results This is the THIRD part.'\
       '4. somesection This SOME section'

dic = section_detection(text)
print(dic)


给出:

{'1. introduction': ' This is the FIRST part.', '2. Methodologies': ' This is the SECOND part.', '3. results': ' This is the THIRD part.', '4. somesection': ' This SOME section'}


非常感谢大家! :-)

最佳答案

真的很喜欢@Franz Forstmayr编写的正则表达式。只想指出一些打破它的方法。

text = '''
introduction This is the FIRST part.
introductionMethodologies This is the SECOND part.
results This is the THIRD part.
'''

import re
#### Regex based on https://stackoverflow.com/a/49546458/8083313
kw = ['methodology', 'results', 'methodologies', 'introduction']
pat = re.compile(r'(%s)' % '|'.join(kw), re.IGNORECASE)

sp = [x for x  in re.split(pat, text) if x]
print sp
dic = {k:v for k,v in zip(sp[0::2],sp[1::2])}

print(dic)


# {'\n': 'introduction',
#  'Methodologies': ' This is the SECOND part.\n',
#  ' This is the FIRST part.\n': 'introduction',
#  'results': ' This is the THIRD part.\n'}


您会看到列表由于\ n字符而移位,并且字典已损坏。因此,我建议进行硬切片

out = re.split(pat, text)
lead = out[0:1]; ### Keep the lead available in case needed
sp = out[1:]

print sp
dic = {k:v for k,v in zip(sp[0::2],sp[1::2])}

print(dic)

# {'introduction': '',
#  'Methodologies': ' This is the SECOND part.\n',
#  'results': ' This is the THIRD part.\n'}

10-07 22:37