(base) C:\Users\Jane Eyre>python
Python 3.14.6 | packaged by Anaconda, Inc. | (main, Jul  9 2026, 14:29:05) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> br_tw = nltk.corpus.brown.tagged_words(categories='mystery')
>>> br_tw[:5]
[('There', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN')]
>>> len(br_tw)
57169
>>> br_ts = nltk.corpus.brown.tagged_sents(categories='mystery')
>>> br_ts[0]
[('There', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN'), ('the', 'AT'), ('bus', 'NN'), ('the', 'AT'), ('morning', 'NN'), ('I', 'PPSS'), ('left', 'VBD'), ('for', 'IN'), ('Hanover', 'NP'), (',', ','), ('most', 'AP'), ('of', 'IN'), ('them', 'PPO'), ('disturbed', 'VBN'), ('and', 'CC'), ('hallucinating', 'VBG'), ('.', '.')]
>>> br_ts[1]
[('An', 'AT'), ('interne', 'NN'), (',', ','), ('a', 'AT'), ('nurse', 'NN'), ('and', 'CC'), ('two', 'CD'), ('attendants', 'NNS'), ('were', 'BED'), ('in', 'IN'), ('charge', 'NN'), ('of', 'IN'), ('us', 'PPO'), ('.', '.')]
>>> len(br_ts)
3886

# ---------------------------- Lowercasing tokens

>>> br_tw[:5]
[('There', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN')]
>>> [(w,t) for (w,t) in br_tw[:5]]
[('There', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN')]
>>> [(w.lower(),t) for (w,t) in br_tw[:5]]
[('there', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN')]
>>> br_tw_lower = [(w.lower(),t) for (w,t) in br_tw]
>>> br_tw_lower[:30]
[('there', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN'), ('the', 'AT'), ('bus', 'NN'), ('the', 'AT'), ('morning', 'NN'), ('i', 'PPSS'), ('left', 'VBD'), ('for', 'IN'), ('hanover', 'NP'), (',', ','), ('most', 'AP'), ('of', 'IN'), ('them', 'PPO'), ('disturbed', 'VBN'), ('and', 'CC'), ('hallucinating', 'VBG'), ('.', '.'), ('an', 'AT'), ('interne', 'NN'), (',', ','), ('a', 'AT'), ('nurse', 'NN'), ('and', 'CC'), ('two', 'CD'), ('attendants', 'NNS'), ('were', 'BED')]

# ----------------------------- POS frequencies of word types

>>> br_cfd = nltk.ConditionalFreqDist(br_tw_lower)
>>> br_cfd['so']
FreqDist({'RB': 48, 'QL': 44, 'CS': 34})
>>> br_cfd['question']
FreqDist({'NN': 5, 'VB': 3})
>>> br_cfd['that']
FreqDist({'CS': 251, 'DT': 198, 'WPS': 63, 'QL': 13, 'WPO': 1})

# ----------------------------- Looking up tags through nltk.help
#                               If error, download: nltk.download('tagsets_json')

>>> nltk.help.brown_tagset('DT')
DT: determiner/pronoun, singular
    this each another that 'nother
>>> br_cfd['that']
FreqDist({'CS': 251, 'DT': 198, 'WPS': 63, 'QL': 13, 'WPO': 1})
>>> nltk.help.brown_tagset('WPS')
WPS: WH-pronoun, nominative
    that who whoever whosoever what whatsoever
>>> nltk.help.brown_tagset('WPO')
WPO: WH-pronoun, accusative
    whom that who
>>> br_cfd['like']
FreqDist({'CS': 103, 'VB': 19, 'IN': 14, 'JJ': 3})
>>> br_ts[0]
[('There', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN'), ('the', 'AT'), ('bus', 'NN'), ('the', 'AT'), ('morning', 'NN'), ('I', 'PPSS'), ('left', 'VBD'), ('for', 'IN'), ('Hanover', 'NP'), (',', ','), ('most', 'AP'), ('of', 'IN'), ('them', 'PPO'), ('disturbed', 'VBN'), ('and', 'CC'), ('hallucinating', 'VBG'), ('.', '.')]
>>> [s for s in br_ts if ('like', 'JJ') in s][0]
[('smarter', 'JJR'), (',', ','), ('and', 'CC'), ('wear', 'VB'), ('different', 'JJ'), ('kinds', 'NNS'), ('of', 'IN'), ('clothes', 'NNS'), ('--', '--'), ("she'd", 'PPS+MD'), ('be', 'BE'), ('Katharine', 'NP'), ('Ross', 'NP'), (',', ','), ('just', 'RB'), ('what', 'WDT'),('that', 'WPS'), ('sounded', 'VBD'), ('like', 'JJ'), ('.', '.')]
>>> [s for s in br_ts if ('like', 'JJ') in s][1]
[('``', '``'), ("Can't", 'MD*'), ('you', 'PPSS'), ('possibly', 'RB'), ('imagine', 'VB'), ('what', 'WDT'), ('life', 'NN'), ('is', 'BEZ'), ('going', 'VBG'), ('to', 'TO'), ('be', 'BE'), ('like', 'JJ'), (',', ','), ('here', 'RB'), ("''", "''"), ('?', '.'), ('?', '.')]
>>> [s for s in br_ts if ('like', 'IN') in s][0]
[('``', '``'), ('It', 'PPS'), ('sure', 'RB'), ('as', 'CS'), ('hell', 'NN'), ("doesn't", 'DOZ*'), ('look', 'VB'), ('like', 'IN'), ('it', 'PPO'), (',', ','), ('does', 'DOZ'), ('it', 'PPS'), ('?', '.'), ('?', '.')]

# ----------------------------- building trigrams

>>> br_3grams = list(nltk.ngrams(br_tw_lower, 3))
>>> br_3grams[:5]
[(('there', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD')), (('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS')), (('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN')), (('patients', 'NNS'), ('on', 'IN'), ('the', 'AT')), (('on', 'IN'), ('the', 'AT'),('bus', 'NN'))]
>>> br_3grams[0]
(('there', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD'))
>>> br_3grams[0][1]
('were', 'BED')
>>> br_3grams[0][1][1]
'BED'

# --------------------------- conditional frequency of preceding POS

>>> [((w1,t1),(w2,t2),(w3,t3)) for ((w1,t1),(w2,t2),(w3,t3)) in br_3grams[:5]]
[(('there', 'EX'), ('were', 'BED'), ('thirty-eight', 'CD')), (('were', 'BED'), ('thirty-eight', 'CD'), ('patients', 'NNS')), (('thirty-eight', 'CD'), ('patients', 'NNS'), ('on', 'IN')), (('patients', 'NNS'), ('on', 'IN'), ('the', 'AT')), (('on', 'IN'), ('the', 'AT'),('bus', 'NN'))]
>>> [(w2+"/"+t2, t1) for ((w1,t1),(w2,t2),(w3,t3)) in br_3grams[:5]]
[('were/BED', 'EX'), ('thirty-eight/CD', 'BED'), ('patients/NNS', 'CD'), ('on/IN', 'NNS'), ('the/AT', 'IN')]
>>> tag_pre = [(w2+"/"+t2, t1) for ((w1,t1),(w2,t2),(w3,t3)) in br_3grams]
>>> tag_pre[-5:]
[('the/AT', 'VBD'), ('money/NN', 'AT'), ('with/IN', 'NN'), ('me/PPO', 'IN'), ("''/''", 'PPO')]
>>> tag_pre_cfd = nltk.ConditionalFreqDist(tag_pre)
>>> tag_pre_cfd['so/CS']
FreqDist({'.': 7, ',': 6, 'NN': 5, 'NNS': 3, 'QLP': 2, 'PPO': 2, 'VBD': 2, '--': 2, 'RB': 1, 'CC': 1, ...})
>>> tag_pre_cfd['so/RB']
FreqDist({'CC': 10, '.': 10, 'VB': 4, ',': 4, 'DO': 3, '``': 3, 'VBZ': 2, 'VBG': 2, 'BEDZ*': 2, '--': 1, ...})
>>> tag_pre_cfd['so/QL']
FreqDist({'IN': 5, 'BEDZ': 5, ',': 4, 'RB': 3, 'NN': 3, 'QL': 3, 'VBN': 2, 'VB': 2, 'PPO': 2, 'BEN': 1, ...})

# --------------------------- conditional frequency of following POS

>>> tag_post = [(w2+"/"+t2, t3) for ((w1,t1),(w2,t2),(w3,t3)) in br_3grams]
>>> tag_post_cfd = nltk.ConditionalFreqDist(tag_post)
>>> tag_post_cfd['so/CS']
FreqDist({'PPS': 9, 'CS': 6, 'PPSS': 6, 'AT': 5, 'PPS+MD': 1, 'EX': 1, 'NP': 1, 'DT': 1, 'WRB': 1, 'AP': 1, ...})
>>> nltk.help.brown_tagset('PPS')
PPS: pronoun, personal, nominative, 3rd person singular
    it he she thee
>>> tag_post_cfd['so/RB']
FreqDist({'.': 10, ',': 6, "''": 5, 'IN': 3, 'RB': 3, 'CS': 3, 'VB': 2, 'PN': 2, 'NP': 2, 'DOD': 2, ...})
>>> tag_post_cfd['so/QL']
FreqDist({'JJ': 18, 'RB': 14, 'AP': 7, 'QL': 3, 'VBN': 2})
>>> br_cfd['to']
FreqDist({'TO': 798, 'IN': 496})
>>> tag_pre_cfd['to/TO']
FreqDist({'NN': 108, 'VBD': 106, 'VBG': 76, 'VB': 66, 'VBN': 56, 'PPO': 51, 'JJ': 50, 'RB': 50, 'HV': 34, 'NNS': 24, ...})
>>> tag_post_cfd['to/TO']
FreqDist({'VB': 670, 'BE': 73, 'DO': 28, 'HV': 17, '.': 3, "''": 3, '--': 1, 'NN': 1, ',': 1, '``': 1})
>>> tag_post_cfd['to/TO'].freq('VB')
0.8395989974937343


# ---------------------------- Now onto Penn Treebank

>>> from nltk.corpus import treebank
>>> treebank.words()
['Pierre', 'Vinken', ',', '61', 'years', 'old', ',', ...]
>>> treebank.tagged_words()
[('Pierre', 'NNP'), ('Vinken', 'NNP'), (',', ','), ...]
>>> treebank.tagged_words()[:10]
[('Pierre', 'NNP'), ('Vinken', 'NNP'), (',', ','), ('61', 'CD'), ('years', 'NNS'), ('old', 'JJ'), (',', ','), ('will', 'MD'), ('join', 'VB'), ('the', 'DT')]
>>> treebank.tagged_sents()[0]
[('Pierre', 'NNP'), ('Vinken', 'NNP'), (',', ','), ('61', 'CD'), ('years', 'NNS'), ('old', 'JJ'), (',', ','), ('will', 'MD'), ('join', 'VB'), ('the', 'DT'), ('board', 'NN'), ('as', 'IN'), ('a', 'DT'), ('nonexecutive', 'JJ'), ('director', 'NN'), ('Nov.', 'NNP'), ('29', 'CD'), ('.', '.')]
>>> treebank.tagged_sents()[1]
[('Mr.', 'NNP'), ('Vinken', 'NNP'), ('is', 'VBZ'), ('chairman', 'NN'), ('of', 'IN'), ('Elsevier', 'NNP'), ('N.V.', 'NNP'), (',', ','), ('the', 'DT'), ('Dutch', 'NNP'), ('publishing', 'VBG'), ('group', 'NN'), ('.', '.')]
>>> treebank.tagged_sents()[2]
[('Rudolph', 'NNP'), ('Agnew', 'NNP'), (',', ','), ('55', 'CD'), ('years', 'NNS'), ('old', 'JJ'), ('and', 'CC'), ('former', 'JJ'), ('chairman', 'NN'), ('of', 'IN'), ('Consolidated', 'NNP'), ('Gold', 'NNP'), ('Fields', 'NNP'), ('PLC', 'NNP'), (',', ','), ('was', 'VBD'), ('named', 'VBN'), ('*-1', '-NONE-'), ('a', 'DT'), ('nonexecutive', 'JJ'), ('director', 'NN'), ('of', 'IN'), ('this', 'DT'), ('British', 'JJ'), ('industrial', 'JJ'), ('conglomerate', 'NN'), ('.', '.')]
>>> treebank.tagged_sents()[9]
[('There', 'EX'), ('is', 'VBZ'), ('no', 'DT'), ('asbestos', 'NN'), ('in', 'IN'), ('our', 'PRP$'), ('products', 'NNS'), ('now', 'RB'),('.', '.'), ("''", "''")]
>>> len(treebank.tagged_words())
100676
>>> len(treebank.tagged_sents())
3914

# ---------------------------- Syntactic trees

>>> treebank.parsed_sents()[9]
Tree('S', [Tree('NP-SBJ', [Tree('EX', ['There'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('NP-PRD', [Tree('DT', ['no']), Tree('NN', ['asbestos'])]), Tree('PP-LOC', [Tree('IN', ['in']), Tree('NP', [Tree('PRP$', ['our']), Tree('NNS', ['products'])])]), Tree('ADVP-TMP',[Tree('RB', ['now'])])]), Tree('.', ['.']), Tree("''", ["''"])])
>>> treebank.parsed_sents()[9].pprint()
(S
  (NP-SBJ (EX There))
  (VP
    (VBZ is)
    (NP-PRD (DT no) (NN asbestos))
    (PP-LOC (IN in) (NP (PRP$ our) (NNS products)))
    (ADVP-TMP (RB now)))
  (. .)
  ('' ''))
>>> treebank.parsed_sents()[9].draw()

# --------------------------- Syntactic trees are RECURSIVE!
#                             Now back to tagged words:

>>> treebank.tagged_words()[:10]
[('Pierre', 'NNP'), ('Vinken', 'NNP'), (',', ','), ('61', 'CD'), ('years', 'NNS'), ('old', 'JJ'), (',', ','), ('will', 'MD'), ('join', 'VB'), ('the', 'DT')]
>>> tb_cfd = nltk.ConditionalFreqDist(treebank.tagged_words())
>>> tb_cfd['question']
FreqDist({'NN': 12, 'VBP': 1, 'VB': 1})

# --------------------------- Penn Treebank tagset definitions

>>> nltk.help.upenn_tagset('VBP')
VBP: verb, present tense, not 3rd person singular
    predominate wrap resort sue twist spill cure lengthen brush terminate
    appear tend stray glisten obtain comprise detest tease attract
    emphasize mold postpone sever return wag ...
>>> nltk.help.upenn_tagset('VB')
VB: verb, base form
    ask assemble assess assign assume atone attention avoid bake balkanize
    bank begin behold believe bend benefit bevel beware bless boil bomb
    boost brace break bring broil brush build ...
>>> nltk.help.upenn_tagset('V.*')
VB: verb, base form
    ask assemble assess assign assume atone attention avoid bake balkanize
    bank begin behold believe bend benefit bevel beware bless boil bomb
    boost brace break bring broil brush build ...
VBD: verb, past tense
    dipped pleaded swiped regummed soaked tidied convened halted registered
    cushioned exacted snubbed strode aimed adopted belied figgered
    speculated wore appreciated contemplated ...
VBG: verb, present participle or gerund
    telegraphing stirring focusing angering judging stalling lactating
    hankerin' alleging veering capping approaching traveling besieging
    encrypting interrupting erasing wincing ...
VBN: verb, past participle
    multihulled dilapidated aerosolized chaired languished panelized used
    experimented flourished imitated reunifed factored condensed sheared
    unsettled primed dubbed desired ...
VBP: verb, present tense, not 3rd person singular
    predominate wrap resort sue twist spill cure lengthen brush terminate
    appear tend stray glisten obtain comprise detest tease attract
    emphasize mold postpone sever return wag ...
VBZ: verb, present tense, 3rd person singular
    bases reconstructs marks mixes displeases seals carps weaves snatches
    slumps stretches authorizes smolders pictures emerges stockpiles
    seduces fizzes uses bolsters slaps speaks pleads ...

>>> tb_cfd['Pierre']
FreqDist({'NNP': 1})
>>> tb_cfd['will']
FreqDist({'MD': 280, 'NN': 1})
>>> tb_cfd['share']
FreqDist({'NN': 116, 'VB': 3})

# --------------------------- 'share' is overwhelmingly a noun?? Digging deeper...

>>> dir(treebank)
['_LazyCorpusLoader__args', '_LazyCorpusLoader__kwargs', '_LazyCorpusLoader__name', '_LazyCorpusLoader__reader_cls', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__','__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', '_citation', '_comment_char', '_detect_blocks', '_encoding', '_fileids', '_get_root', '_license', '_normalize', '_parse', '_read_block','_read_parsed_sent_block', '_read_sent_block', '_read_tagged_sent_block', '_read_tagged_word_block', '_read_word_block', '_readme', '_root', '_tag', '_tagset', '_unload', '_word', 'abspath', 'abspaths', 'citation', 'encoding', 'ensure_loaded', 'fileids', 'license', 'open', 'parsed_sents', 'raw', 'readme', 'root', 'sents', 'subdir', 'tagged_sents', 'tagged_words', 'words']
>>> print(treebank.readme())
[ PENN TREEBANK SAMPLE ]
http://www.cis.upenn.edu/~treebank/home.html

This is a ~5% fragment of Penn Treebank, (C) LDC 1995.  It is made
available under fair use for the purposes of illustrating NLTK tools
for tokenizing, tagging, chunking and parsing.  This data is for
non-commercial use only.

Contents: raw, tagged, parsed and combined data from Wall Street
Journal for 1650 sentences (99 treebank files wsj_0001 .. wsj_0099).
For details about each of the four types, please see the other README
files included in the treebank sample directory.  Examples of the four
types are shown below:

### The rest of readme clipped. Wall Street Journal it is!


# --------------------------- More tag ambiguity

>>> tb_cfd['fly']
FreqDist({'VB': 1, 'VBP': 1})
>>> tb_cfd['like']
FreqDist({'IN': 49, 'VB': 8, 'VBP': 4, 'JJ': 1})
>>> tb_cfd['so']         # only RB and IN?
FreqDist({'RB': 55, 'IN': 16})
>>> nltk.help.upenn_tagset('RB')
RB: adverb
    occasionally unabatingly maddeningly adventurously professedly
    stirringly prominently technologically magisterially predominately
    swiftly fiscally pitilessly ...
>>> nltk.help.upenn_tagset('IN')
IN: preposition or conjunction, subordinating
    astride among uppon whether out inside pro despite on by throughout
    below within for towards near behind atop around if like until below
    next into if beside ...

# --------------------------- Treebank treats CS usage of 'so' as part of its preposition (IN) use
#                               and its QL (qualifier) use as RB (adverb) broadly

# --------------------------- Let's try NLTK's built-in POS tagger
#                             warning: not that great

>>> sent = 'Colorless green ideas sleep furiously.'
>>> nltk.word_tokenize(sent)
['Colorless', 'green', 'ideas', 'sleep', 'furiously', '.']
>>> toks = nltk.word_tokenize(sent)
>>> toks
['Colorless', 'green', 'ideas', 'sleep', 'furiously', '.']
>>> nltk.pos_tag(toks)
LookupError:
**********************************************************************
  Resource averaged_perceptron_tagger_eng not found.
  Please use the NLTK Downloader to obtain the resource:
  >>> import nltk
  >>> nltk.download('averaged_perceptron_tagger_eng')
  For more information see: https://www.nltk.org/data.html
>>> nltk.download('averaged_perceptron_tagger_eng')   # download tagger model first
[nltk_data] Downloading package averaged_perceptron_tagger_eng to
[nltk_data]     C:\Users\Jane Eyre\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping taggers\averaged_perceptron_tagger_eng.zip.
True
>>> nltk.pos_tag(toks)
[('Colorless', 'NNP'), ('green', 'JJ'), ('ideas', 'NNS'), ('sleep', 'VBP'), ('furiously', 'RB'), ('.', '.')]
>>> nltk.help.upenn_tagset('NNP')
NNP: noun, proper, singular
    Motown Venneboerger Czestochwa Ranzer Conchita Trumplane Christos
    Oceanside Escobar Kreisler Sawyer Cougar Yvette Ervin ODI Darryl CTCA
    Shannon A.K.C. Meltex Liverpool ...
>>> sent = 'She sells seashells by the seashore.'
>>> nltk.pos_tag(nltk.word_tokenize(sent))
[('She', 'PRP'), ('sells', 'VBZ'), ('seashells', 'NNS'), ('by', 'IN'), ('the', 'DT'), ('seashore', 'NN'), ('.', '.')]
>>> nltk.help.upenn_tagset('PRP')
PRP: pronoun, personal
    hers herself him himself hisself it itself me myself one oneself ours
    ourselves ownself self she thee theirs them themselves they thou thy us
>>> sent = 'Time flies like an arrow and fruit flies like bananas.'
>>> nltk.pos_tag(nltk.word_tokenize(sent))
[('Time', 'NNP'), ('flies', 'NNS'), ('like', 'IN'), ('an', 'DT'), ('arrow', 'NN'), ('and', 'CC'), ('fruit', 'NN'), ('flies', 'NNS'), ('like', 'IN'), ('bananas', 'NNS'), ('.', '.')]

>>> nltk.pos_tag('January was a cold month .'.split())
[('January', 'NNP'), ('was', 'VBD'), ('a', 'DT'), ('cold', 'JJ'), ('month', 'NN'), ('.', '.')]
>>> nltk.pos_tag('Yesterday was a cold day .'.split())
[('Yesterday', 'NN'), ('was', 'VBD'), ('a', 'DT'), ('cold', 'JJ'), ('day', 'NN'), ('.', '.')]
>>>