Best approach corpus pre-processing with single tokens and bigram tokens?

I’m wondering if there is general advice for the smartest way to approach this problem.

I am using word2vec to determine similarity scores (that is the final output I am interested in) between specific words – some of these are single tokens but others should be bigrams. To complicate matters, I am using tensorflow (in order to learn how to work with tensorflow).

I want to keep bigrams that are found in a separate list:

Bigram_list = ["northern lights", "cloud cover", "table leg",...]

At the moment, the process should look something like this:

  1. Identify bigrams in corpus (using nltk collocations)
  2. create identified_bigrams_list = ["northern lights", "cloud cover", "banana peel",...]
  3. search through identified_bigrams_list for matches in Bigram_list
  4. PROBLEM: replace matches in the corpus with “_”, so for example “northern_light”, “cloud_cover”. I’ve tried with a dictionary of the Bigram_list (e.g. "northern lights": "northern_lights"). So I am trying to put it back into the corpus so it will be considered a single token and processed as a single embedding

Even if I can get this to work, this seems computationally inefficient, especially once I move on to a larger corpus for the actual training (currently using a tiny corpus in order to get this to work).

Any advice?

Here’s a streamlined approach to handle your task more efficiently while using TensorFlow:

  1. Identify Bigrams in Corpus: Use NLTK or another library to identify
    bigrams in your corpus.
  2. Replace Bigrams in Corpus: Replace identified bigrams with a single
    token format (e.g., “northern_lights”).
  3. Train Word2Vec: Use the modified corpus to train your Word2Vec
    model.
  4. Calculate Similarity Scores: Calculate similarity scores between
    specific words or bigrams.

Implementation:
Use NLTK to identify bigrams in your corpus:

import nltk
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures

def identify_bigrams(corpus):
    tokens = nltk.word_tokenize(corpus)
    finder = BigramCollocationFinder.from_words(tokens)
    bigrams = finder.nbest(BigramAssocMeasures.likelihood_ratio, 50)
    return [' '.join(bigram) for bigram in bigrams]

Next, you need to replace the identified bigrams with a single token format:

def replace_bigrams(corpus, bigram_list):
    tokens = nltk.word_tokenize(corpus)
    bigram_dict = {bigram: bigram.replace(' ', '_') for bigram in bigram_list}
    new_tokens = []
    skip_next = False
    
    for i in range(len(tokens) - 1):
        if skip_next:
            skip_next = False
            continue
        bigram = tokens[i] + ' ' + tokens[i + 1]
        if bigram in bigram_dict:
            new_tokens.append(bigram_dict[bigram])
            skip_next = True
        else:
            new_tokens.append(tokens[i])
    
    if not skip_next:
        new_tokens.append(tokens[-1])
    
    return ' '.join(new_tokens)

corpus = "The northern lights are amazing. I love the table leg design."
bigram_list = ["northern lights", "table leg"]
new_corpus = replace_bigrams(corpus, bigram_list)
print(new_corpus)  # Output: "The northern_lights are amazing. I love the table_leg design."

Then you train the Word2Vec Model

New contributor

Ali Rammal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật