首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中部分字符串比较需要字符串比较的解决方案

在Python中部分字符串比较需要字符串比较的解决方案
EN

Stack Overflow用户
提问于 2014-12-26 05:53:56
回答 2查看 616关注 0票数 1

场景:I为各自的“区段标题”(存储为字符串)执行了一些任务,该任务的结果必须保存到相同的“现有区段标题”(存储为字符串)中

当映射各个任务的“区段标题”是“现有区段标题”之一时,任务结果将被添加到其中。如果不是,新的节标题将被附加到现有的节标题列表中。

现有的节头如下所示:

“活动(最后3天)”、“活动(最后7天)”、“从磁盘上运行的可执行”、“文件中的操作”

对于以下一组字符串,预期行为如下:

“活动(最近30天)-应增加新的部分

“从磁盘运行的可执行文件”

“文件中的操作”--考虑到额外的"a“条,应该引用现有的”文件操作“。

是否有任何内置函数可用的python可以帮助合并相同的逻辑。或任何关于算法的建议都是非常感谢的。

EN

Stack Overflow用户

回答已采纳

发布于 2014-12-26 07:18:45

由于您只想比较给定单词的词干或“根词”,我建议使用一些词干算法。词干算法试图自动删除后缀(在某些情况下是前缀),以便找到给定单词的“根词”或词干。这在各种自然语言处理场景(如搜索)中非常有用。幸运的是,有一个用于stemming的python包。您可以从这里下载它。

接下来,您要比较没有停止的字符串(a、an、the、from等)。因此,在比较字符串之前,您需要过滤这些单词。您可以从互联网上获取停止词列表,也可以使用nltk包导入停止词列表。你可以从nltk那里得到这里

如果nltk有任何问题,下面是停止词列表:

代码语言:javascript
运行
复制
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours',
 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself',
 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which',
 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be',
 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an',
 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for',
 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after',
 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under',
 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all',
 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not',
 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don',
 'should', 'now']

现在,使用以下简单代码获得所需的输出:

代码语言:javascript
运行
复制
from stemming.porter2 import stem
from nltk.corpus import stopwords
stopwords_ =  stopwords.words('english')
def addString(x):
   flag = True
   y = [stem(j).lower() for j in x.split() if j.lower() not in stopwords_]
   for i in section:
      i = [stem(j).lower() for j in i.split() if j.lower() not in stopwords_]
      if y==i:
         flag = False
         break
   if flag:
      section.append(x)
      print "\tNew Section Added"

演示:

代码语言:javascript
运行
复制
>>> from stemming.porter2 import stem
>>> from nltk.corpus import stopwords
>>> stopwords_ =  stopwords.words('english')
>>> 
>>> def addString(x):
...    flag = True
...    y = [stem(j).lower() for j in x.split() if j.lower() not in stopwords_]
...    for i in section:
...       i = [stem(j).lower() for j in i.split() if j.lower() not in stopwords_]
...       if y==i:
...          flag = False
...          break
...    if flag:
...       section.append(x)
...       print "\tNew Section Added"
... 
>>> section = [ "Activity (Last 3 Days)", "Activity (Last 7 days)", "Executable running from disk", "Actions from File"]  # initial Section list
>>> addString("Activity (Last 30 Days)")
    New Section Added
>>> addString("Executables running from disk")
>>> addString("Actions from a file")
>>> section
['Activity (Last 3 Days)', 'Activity (Last 7 days)', 'Executable running from disk', 'Actions from File', 'Activity (Last 30 Days)']  # Final section list
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27653300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档