[英]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.