NLTK¶
Description:
- A suite of open source Python modules, data sets, and tutorials supporting research and development in Natural Language Processing
Resources:
- [GitHub] https://github.com/nltk/nltk
- [Docs] https://www.nltk.org/
- [Examples] https://www.nltk.org/howto.html
Example 1¶
Example utility
In [ ]:
Copied!
from nltk.util import *
print_string("This is a long string, therefore it should break", 25)
from nltk.util import *
print_string("This is a long string, therefore it should break", 25)
Example 2¶
Create a tree
In [ ]:
Copied!
from nltk.tree import Tree
tree = Tree(5,[Tree(4, [Tree(2, [1, 3])]),
Tree(8, [Tree(6, [7]), 9])])
for x in breadth_first(tree):
if isinstance(x, int):
print(x)
else:
print(x.label())
from nltk.tree import Tree
tree = Tree(5,[Tree(4, [Tree(2, [1, 3])]),
Tree(8, [Tree(6, [7]), 9])])
for x in breadth_first(tree):
if isinstance(x, int):
print(x)
else:
print(x.label())
Example 3¶
Chunking
In [ ]:
Copied!
from nltk.chunk import *
from nltk.chunk.util import *
from nltk.chunk.regexp import *
from nltk import Tree
tagged_text = "[ The/DT cat/NN ] sat/VBD on/IN [ the/DT mat/NN ] [ the/DT dog/NN ] chewed/VBD ./."
gold_chunked_text = tagstr2tree(tagged_text)
unchunked_text = gold_chunked_text.flatten()
print(unchunked_text)
from nltk.chunk import *
from nltk.chunk.util import *
from nltk.chunk.regexp import *
from nltk import Tree
tagged_text = "[ The/DT cat/NN ] sat/VBD on/IN [ the/DT mat/NN ] [ the/DT dog/NN ] chewed/VBD ./."
gold_chunked_text = tagstr2tree(tagged_text)
unchunked_text = gold_chunked_text.flatten()
print(unchunked_text)
Example 4¶
Parsing
In [ ]:
Copied!
import pickle
import subprocess
import sys
from nltk import Nonterminal, nonterminals, Production, CFG
nt1 = Nonterminal('NP')
nt2 = Nonterminal('VP')
nt1.symbol()
import pickle
import subprocess
import sys
from nltk import Nonterminal, nonterminals, Production, CFG
nt1 = Nonterminal('NP')
nt2 = Nonterminal('VP')
nt1.symbol()