[英]Using get_word_forms, how do I generate all possible forms of a list of words?
[英]How do I get python to search text for one word in a list rather than all the words in a list?
我想創建一個同義詞列表,並用python搜索共址,但我不需要列表中的每個單詞都可以顯示。 我只需要列出每個列表中的一個詞即可。
我在csv文件中有一個在線論壇的對話主題。 我一直在尋找在這些線程中共同定位的單詞。 例如,如果在同一篇文章中出現單詞“ fraud”和“ flag”(請參見下面的代碼),那么我可以抓住這些文章。
def searchWord(word, string):
if word in string:
return 1
else:
return 0
df['flag'] = df['Post_Text_lowercase'].apply(lambda x: 1 if 'flag' in x else 0)
df['flag'] = df['Post_Text_lowercase'].apply(lambda x: searchWord('flag', x))
df['flag'].value_counts()
df['fraud'] = df['Post_Text_lowercase'].apply(lambda x: 1 if 'fraud' in x else 0)
df['flag_fraud'] = df['flag'] & df['fraud']
df_flag_fraud = df[ df['flag_fraud'] == 1 ].copy()
df_flag_fraud['Post_Text'].values
但是,現在我要搜索說“'欺詐','騙子','假','用戶']和['標志'],但是只需要顯示第一個列表中的一個單詞即可,而不是全部其中。 我該怎么做呢? (請介意,我意識到我可以阻止或挫敗,但並不能完全抓住我想要的東西)。 因為我是新手,所以我意識到我的代碼可能不是這里的最佳方法。
謝謝大家。 我從你身上學到了很多
因此,您想提供一個或多個單詞,並返回包含您提供的單詞或單詞同義詞的所有帖子。
似乎發現單詞的同義詞很棘手。 有些人建議使用WordNet 。 我假設您需要考慮的同義詞更加有限,並且您對給定組中具有相同關系和權重的同義詞的所有重要性都沒事。
然后,您可以使用圖形數據結構對其進行處理,並為具有相同含義的每組單詞開發一個圖形。 我正在使用下面這篇文章中找到的Graph
的實現–可能有點過頭了,但可以幫助您快速理解這一點。
# Produce a set of synonyms for each idea you're interested in searching for
synonyms = ["scam", "fraud", "sham", "swindle", "hustle", "racket"]
# Create a complete graph which connects each word in
# a group of synonyms to every other with equal weight
synonyms = [(x, y) for x in synonyms for y in synonyms]
print synonyms
'''
[('scam', 'scam'),
('scam', 'fraud'),
('scam', 'sham'),
('scam', 'swindle'),
('scam', 'hustle'),
('scam', 'racket'),
('fraud', 'scam'),
('fraud', 'fraud'),
('fraud', 'sham'),
('fraud', 'swindle'),
('fraud', 'hustle'),
('fraud', 'racket'),
('sham', 'scam'),
('sham', 'fraud'),
('sham', 'sham'),
('sham', 'swindle'),
('sham', 'hustle'),
('sham', 'racket'),
('swindle', 'scam'),
('swindle', 'fraud'),
('swindle', 'sham'),
('swindle', 'swindle'),
('swindle', 'hustle'),
('swindle', 'racket'),
('hustle', 'scam'),
('hustle', 'fraud'),
('hustle', 'sham'),
('hustle', 'swindle'),
('hustle', 'hustle'),
('hustle', 'racket'),
('racket', 'scam'),
('racket', 'fraud'),
('racket', 'sham'),
('racket', 'swindle'),
('racket', 'hustle'),
('racket', 'racket')]
'''
synonyms_graph = Graph(synonyms)
print synonyms_graph
'''
Graph({'fraud': set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle']), 'scam': set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle']), 'racket': set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle']), 'swindle': set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle']), 'hustle': set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle']), 'sham': set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle'])})
'''
# Add another network of synonyms for a different topic
synonyms = ["flag", "problem", "issue", "worry", "concern"]
synonyms_graph.add_connections([(x, y) for x in synonyms for y in synonyms])
# Now you can provide any word in a synonym group and get all the other synonyms
print synonyms_graph._graph["scam"]
'''
set(['fraud', 'scam', 'sham', 'racket', 'hustle', 'swindle'])
'''
print synonyms_graph._graph["flag"]
'''
set(['flag', 'issue', 'problem', 'worry', 'concern'])
'''
# Apply this to your dataframe
df["Post_Word_Set"] = df["Post_Text_lowercase"].apply(lambda x: set(x.split()))
df["scam"] = df.apply(lambda x: 1 if x.Post_Word_Set.intersection(synonyms_graph._graph["scam"]) else 0, axis=1)
df["flag"] = df.apply(lambda x: 1 if x.Post_Word_Set.intersection(synonyms_graph._graph["flag"]) else 0, axis=1)
posts_of_interest = df[(df.scam.values == 1) | (df.flag.values == 1)].Post_Text_lowercase
print posts_of_interest
'''
This makes me worry. Maybe it's a scam
Big red flag here
So sick of all this fraud
Name: Post_Text_lowercase, dtype: object
'''
肯定有潛力在這里進行優化。 您提到了詞干和詞根化,這可能是一個好主意。 我還考慮刪除標點符號,以免您錯過“這是一個騙局”之類的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.