正则表达式SpaCy

时间:2018-01-12 08:32:21

标签: regex pandas nltk spacy

我正在创建一个spaCy正则表达式匹配匹配数字并提取它pandas数据框。

问题:熊猫从数字中提取但覆盖了价值而不是追加。怎么解决?

(原始代码:yarongon)

from __future__ import unicode_literals
import spacy
import re
import pandas as pd
from datetime import date
nlp = spacy.load('en_core_web_sm', disable=['parser', 'tagger', 'ner'])
doc = nlp("This is a sample number: 11. This is second sample number: 1145.")
NUM_PATTERN = re.compile(r"\d+")
for match in re.finditer(NUM_PATTERN, doc.text):
    start, end = match.span()
    Number = doc.char_span(start, end)
    print Number
pandas_attributes = [Number,]
df = pd.DataFrame(pandas_attributes,
                  columns=['Number'])
print df

输出:

11
1145
  Number
0   1145

Expected output:
      Number
o      11 
1      1145

编辑1: 我正在尝试单个文本上的多个模式匹配。

from __future__ import unicode_literals
import spacy
import re
import pandas as pd
from datetime import date
nlp = spacy.load('en_core_web_sm', disable=['parser', 'tagger', 'ner'])
doc = nlp("This is a sample-number: 11. This is second sample number: 1145.")
NUM_PATTERN = re.compile(r"\d+")
HYPH_PATTERN = re.compile('\w+(?:-)\w+')

for match in re.finditer(NUM_PATTERN, doc.text):
    start, end = match.span()
    Number = doc.char_span(start, end)
    print Number

for match in re.finditer(HYPH_PATTERN, doc.text):
    start, end = match.span()
    Hyph_word = doc.char_span(start, end)
    print Hyph_word

pandas_attributes = [Number,Hyph_word]
df = pd.DataFrame(pandas_attributes,
                  columns=['Number','Hyphenword'])
print df

当前输出。

Output:
11
1145
sample-number

AssertionError: 2 columns passed, passed data had 3 columns

Expected output:
Number  Hyphen_word
11      sample-number
1145  

编辑2:输出

                Number Hyphenword
0                 (11)     (1145)
1  (sample, -, number)       Non

Expected output:

    Number   Hyphenword
0        11   sample-word
1      1145   Non

1 个答案:

答案 0 :(得分:2)

您需要追加值以循环列出:

L = []
for match in re.finditer(NUM_PATTERN, doc.text):
    start, end = match.span()
    L.append(doc.char_span(start, end))

然后使用DataFrame构造函数:

df = pd.DataFrame(L,columns=['Number'])

您还可以使用多个值附加元组:

样品:

L = []
for x in range(3):
    Number = x + 1
    Val = x + 4
    L.append((Number, Val))

print (L)
[(1, 4), (2, 5), (3, 6)]

df = pd.DataFrame(L,columns=['Number', 'Val'])
print (df)
   Number  Val
0       1    4
1       2    5
2       3    6

我相信你可以使用双append

PATTERNS = [NUM_PATTERN, HYPH_PATTERN]

pandas_attributes = []
for pat in PATTERNS:
    L = []
    for match in re.finditer(pat, doc.text):
        start, end = match.span()
        L.append(doc.char_span(start, end))
    pandas_attributes.append(L) 

df = pd.DataFrame(pandas_attributes,
                  index=['Number','Hyphenword']).T