1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Replace words in a sentence with synonyms using Python

Discussão em 'Python' iniciado por Stack, Setembro 21, 2021.

  1. Stack

    Stack Membro Participativo

    I have a dataset named news_collection.csv where that has news and what I was struggling to do is that to replace words of the data set with the synonyms from pre built collection called syno.txt . If a word in the data set has a synonyms from syno.txt I want to replace with the first value of that particular synonym line.

    Below is the news_collection.csv


    created_at,text
    5/13/2021 3:27:55 PM,"my mom went with her mommy to bring the food for us"
    5/13/2021 3:27:55 PM,"that is my dad and haven't your dada talk to my father"

    Below is the syno.txt


    mother, mommy, mom, ma
    father, dad, daddy, dada

    Below is the expected result


    created_at,text
    5/13/2021 3:27:55 PM,"my mother went with her mother to bring the food for us"
    5/13/2021 3:27:55 PM,"that is my father and haven't your father talk to my father"

    Below is what I have tried upto now

    import pandas as pd
    import re
    from nltk.tokenize import word_tokenize


    def similarity():
    tweets = pd.read_csv(r'news_collection.csv')
    df = pd.DataFrame(tweets, columns=['created_at', 'text'])
    df['created_at'] = pd.to_datetime(df['created_at'])
    df['text'] = df['text'].apply(lambda x: str(x))
    df["text"] = df["text"].apply(lambda x: replacesynonyms(x))

    return df

    def replacesynonyms(text):
    file = open('sino.txt', 'r', encoding="utf8")
    //code to be added


    Can someone help to solve this algorithm?

    Continue reading...

Compartilhe esta Página