2

what is the prime purpose of the using text.common_contexts() in nltk.Guys I have searched and gone through as best as I could do. but sorry to say I didn't understand a bit.please help me by giving an example.Thank you.

dex
  • 121
  • 2
  • 5
  • 1
    I think you should support your question with a specific example/code. Literally, you are asking for educational content, which is not the purpose of this forum. – Ibo Feb 20 '18 at 18:09
  • See http://www.nltk.org/book/ch01.html section 1.3 – alvas Feb 21 '18 at 05:56
  • 1
    Very nicely explained in the link below: [common_contexts](https://stackoverflow.com/questions/43438008/difference-between-similar-and-concordance-in-nltk) – Saby Feb 27 '18 at 09:02

2 Answers2

2

Example to understand:

Let's first define our input text, I will just Copy/Paste the first paragraph of Game of Thrones Wikipedia page:

input_text = "Game of Thrones is an American fantasy drama television series \
created by David Benioff and D. B. Weiss for HBO. It is an adaptation of A Song \
of Ice and Fire, George R. R. Martin's series of fantasy novels, the first of \
which is A Game of Thrones. The show was filmed in Belfast and elsewhere in the \
United Kingdom, Canada, Croatia, Iceland, Malta, Morocco, Spain, and the \
United States.[1] The series premiered on HBO in the United States on April \
17, 2011, and concluded on May 19, 2019, with 73 episodes broadcast over \
eight seasons. Set on the fictional continents of Westeros and Essos, Game of \
Thrones has several plots and a large ensemble cast, and follows several story \
arcs. One arc is about the Iron Throne of the Seven Kingdoms, and follows a web \
of alliances and conflicts among the noble dynasties either vying to claim the \
throne or fighting for independence from it. Another focuses on the last \
descendant of the realm's deposed ruling dynasty, who has been exiled and is \
plotting a return to the throne, while another story arc follows the Night's \
Watch, a brotherhood defending the realm against the fierce peoples and \
legendary creatures of the North."

To be able to apply nltk functions we need to convert our text of type 'str' to 'nltk.text.Text'.

import nltk

text = nltk.Text( input_text.split() )

text.similar()

The similar() method takes an input_word and returns other words who appear in a similar range of contexts in the text.

For example let's see what are the words used in similar context to the word 'game' in our text:

text.similar('game') #output: song web

text.common_contexts()

The common_contexts() method allows you to examine the contexts that are shared by two or more words. Let's see in which context the words 'game' and 'web' were used in the text:

text.common_contexts(['game', 'web']) #outputs a_of

This means that in the text we'll find 'a game of' and 'a song of'.

These methods are especially interesting when your text is quite large (book, magazine...)

HLeb
  • 591
  • 5
  • 10
1

Observe the below example. You will understand:


>>> text1.concordance("tiger")

of miles you wade knee - deep among Tiger - lilies -- what is the one charm wa
but nurse the cruellest fangs : the tiger of Bengal crouches in spiced groves
e would be more hideous than a caged tiger , then . I could not endure the sigh


>>> text1.concordance("bird")

o the winds when that storm - tossed bird is on the wing . The three correspon
, Ahab seemed not to mark this wild bird ; nor , indeed , would any one else
nd incommoding Tashtego there ; this bird now chanced to intercept its broad f
his hammer frozen there ; and so the bird of heaven , with archangelic shrieks

text1.common_contexts(["tiger","bird"])

the_of

MVS HARISH
  • 11
  • 3