首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从纯文本中捕获字典关键字?

如何从纯文本中捕获字典关键字?
EN

Stack Overflow用户
提问于 2017-05-19 09:31:25
回答 2查看 117关注 0票数 2

本研究课题来源于Text processing and detection from a specific dictionary in python课题。也许我误解了OP的问题,但我一直在努力改进代码。所以,也许我的问题可以有点不同。在解释我想要做的事情之前,让我先与您分享代码:

代码语言:javascript
复制
dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"}
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']
for i in dict_1:
    if i.lower() in " ".join(list_1).lower():
        print("Key: {}\nValue: {}\n".format(i,dict_1[i]))

这些代码可以从用list_1编写的纯文本中捕获字典关键字。然而,当我学习这些代码时,我想知道如果一些字典中的关键字在list_1中重复会怎么样。然后我在这个list_1中写了两次相同的密钥。并且上面的代码不能识别重复的代码,程序给出的结果如下所示。

代码语言:javascript
复制
Key: cfDNA
Value: Blood for analysis

Key: Liquid Biopsy
Value: Blood for analysis


Process finished with exit code 0

然后,我尝试更改我的方法,并编写了不同的代码,如下所示:

代码语言:javascript
复制
dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"}
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', "cfdna",u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']
for i in list_1:
    for j in dict_1:
        for k in j.split():
            count=0
            if k.lower() in i.lower():
                count+=1
                print("Key: {}\nValue: {}\nCount: {}\nDescription: Came from '{}'\n".format(j, dict_1[j],str(count),i))

但很明显,最后的代码会给出不想要的结果。如下图所示,该程序从list_1中捕获liquidbiopsy字。cfDNA是在list_1中第二次编写的,所以程序捕获了两次。但是,有没有可能只写一次结果,然后汇总捕获时间呢?

代码语言:javascript
复制
Key: Liquid Biopsy
Value: Blood for analysis
Count: 1
Description: Came from 'Liquid'

Key: Liquid Biopsy
Value: Blood for analysis
Count: 1
Description: Came from 'biopsy'

Key: cfDNA
Value: Blood for analysis
Count: 1
Description: Came from 'cfdna'

Key: cfDNA
Value: Blood for analysis
Count: 1
Description: Came from '(cfDNA)'


Process finished with exit code 0

我希望你能理解我想做什么。我想抓住所有写在文本中的关键字。我还想计算一下,这些键在一篇文章中重复了多少次。

EN

回答 2

Stack Overflow用户

发布于 2017-05-19 09:52:52

如果我没理解错的话,你想知道“关键字”在文本中出现的次数。您可以使用"re“模块来实现此目的。

代码语言:javascript
复制
import re

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis", "asfdafaf":"dunno"}
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', "cfdna",u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']

text = ' '.join(list_1).lower()

for key in dict_1:
    n = len(re.findall(key.lower(), text))
    if n > 0:
        print('Key:', key)
        print('Value:', dict_1[key])
        print('n:', n)
        print()
票数 3
EN

Stack Overflow用户

发布于 2017-05-19 21:39:52

最近,我学到了一种新的方法,可以在不导入"re“模块的情况下计算字典键在纯文本中重复多少次。也许在这个主题中加入另一种方法是合适的。

代码语言:javascript
复制
dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"}
list_1=[u'Liquid', u'biopsy', u'liquid', u'biopsy',u'based',u'cfdna' ,u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']
string_1=" ".join(list_1).lower()
for i in dict_1:
    if i.lower() in string_1:
        print("Key: {}\nValue: {}\nCount: {}\n".format(i,dict_1[i],string_1.count(i.lower())))

上面的代码与导入re模块的方法给出的结果几乎相同。不同的是,它不会写两次密钥。因此,它有点类似于第一篇文章中编写的第一个代码结构。

代码语言:javascript
复制
Key: Liquid Biopsy
Value: Blood for analysis
Count: 2

Key: cfDNA
Value: Blood for analysis
Count: 2


Process finished with exit code 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44060080

复制
相关文章

相似问题

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