python - 如何解析半结构化文本 (cran.all.1400)

python - how to parse semi structured text (cran.all.1400)

我需要使用 cran.all.1400 文本文件。

它是文章摘要的集合,其中包含每篇文章的一些附加数据。它的形式是:

.我1
.T
空气动力学的实验研究 滑流中的机翼。
.A
布伦克曼
.B
j. ae. scs。 25, 1958, 324.
.W
//大量文本
.I 2
.T
小的不可压缩流体中的简单剪切流通过平板 粘度.
.A
婷宜
.B
伦斯勒理工学院航空工程系 研究所 特洛伊,n.y.
.W
//很多文字


等等。

我需要的是这样组织的数据:

第 1 条:.T="whatever the title of article 1 is"、.A="w/e the author is"、.B="w/e"、.T="all the text"
第2条:.T="whatever the title is",.A="w/e the author is",.B="w/e",.T="all the text"

我将如何在 Python 中执行此操作? 谢谢你的时间。

你在 .I 评论中提出的分裂想法似乎是一个好的开始。

以下似乎有效:

with open('crantest.txt') as f:
    articles = f.read().split('\n.I')

def process(i, article):
    article = article.replace('\n.T\n','.T=')
    article = '.T=' + article.split('.T=')[1] #strips off the article number, restored below
    article = article.replace('\n.A\n',',.A=')
    article = article.replace('\n.B\n',',.B=')
    article = article.replace('\n.W\n',',.W=')
    return 'article ' + str(i) + ':' + article

data = [process(i+1, article) for i,article in enumerate(articles)]

我创建了一个仅包含前 10 篇文章的测试文件(丢弃一小部分 header 和所有以 .I 11 开头的文件)。当我 运行 上面的代码时,我得到一个长度为 10 的列表。第一行以 .I 开头(之前没有换行符)很重要,因为我没有努力测试第一个条目是否分裂是空的。列表中的第一个条目是一个以以下开头的字符串:

article 1:.T=experimental investigation of the aerodynamics of a\nwing in a slipstream .,.A=brenckman,m.,.B=j. ae. scs. 25, 1958, 324.,.W=experimental investigation of the aerodynamics of a\nwing in a slipstream

On Edit 这是一个字典版本,它使用 partition 连续提取相关块。它 return 是字典的字典而不是字符串列表:

with open('crantest.txt') as f:
    articles = f.read().split('\n.I')

def process(article):
    article = article.split('\n.T\n')[1]
    T, _, article = article.partition('\n.A\n')
    A, _, article = article.partition('\n.B\n')
    B, _, W = article.partition('\n.W\n')
    return {'T':T, 'A':A, 'B':B, 'W':W}

data = {(i+1):process(article) for i,article in enumerate(articles)}

例如:

>>> data[1]
{'A': 'brenckman,m.', 'T': 'experimental investigation of the aerodynamics of a\nwing in a slipstream .', 'B': 'j. ae. scs. 25, 1958, 324.', 'W': 'experimental investigation of the aerodynamics of a\nwing in a slipstream .\n  an experimental study of a wing in a propeller slipstream was\nmade in order to determine the spanwise distribution of the lift\nincrease due to slipstream at different angles of attack of the wing\nand at different free stream to slipstream velocity ratios .  the\nresults were intended in part as an evaluation basis for different\ntheoretical treatments of this problem .\n  the comparative span loading curves, together with\nsupporting evidence, showed that a substantial part of the lift increment\nproduced by the slipstream was due to a /destalling/ or\nboundary-layer-control effect .  the integrated remaining lift\nincrement, after subtracting this destalling lift, was found to agree\nwell with a potential flow theory .\n  an empirical evaluation of the destalling effects was made for\nthe specific configuration of the experiment .'}

s.partition() returns 一个三元组,由第一次出现分隔符之前的字符串部分 s、分隔符本身以及出现该分隔符之后的字符串部分组成的分隔符。代码中的下划线 (_) 是一个 Python 惯用语,强调打算丢弃 return 值的那部分。