代码之家  ›  专栏  ›  技术社区  ›  Gijo george

如何识别段落中每个句子的情绪

  •  -1
  • Gijo george  · 技术社区  · 3 年前
    import pandas as pd
    import numpy as np
    df = pd.read_csv("./email1.csv")
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.model_selection import train_test_split
    from sklearn.svm import LinearSVC
    from sklearn.metrics import classification_report
    X = df['emails']
    
    tfids = TfidfVectorizer(max_features=10000,ngram_range=(1,2))
    X = tfids.fit_transform(X)
    y = df['sentiment']
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)
    clf = LinearSVC()
    clf.fit(X_train,y_train)
    y_pred = clf.predict(X_test)
    print(classification_report(y_test,y_pred))
    
    x = """The patient presents to clinic for initial evaluation of some pain and swelling to the left foot. On the day of injury a metal bar fell directly on the top of his left foot striking above the steel toed area of his boot. He had some pain, swelling and bruising early on but this has gotten better. However, the patient continues to have some soreness on the ball of his foot and points to the seconcbdista: metatarsal region. He states that it feels a little like a stone bruise. He has continued hs regglar duties without problems and is able to wear a regular shoe."""
    vec = tfids.transform([x])
    a = clf.predict(vec)
    if a==0:
        print("Negative communication")
    else:
        print("Positive communication")
    

    有什么方法可以识别每个句子的情绪吗。例如:“他在早期有一些疼痛、肿胀和瘀伤,但现在已经好转了”,这是一个积极的句子。

    1 回复  |  直到 3 年前
        1
  •  1
  •   Selcuk    3 年前

    如果我理解正确,你所需要的就是把这段分成点,例如:

    for sentence in x.split("."):
        vec = tfids.transform([sentence])
        a = clf.predict(vec)
        print(f'Sentiment analysis for "{sentence}":')
        if a==0:
            print("Negative communication")
        else:
            print("Positive communication")