Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:05:16+00:00 2026-05-29T09:05:16+00:00

I am trying to find collocations with NLTK in a text by using the

  • 0

I am trying to find collocations with NLTK in a text by using the built-in method.

Now I am having the following example text (test and foo follow each other, but there is a sentence border in between):

content_part = """test. foo 0 test. foo 1 test. 
               foo 2 test. foo 3 test. foo 4 test. foo 5"""

Result from tokenization and collocations() is as follows:

print nltk.word_tokenize(content_part)
# ['test.', 'foo', 'my', 'test.', 'foo', '1', 'test.',
# 'foo', '2', 'test.', 'foo', '3', 'test.', 'foo', '4', 'test.', 'foo', '5']

print nltk.Text(nltk.word_tokenize(content_part)).collocations()
# test. foo

How can I prevent NLTK from:

  1. Including the dot in my tokenization
  2. Not find collocations() over sentence borders?

So in this example it should not print any collocation at all, but I guess you can imagine more complicated texts where there are also collocations within sentences.

I can guess that I need to use the Punkt sentence segmenter, but then I do not know how to put them together again to find collocations with nltk (collocation() seems to be more mighty than just counting stuff myself).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:05:17+00:00Added an answer on May 29, 2026 at 9:05 am

    You could use WordPunctTokenizer to separate the punctuation from words and later filter out the bigrams with punctuation with apply_word_filter().

    Same thing may be used for trigrams for not finding collocations over sentence borders.

    from nltk import bigrams
    from nltk import collocations
    from nltk import FreqDist
    from nltk.collocations import *
    from nltk import WordPunctTokenizer
    
    content_part = """test. foo 0 test. foo 1 test. 
                   foo 2 test. foo 3 test. foo 4 test, foo 4 test."""
    
    tokens = WordPunctTokenizer().tokenize(content_part)
    
    bigram_measures = collocations.BigramAssocMeasures()
    word_fd = FreqDist(tokens)
    bigram_fd = FreqDist(bigrams(tokens))
    finder = BigramCollocationFinder(word_fd, bigram_fd)
    
    finder.apply_word_filter(lambda w: w in ('.', ','))
    
    scored = finder.score_ngrams(bigram_measures.raw_freq)
    
    print tokens
    print sorted(finder.nbest(bigram_measures.raw_freq,2),reverse=True)
    

    Output:

    ['test', '.', 'foo', '0', 'test', '.', 'foo', '1', 'test', '.', 'foo', '2', 'test', '.', 'foo', '3', 'test', '.', 'foo', '4', 'test', ',', 'foo', '4', 'test', '.']
    [('4', 'test'), ('foo', '4')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find an example that has css rollover using sprites & sliding door
I am trying to create a generic method using EF4 to find the primary
I am trying to find collocations (PDF) in Java. I know NLTK has a
Trying to find a good name for a method swapping each coordinate X and
I'm trying to start using LINQ and specifically LINQ to SQL but I'm having
I am trying to use the Except method on a LINQ result set using
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to find the sqlserver adapter for rails on windows. I have tried getting
Trying to find an XML file I can use in lieu of a look-up
Trying to find the best way of create an overlap/overlay layer to fill the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.