我有两个csv文件,dictionary.csv和file.csv,我想检查dictionary.csv中的单词是否存在于file.csv中。dictionary.csv中的一些行包含超过2个单词,我想知道是否有方法可以做到这一点,
如果行中有3个单词,并且行中至少有2/3个单词与file.csv匹配,则返回1,否则返回0
如果行中有2个单词,并且行中至少有1/2个单词与file.csv匹配,则返回1,否则返回0
下面是到目前为止我的代码,它是完全匹配的
file=pd.read_csv("file.csv")
dictionary=pd.read_csv("dictionary.csv")
pattern='|'.join(dictionary)
news["contain diseases1"] = np.where(
news["STORY"].str.contains(pattern, na=False),
1, 0
)
news.to_csv("clues.csv")
为了进一步帮助您理解我的问题,下面是dictionary.csv和file.csv的内容
dictionary.csv
sigmoid colon cancer
site specific early onset breast cancer syndrome
skin cancer
file.csv
id STORY
0 Ari have a colon cancer
1 Cancer is an epidemic
2 Breast cancer can happen to both genders
我应该从这些文件中获得输出是
clue.csv
id STORY contain diseases1
0 Ari have a colon cancer 1
1 Cancer is an epidemic 1
2 Breast cancer can happen to both genders 1
3 Prioritizing the health of skin 0
4 A specific camping site is only for early birds 0
到目前为止,因为我现在拥有的代码是完全匹配的,所以我一直得到0
发布于 2019-10-12 01:02:17
你有没有考虑过fuzzywuzzy库?这是一个由SeatGeek开源的字符串匹配库。它提供了一个基于不完全匹配的匹配分数,然后您可以决定哪个阈值最接近-足够匹配。
根据我的经验,我曾使用它来匹配来自不同数据源的医生姓名(例如,有些人说"Dr.“一些人说"M.D.",一些名字是缩写的,一些姓是因为婚前的名字而改变的)。
这里有两个库的链接。
https://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/
https://stackoverflow.com/questions/58345116
复制相似问题