I have to do a parsing: the goal is to create a grammar rules that will be applied in a corpus. I have a question: is it possible to have a list within a grammar?
Example:
1) Open the text to be analyzed
2) Write the grammatical rules (just an example):
grammar("""
S -> NP VP
NP -> DET N
VP -> V N
DET -> list_det.txt
N -> list_n.txt
V -> list.txt""")
3) Print the result with the entries that obey this grammar
It's possible?
Here is a quick conceptual prototype of your grammar, using pyparsing. I could not tell from your question what the contents of the
N
,V
, andDET
lists could be, so I just arbitrarily chose words composed of 'n's and 'v's, and the literal 'det'. You can replace the<<=
assignments with the correct expressions for your grammar, but this parser and the sample string should show that your grammar is at least feasible. (If you edit your question to show whatN
,V
, andDET
are lists of, I can update this answer with less arbitrary expressions and sample. Also including a sample string to be parsed would be useful.)I also added some grouping so that you could see how the structure of the grammar is reflected in the structure of the results. You can leave this in or removve it and the parser will still work.
Prints:
EDIT:
I guessed the "NP" and "VP" are "noun phrase" and "verb phrase", but I don't know what "DET" could be. Still, I made up a less abstract example. I also expanded the lists to accept more grammatical forms of lists of nouns and verbs, with connecting 'and's and commas.
Prints: