대괄호 정규 표현식의 발생 횟수를 계산합니다.

대괄호 정규 표현식의 발생 횟수를 계산합니다.

재귀 대괄호 표현식이 포함된 정규 표현식의 발생 횟수를 계산하려고 합니다. 내 특별한 경우에는 줄이나 파일별로 발생 횟수를 계산하려고 합니다 (NP *) (VP *) (NP *). 내 예제 파일에는 다음이 포함되어 있습니다(라인 4에는 재귀 사례가 있음).

$ more mini.example 
    <parse> (NP (NN opposition)) (VP et) (NP gouvernement) (NP (NN opposition)) (VP et) (NP gouvernement) (NP (NN opposition)) (VP et) (NP gouvernement) </parse>
    <parse> (NP (NN opposition)) (XP et) (NP gouvernement) (NP (NN opposition)) (VP et) (NP gouvernement) (NP (NN opposition)) (VP et) (NP gouvernement) </parse>
    <parse> (NP (NN opposition)) (VP et) (NP gouvernement) (NP (NN opposition)) (VP et) (NP gouvernement) </parse>
    <parse> (NP (NN opposition)) (VP et) (NP gouvernement (NP (NN opposition)) (VP et) (NP gouvernement))  </parse>
    <parse> (NP (NN opposition)) (VP et) (FP gouvernement) (NP (NN opposition)) (RP et) (NP gouvernement) </parse>
    <parse> (NP (NN opposition)) (VP et) </parse>
    <parse> (VP et) (NP gouvernement) </parse>

나는 다음과 같은 출력을 원합니다 :

3 1
2 2
2 3
2 4
0 5
0 6

나는 이것을 시도했습니다 :

$ grep -Pon '(?<=\(NP ).*(?=\).*(?<=\(VP ).*(?=\).*(?<=\(NP ).*(?=\))))' mini.example | cut -d : -f 1 | uniq -c | sort -k 1

그러나 출력은 다음과 같습니다.

1 1
1 2
1 4
1 5
1 6

이는 요구되는 것과 다릅니다. 전체 패턴이 일치하지 않고 재귀를 확인할 수 없는 경우에도 패턴의 첫 번째 부분을 고유하게 평가합니다. 도움을 주셔서 감사합니다.

답변1

어쩌면 다음과 같은 것일 수도 있습니다.

grep -nPo '(?=(\((?:[^()]++|(?1))*\)) (?=\(VP)(?1) (?=\(NP)(?1))\(NP' |
 cut -d: -f1 | uniq -c

즉, (NPa의 시작이면 a와 일치하고 해당 부분 ( 직접 부분) (NP *) (VP *) (NP *)에 대해 PCRE 재귀 매칭을 사용합니다.(...)(\((?:[^()]++|(?1))*\))pcrepattern 매뉴얼 페이지에서).

관련 정보