【问题标题】:Parsing two-dimensional text解析二维文本
【发布时间】:2010-05-02 16:52:32
【问题描述】:

我需要解析文本文件,其中相关信息通常以非线性方式分布在多行中。一个例子:

1234
 1         IN THE SUPERIOR COURT OF THE STATE OF SOME STATE           
 2              IN AND FOR THE COUNTY OF SOME COUNTY                
 3                      UNLIMITED JURISDICTION                        
 4                            --o0o--                                 
 5                                                                    
 6   JOHN SMITH AND JILL SMITH,         )                             
                                        )                             
 7                  Plaintiffs,         )                             
                                        )                             
 8        vs.                           )     No. 12345
                                        )                             
 9   ACME CO, et al.,                   )                             
                                        )                             
10                  Defendants.         )                             
     ___________________________________)                             

我需要提取原告和被告的身份。

这些成绩单有各种各样的格式,所以我不能总是指望那些漂亮的括号在那里,或者原告和被告的信息被整齐地框起来,例如:

 1        SUPREME COURT OF THE STATE OF SOME OTHER STATE
                      COUNTY OF COUNTYVILLE
 2                  First Judicial District
                     Important Litigation
 3  --------------------------------------------------X
    THIS DOCUMENT APPLIES TO:
 4
    JOHN SMITH,
 5                            Plaintiff,          Index No.
                                                  2000-123
 6
                                            DEPOSITION
 7                  - against -             UNDER ORAL
                                            EXAMINATION
 8                                              OF
                                            JOHN SMITH,
 9                                           Volume I

10  ACME CO,
    et al,
11                            Defendants.

12  --------------------------------------------------X

这两个常数是:

  1. “原告”将在 原告的姓名,但不是 必须在同一行。
  2. 原告和被告姓名 将大写。

有什么想法吗?

【问题讨论】:

  • 左边的数字是多少?你添加了这些还是它们是源的一部分?你说原告是大写的,但“JOHN SMITH and JILL SMITH”包含小写字母。原告姓名和“原告”文字之间可能有哪些字符?是纯空格、括号和逗号吗?
  • 这些是源代码的行号。我已经更正了原告姓名的大写。原告姓名和“原告”之间可以是任何东西。不保证只有非字母字符和空格。
  • 你总是可以使用神经网络。这些适用于文本解析:thedailywtf.com/Articles/No,_We_Need_a_Neural_Network.aspx
  • 我想我可能需要应用一些机器学习:我很难传达这些脚本文件的不一致程度。你们都为我发布的示例提交了非常好的解决方案,但是对于您处理的每个特殊情况,我可以找到另外三个违反和破坏您的解决方案的成绩单(当然是由不同的转录公司编写的)。我正在考虑增强一个简单的词法解析器。
  • 人们有点看着你的两个例子并说“我能做到”而不是看到一般问题,这真的很难(至少 N^3 难!)。我立即想到:必须将文本放入二维数组中,以便检测“岛屿”。无论如何,只是为了我自己将来的参考,我指向这篇 MS 研究论文:research.microsoft.com/pubs/69347/docgeom_icdar2005.pdf

标签: regex text text-parsing


【解决方案1】:

我喜欢Martin's answer
这可能是使用Python 的更通用的方法:

import re

# load file into memory 
# (if large files, provide some limit to how much of the file gets loaded)
with open('paren.txt','r') as f:
  paren = f.read() # example doc with parens

# match all sequences of one or more alphanumeric (or underscore) characters 
# when followed by the word `Plaintiff`; this is intentionally general
list_of_matches = re.findall(r'(\w+)(?=.*Plaintiff)', paren, 
    re.DOTALL|re.MULTILINE)

# join the list separating by whitespace
str_of_matches = ' '.join(list_of_matches)

# split string by digits (line numbers)
tokens = re.split(r'\d',str_of_matches)

# plaintiffs will be in 2nd-to-last group
plaintiff = tokens[-2].strip()

测试:

with open('paren.txt','r') as f:
  paren = f.read() # example doc with parens
list_of_matches = re.findall(r'(\w+)(?=.*Plaintiff)',paren,
  re.DOTALL|re.MULTILINE)
str_of_matches = ' '.join(list_of_matches)>>> tokens = re.split(r'\d', str_of_matches)
tokens = re.split(r'\d', str_of_matches)
plaintiff = tokens[-2].strip()
plaintiff
# prints 'JOHN SMITH and JILL SMITH'

with open('no_paren.txt','r') as f:
  no_paren = f.read() # example doc with no parens
list_of_matches = re.findall(r'(\w+)(?=.*Plaintiff)',no_paren,
  re.DOTALL|re.MULTILINE)
str_of_matches = ' '.join(list_of_matches)
tokens = re.split(r'\d', str_of_matches)
plaintiff = tokens[-2].strip()
plaintiff
# prints 'JOHN SMITH'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多