Zählung der Kombination von Bigrams
Posted: 02 Mar 2025, 14:40
Ich habe einen Datensatz erstellt wie folgt mit Bigrams < /p>
Code: Select all
index | product_action
-------------------------------------------------------|
('customer', 'called') | action
('customer', 'service') | action
('blue', 'dress') | product
('the', 'service') | product
('to', 'complain') | action
('complain', 'about') | action
('service', 'received') | action
('the', 'dress') | product
< /code>
Ich möchte wissen, ob in jedem Satz, wie oft die Kombination für den gesamten Datensatz aufgetreten istdef get_bigrams(text):
tokens = nltk.word_tokenize(text.lower())
return list(ngrams(tokens, 2))
def count_bigrams(text, bigram):
bigrams = get_bigrams(text)
return bigrams.count(bigram)
< /code>
Der Datensatz, den ich im Sinn habe, lautet wie folgt: < /p>
product | action | count
---------------------------------------------------------|
('blue', 'dress') | ('customer', 'called') | 10