【问题标题】:Extracting specific information from data从数据中提取特定信息
【发布时间】:2017-02-17 02:30:21
【问题描述】:

如何转换如下数据格式:

James Smith was born on November 17, 1948

变成类似

("James Smith", DOB, "November 17, 1948")

不必依赖字符串的位置索引

我已经尝试了以下

from nltk import word_tokenize, pos_tag

new = "James Smith was born on November 17, 1948"
sentences = word_tokenize(new)
sentences = pos_tag(sentences)
grammar = "Chunk: {<NNP*><NNP*>}"
cp = nltk.RegexpParser(grammar)
result = cp.parse(sentences)
print(result)

如何进一步获得所需 fromat 的输出。

【问题讨论】:

    标签: python python-3.x nltk stanford-nlp information-retrieval


    【解决方案1】:

    用 'wasborne on' 分割字符串,然后修剪空格并分配给 name 和 dob

    【讨论】:

      【解决方案2】:

      你总是可以使用正则表达式。 正则表达式 (\S+)\s(\S+)\s\bwas born on\b\s(\S+)\s(\S+),\s(\S+) 将匹配并返回特定于上述字符串格式的数据。

      下面是实际操作:https://regex101.com/r/W2ykKS/1

      python 中的正则表达式:

      import re
      
      regex = r"(\S+)\s(\S+)\s\bwas born on\b\s(\S+)\s(\S+),\s(\S+)"
      test_str = "James Smith was born on November 17, 1948"
      
      matches = re.search(regex, test_str)
      
      # group 0 in a regex is the input string
      
      print(matches.group(1)) # James
      print(matches.group(2)) # Smith
      print(matches.group(3)) # November
      print(matches.group(4)) # 17
      print(matches.group(5)) # 1948
      

      【讨论】:

        猜你喜欢
        • 2022-12-17
        • 1970-01-01
        • 2020-10-30
        • 2018-01-06
        • 1970-01-01
        • 2022-12-18
        • 2015-09-08
        • 2015-01-20
        • 1970-01-01
        相关资源
        最近更新 更多