Python 如何使用pandas中的输入语料库/列表从列中提取所有字符串匹配?
例如,我有下面的字符串列表作为输入语料库(实际上是一个包含100个值的大列表)。 动作=[‘跳跃’、‘飞翔’、‘奔跑’、‘游泳’] 数据包含一个名为action\u description的列。如何使用操作列表作为输入语料库提取操作描述中的所有字符串匹配 注意:我已经完成了lemitization description_action,因此如果列中有跳跃或跳跃之类的词,那么它已经转换为跳跃 样本输入和输出 注意:我发现了下面的pandas函数,但是它没有很好的文档记录,所以我不知道如何使用它 请推荐最佳解决方案,因为by input dataframe有200K行 编辑 算法应忽略跳线和跑道等词,即不应归类为跳跃和奔跑。Python 如何使用pandas中的输入语料库/列表从列中提取所有字符串匹配?,python,regex,pandas,nltk,text-mining,Python,Regex,Pandas,Nltk,Text Mining,例如,我有下面的字符串列表作为输入语料库(实际上是一个包含100个值的大列表)。 动作=[‘跳跃’、‘飞翔’、‘奔跑’、‘游泳’] 数据包含一个名为action\u description的列。如何使用操作列表作为输入语料库提取操作描述中的所有字符串匹配 注意:我已经完成了lemitization description_action,因此如果列中有跳跃或跳跃之类的词,那么它已经转换为跳跃 样本输入和输出 注意:我发现了下面的pandas函数,但是它没有很好的文档记录,所以我不知道如何使用它
action=['jump'、'fly'、'run'、'swim']
action=['jump','fly','run','swim']
str1="I love to run and while my friend prefer to swim" ##--> "run swim"
str2="Allan excels at high jump but he is not a good at running" ##--> "jump run"
actionDtl=""
for word in str1.split():
if word in action:
if actionDtl<>"":
actionDtl=actionDtl+" " +word
else:
actionDtl=actionDtl+word
else:
for act in action:
if word.find(act)>=0:
if actionDtl<>"":
actionDtl=actionDtl+" " +act
else:
actionDtl=actionDtl+act
break
print actionDtl
str1=“我喜欢跑步,而我的朋友更喜欢游泳”##-->“跑步游泳”
str2=“艾伦擅长跳高,但他不擅长跑步”##-->“跳跃跑”
actionDtl=“”
对于str1.split()中的单词:
如果文字在起作用:
如果actionDtl“”:
actionDtl=actionDtl+“”+word
其他:
actionDtl=actionDtl+word
其他:
对于实际行动:
如果word.find(act)>=0:
如果actionDtl“”:
actionDtl=actionDtl+“”+act
其他:
actionDtl=actionDtl+act
打破
打印操作DTL
动作=['jump'、'fly'、'run'、'swim']
str1=“我喜欢跑步,而我的朋友更喜欢游泳”##-->“跑步游泳”
str2=“艾伦擅长跳高,但他不擅长跑步”##-->“跳跃跑”
actionDtl=“”
对于str1.split()中的单词:
如果文字在起作用:
如果actionDtl“”:
actionDtl=actionDtl+“”+word
其他:
actionDtl=actionDtl+word
其他:
对于实际行动:
如果word.find(act)>=0:
如果actionDtl“”:
actionDtl=actionDtl+“”+act
其他:
actionDtl=actionDtl+act
打破
打印操作DTL
步骤:
df
# A B ApproxMatch \
#0 1 I love to run and while my friend prefer to... [run, swim]
#1 2 Allan excels at high jump but he is not a good... [jump, run]
#2 3 Ostrich can run very fast but cannot fly [fly, run]
#3 4 The runway was wet hence the Jumper flew over it [run]
#
# ExactMatch
#0 [run, swim]
#1 [jump]
#2 [fly, run]
#3 []
pos='v'
对动词进行柠檬化,并通过对str.split
操作得到的列表中的每个单词进行迭代,使名词保持原样set
,获取查找列表中存在的单词的所有匹配项和柠檬化列表李>
启动
DF
已使用:
df = pd.DataFrame(dict(action_description=["I love to run and while my friend prefer to swim",
"Allan excels at high jump but he is not a good at running"]))
要生成二进制标志(0/1),我们可以通过在空白处拆分字符串并计算其指示符变量来使用该方法,如图所示:
bin_flag = df['action_description'].str.get_dummies(sep=' ').add_suffix('_flag')
pd.concat([df['action_description'], bin_flag], axis=1)
步骤:
df
# A B ApproxMatch \
#0 1 I love to run and while my friend prefer to... [run, swim]
#1 2 Allan excels at high jump but he is not a good... [jump, run]
#2 3 Ostrich can run very fast but cannot fly [fly, run]
#3 4 The runway was wet hence the Jumper flew over it [run]
#
# ExactMatch
#0 [run, swim]
#1 [jump]
#2 [fly, run]
#3 []
pos='v'
对动词进行柠檬化,并通过对str.split
操作得到的列表中的每个单词进行迭代,使名词保持原样set
,获取查找列表中存在的单词的所有匹配项和柠檬化列表李>
启动
DF
已使用:
df = pd.DataFrame(dict(action_description=["I love to run and while my friend prefer to swim",
"Allan excels at high jump but he is not a good at running"]))
要生成二进制标志(0/1),我们可以通过在空白处拆分字符串并计算其指示符变量来使用该方法,如图所示:
bin_flag = df['action_description'].str.get_dummies(sep=' ').add_suffix('_flag')
pd.concat([df['action_description'], bin_flag], axis=1)
使用
re.findall
匹配字符串和运算符时,这确实是一个正则表达式问题。添加
组合匹配项
import pandas as pd
import re
import operator as op
action=['jump','fly','run','swim']
str1="I love to run and while my friend prefer to swim" ##--> "run swim"
str2="Allan excels at high jump but he is not a good at running" ##--> "jump run
df=pd.DataFrame({'A':[1,2,3,4],
'B':['I love to run and while my friend prefer to swim',
'Allan excels at high jump but he is not a good at running',
'Ostrich can run very fast but cannot fly',
'The runway was wet hence the Jumper flew over it'] })
df['ApproxMatch']=df['B'].apply(lambda x: [reduce(op.add, re.findall(act,x)) for act in action if re.findall(act,x) <> []] )
#using r'\b'+jump+r'\b' to match jump exactly, where \b stands for word boundaries
df['ExactMatch']=df['B'].apply(lambda x: [reduce(op.add, re.findall(r"\b"+act+r"\b",x)) for act in action if re.findall(r"\b"+act+r"\b",x) <> []] )
请注意,对于第2行的精确匹配,“running”与“run”不匹配这确实是一个正则表达式问题,使用
re.findall
匹配字符串和运算符。添加组合匹配项
import pandas as pd
import re
import operator as op
action=['jump','fly','run','swim']
str1="I love to run and while my friend prefer to swim" ##--> "run swim"
str2="Allan excels at high jump but he is not a good at running" ##--> "jump run
df=pd.DataFrame({'A':[1,2,3,4],
'B':['I love to run and while my friend prefer to swim',
'Allan excels at high jump but he is not a good at running',
'Ostrich can run very fast but cannot fly',
'The runway was wet hence the Jumper flew over it'] })
df['ApproxMatch']=df['B'].apply(lambda x: [reduce(op.add, re.findall(act,x)) for act in action if re.findall(act,x) <> []] )
#using r'\b'+jump+r'\b' to match jump exactly, where \b stands for word boundaries
df['ExactMatch']=df['B'].apply(lambda x: [reduce(op.add, re.findall(r"\b"+act+r"\b",x)) for act in action if re.findall(r"\b"+act+r"\b",x) <> []] )
请注意,对于第2行的精确匹配,“running”与“run”不匹配。谢谢!如果描述中包含诸如“跑道”和“跳线”之类的词会怎样?添加了精确匹配,是否符合您的期望。你能用“跑道”和“跳线”的输入和输出期望更新你的原始帖子吗?谢谢分享。一个建议是执行lemmitization,这样飞行和跑步可以像@Nickil Maveli那样正确分类。谢谢!如果描述中包含诸如“跑道”和“跳线”之类的词会怎样?添加了精确匹配,是否符合您的期望。你能用“跑道”和“跳线”的输入和输出期望更新你的原始帖子吗?谢谢分享。一个建议是执行lemmitization,这样飞行和跑步可以像@Nickil Maveli那样正确分类。为什么使用pos='v'即动词?如果识别出句子上的第一个词性标记,然后将词性标记传递给lemmatize()函数,结果会有所改善吗?Lemmatiser生成真实的单词,但没有上下文信息,它无法区分名词和动词。上下文由POS标记提供(因为所有单词都是动词形式,所以使用POS='v')。是的,如果在你的查找列表中有动词以外的单词,如果你能自动生成词性标签并将词性语法化,结果肯定会有所改善。谢谢!我怎样才能修改你的代码,用lemmitization将“leak”等词转换成“leak”等词呢?恐怕你不能,因为这是一个纯文本。如果它是“泄漏”或类似的东西,它就会起作用。顺便说一句,您可以尝试先阻止这种情况。例如,某些词干分析器会为您提供不同的词根词,这些词根词是从中派生出来的。例如:在这种情况下,可以使用Lancaster词干分析器-lanc=Lancaster词干分析器()lanc.stem('leaks')
它将泄漏作为带词干的输出。Hi-Nickil,对于操作列表中的每个匹配值,我要创建二进制标志。例如:“操作描述”中存在匹配的跳转\u flg=1,否则跳转\u flg=1。类似于