【发布时间】:2018-08-24 01:17:27
【问题描述】:
场景是,我将推文分为 6 种不同的极性,即。正面、中度正面、高度正面、负面、中度负面和高度负面。
因为,这个过程经历了 NLP 的步骤(使用 NLTK)我需要逐个推文/推文。
问题:
这些极性由 POS 标记后的模式定义。其中一种模式包括:动词+动词+形容词包括在D(干旱相关术语)和F(常用词)中
我需要那些常用词,将这句话更改为这 6 种极性中的任何一种,以保存到我的数据框中。
片段:
这是我尝试过的
for (w1, tag1), (w2, tag2), (w3, tag3) in nltk.trigrams(PoS_TAGS):
if tag1.startswith("RB") and tag2.startswith("RB") and tag3.startswith("JJ"):
tri_pairs.append((w1, w2, w3))
if tri_pairs[0] or tri_pairs[1] or tri_pairs[2] in D:
print("[True]: Tri Pairs are found in Drought Rel. Term")
for j in range(len(F)):
if tri_pairs[0] or tri_pairs[1] or tri_pairs[2] in F[j]:
print("[True]: Tri Pairs are found in Frequent Wordset")
if RES is "Positive":
RES = "Highly Positive"
elif RES is "Negative":
RES = "Highly Negative"
print "="*25,F[j]
FW_list.append(F[j])
else:
print"[False]: Doesn't Match with Frequent Wordset\n"
else:
print"[False]: Tri Pairs Matched Nowhere in D\n"
else:
print "[TriPair(F)]: Pattern for Adverb, Adverb, Adjective did not match.\n Looking for Bi-Pair Patterns\n"
print(tri_pairs)
print(">"*13,FW)
如您所见,我尝试以大多数方式使用 List 甚至内部循环进行打印。两者都没有返回任何有用的东西。同样,其他两种模式决定了被忽略的极性。
我还编写了代码将其添加到数据框中:
fuzzy_df = fuzzy_df.append({'Tweets': tweets[i], 'Classified': RES, 'FreqWord': FW}, ignore_index=True)
但到目前为止,该列的 csv 为空白。
编辑 1:
我已经可以使用常用词。它们如下:
>>> F
['drought', 'water', 'love', 'rain', 'year', 'famine', 'farmers', 'crops', 'south', 'http', 'europe', 'scarcity', 'near', 'thought', 'ever', 'devastates', 'feed', 'message', 'eduaubdedubu', 'instant', 'italy', 'severe', 'by', 'beaches', 'wildfires', 'heat', 'us']
编辑 2
CSV 看起来像这样:
Tweets,Classified,FreqWord
real time strategy password wastelands depletion groundwater skyrocketing debts make years anantapur drought worse,Negative,
calm director day science meetings nasal talk cutting edge remote sensing research drought veg fluorescence calm love,Positive,
love thought drought,Positive,
neville rooney end ever tons trophy drought,Positive,
lakes drought,Positive,
lakes fan joint trailblazers dot forget play drought,Positive,
reign mother kerr funny none tried make come back drought,Positive,
wonder could help thai market b post reuters drought devastates south europe crops,Negative,
输入文件:
tweets,polarity
real time strategy password wastelands depletion groundwater skyrocketing debts make years anantapur drought worse,Positive
calm director day science meetings nasal talk cutting edge remote sensing research drought veg fluorescence calm love,Positive
hate thought drought,Negative
虽然,我上面显示的输出是标记化的,并且停用词也被删除了。
预期输出文件:
Tweets,Classified,FreqWord
real time strategy password wastelands depletion groundwater skyrocketing debts make years anantapur drought worse,Negative,water
calm director day science meetings nasal talk cutting edge remote sensing research drought veg fluorescence calm love,Positive,drought
love thought drought,Positive,drought
neville rooney end ever tons trophy drought,Positive,rain
lakes drought,Positive,drought
lakes fan joint trailblazers dot forget play drought,Positive,farmer
reign mother kerr funny none tried make come back drought,Positive,crops
wonder could help thai market b post reuters drought devastates south europe crops,Negative,crops
编辑 3
FW = ''
for i in range(len(tweets)):
sent = nltk.word_tokenize(tweets[i])
PoS_TAGS = nltk.pos_tag(sent)
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
one_sentence = tweets.iloc[i]
scores = sia.polarity_scores(text=one_sentence)
print "POS:", scores.get('pos')
print "NEG:", scores.get('neg')
print "NEU:", scores.get('neu')
POS = scores.get('pos')
NEG = scores.get('neg')
NEU = scores.get('neu')
RES = str()
if POS > NEG:
RES = 'Positive'
elif NEG > POS:
RES = 'Negative'
elif NEU >= 0.5 or POS > NEU:
RES = 'Positive'
elif NEU < 0.5:
RES = 'Negative'
# -------------------------------------------------------- PATTERN ADVERB, ADVERB, ADJECTIVE (Down)
tri_pairs = list()
for (w1, tag1), (w2, tag2), (w3, tag3) in nltk.trigrams(PoS_TAGS):
if tag1.startswith("RB") and tag2.startswith("RB") and tag3.startswith("JJ"):
tri_pairs.append((w1, w2, w3))
if tri_pairs[0] or tri_pairs[1] or tri_pairs[2] in D:
print("[True]: Tri Pairs are found in Drought Rel. Term")
# TRIGGER AREA
for j in range(len(F)):
if tri_pairs[0] or tri_pairs[1] or tri_pairs[2] in F[j]:
print("[True]: Tri Pairs are found in Frequent Wordset")
if RES is "Positive":
RES = "Highly Positive"
FW = F[j]
#fuzzy_df['FreqWord'].map(lambda x: next((y for y in x.split() if y in F), 'Not Found'))
elif RES is "Negative":
RES = "Highly Negative"
FW = F[j]
else:
print"[False]: Doesn't Match with Frequent Wordset\n"
else:
print"[False]: Tri Pairs Matched Nowhere in D\n"
else:
print "[TriPair(F)]: Pattern for Adverb, Adverb, Adjective did not match.\n Looking for Bi-Pair Patterns\n"
print(tri_pairs)
# -------------------------------------------------------- PATTERN ADVERB, ADJECTIVE (Down)
bi_pairs = list()
for (w1, tag1), (w2, tag2) in nltk.bigrams(PoS_TAGS):
if tag1.startswith("RB") and tag2.startswith("JJ"):
bi_pairs.append((w1, w2))
if bi_pairs[0] or bi_pairs[1] in D:
print("[True]: Bi Pairs are found in Drought Rel. Term")
for k in range(len(F)):
if bi_pairs[0] or bi_pairs[1] is F[k]:
print("[True]: Bi Pairs are found in Frequent Wordset")
if RES is "Positive":
RES = "Moderately Positive"
FW = F[k]
elif RES is "Negative":
RES = "Moderately Negative"
FW = F[k]
else:
print("[False]: Bi Pairs found missing in Freq. Wordset")
else:
print("[False]: Bi Pairs Matched Nowhere in D")
else:
print("[BiPair(F)]: Pattern Not Matched, Looking for Mono Pattern")
print(bi_pairs)
# -------------------------------------------------------- PATTERN ADJECTIVE (Down)
for w, tag in PoS_TAGS:
print w, " - ", tag
if tag.startswith("JJ"):
if w in D:
print("Matched with D")
for l in range(len(F)):
if w is F[l]:
print("Matched with F")
if RES is "Positive":
RES = "Positive"
FW = F[l]
elif RES is "Negative":
RES = "Negative"
FW = F[l]
else:
print("Unmatched in F")
FW = F[l] in sent
else:
print("Unmatched in D")
else:
print w, "is not an ADJECTIVE"
# -------------------------------------------------------- MAKING ENTRY OF RECORDS OF TWEETS and POLARITY RESULT
fuzzy_df = fuzzy_df.append({'Tweets': tweets[i], 'Classified': RES, 'FreqWord': FW}, ignore_index=True)
# ADDING RECORDS IN DATAFRAME
fuzzy_df.to_csv("fuzzy.csv", index=False)
【问题讨论】:
-
我不确定我是否理解你的意思。但是要得到频繁的单词,你不能只做 nltk.tokenize,得到每个单词的计数,按降序排序,得到最频繁的单词吗?
-
我已经将常用词存储在一个列表中。我将使用列表 F 值更新问题@ManishSaraswat
-
您有示例 csv 文件吗?
-
@alvas 当然,请检查编辑 2
-
另外,您是否有一个示例输入文件和您希望获得的所需输出?
标签: python python-2.7 pandas csv nltk