Error in Tree Structure
Steven D'Aprano
steve at pearwood.info
Sat Feb 27 06:29:17 EST 2016
On Sat, 27 Feb 2016 08:17 pm, subhabangalore at gmail.com wrote:
> Is it any error in Python part or in NLTK part?
Neither.
Any time you think there is an error in Python, it is 99.9% sure that the
error is in your code, not Python.
If the error is a SyntaxError, that is 99.99999%.
> If any one may guide me what is the error I am doing and how may I solve
> it.
Look at the SyntaxError traceback and read what it says. Does it tell you
what the error is? Does it use a ^ as an arrow to point to the error, or
immediately after the error?
Chances are, the error is that you have added or deleted a bracket or
parenthesis somewhere. Dealing with nested lists like this is usually
awful, because they are unreadable. Can you avoid copying and pasting the
nested list?
If not, try re-formatting it so you can at least read it:
# Unreadable, bad:
thelist = [Tree('S', [Tree('LOCATION', [(u'NAIROBI', 'NNP')]), (u',', ','),
Tree('LOCATION', [(u'Kenya', 'NNP')]), (u'(', '('), Tree('ORGANIZATION',
[(u'AP', 'NNP')]), (u')', ')'), (u'_', 'NNP'), Tree('CARDINAL',
[(u'Thousands', 'NNP')]), (u'of', 'IN'), (u'laborers,', 'JJ'),
(u'students', 'NNS'), (u'and', 'CC'), (u'opposition', 'NN'),
(u'politicians', 'NNS'), (u'on', 'IN'), Tree('DATE',
[(u'Saturday', 'NNP')]), (u'protested', 'VBD'), (u'tax', 'NN'),
(u'hikes', 'NNS'), (u'imposed', 'VBN'), (u'by', 'IN'), (u'their', 'PRP$'),
(u'cash-strapped', 'JJ'), (u'government,', 'NN'), (u'which', 'WDT'),
(u'they', 'PRP'), (u'accused', 'VBD'), (u'of', 'IN'), (u'failing', 'VBG'),
(u'to', 'TO'), (u'provide', 'VB'), (u'basic', 'JJ'), (u'services.', 'NN'),
(u'(cm-kjd)', 'NN')])]
# Slightly more readable, good:
thelist = [
Tree('S', [
Tree(
'LOCATION', [
(u'NAIROBI', 'NNP')
]
),
(u',', ','),
Tree(
'LOCATION', [
(u'Kenya', 'NNP')
]
),
(u'(', '('),
Tree(
'ORGANIZATION', [
(u'AP', 'NNP')
]
),
(u')', ')'),
(u'_', 'NNP'),
Tree(
'CARDINAL', [
(u'Thousands', 'NNP')
]
),
(u'of', 'IN'),
(u'laborers,', 'JJ'),
(u'students', 'NNS'),
(u'and', 'CC'),
(u'opposition', 'NN'),
(u'politicians', 'NNS'),
(u'on', 'IN'),
Tree(
'DATE', [
(u'Saturday', 'NNP')
]
),
(u'protested', 'VBD'),
(u'tax', 'NN'),
(u'hikes', 'NNS'),
(u'imposed', 'VBN'),
(u'by', 'IN'),
(u'their', 'PRP$'),
(u'cash-strapped', 'JJ'),
(u'government,', 'NN'),
(u'which', 'WDT'),
(u'they', 'PRP'),
(u'accused', 'VBD'),
(u'of', 'IN'),
(u'failing', 'VBG'),
(u'to', 'TO'),
(u'provide', 'VB'),
(u'basic', 'JJ'),
(u'services.', 'NN'),
(u'(cm-kjd)', 'NN')
]
)
]
If you format your giant list so you can see the structure, then you will
likely find where there is a problem. Perhaps you have:
- missing or too many [ ( ) or ]
- a missing or extra comma
- a missing or extra quotation marks
- unexpected symbols like .... inside the list
--
Steven
More information about the Python-list
mailing list