首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从python中的句子中提取主要主题

从python中的句子中提取主要主题
EN

Stack Overflow用户
提问于 2018-06-18 10:47:55
回答 1查看 852关注 0票数 1

我试图从包含在文本文件中的句子中提取主要主题。例如,文件包含如下所示的数据

I never used tobacco
They smoke tobacco
I do not like today's weather
Good weather
Exercise 3 to 4 times a week
No exercise
Family history of Cancer
No Cancer
,,· Alcohol use
Amazing football match
Pathetic football match
Has Depression

我必须提取主要主题,并按如下方式打印:

I never used tobacco | Tobacco | False
They smoke tobacco | Tobacco | True
I do not like today's weather | Weather | False
Good weather | Weather | True
Exercise 3 to 4 times a week | Exercise | True
No exercise | Exercise | False
Family history of Cancer | Cancer | True
No Cancer | Cancer | False
,,· Alcohol use. | Alcohol | True
Amazing football match | Football Match| True
Pathetic football match | Football Match | False
Has Depression | Depression | True

我正在尝试Spacy,但无法获得所需的输出。我使用Spacy对句子进行了标记化,然后使用词性标注来提取名词,但仍然没有得到所需的内容。有没有人可以帮助我们如何做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-03 07:05:04

没有确切的解决方案,但我使用的以下代码有一定的帮助:

negatedwords = read_words_from_file('false.txt') # file containing all the negation words
#read_words_from_file() will read words from file

from collections import Counter
import spacy
nlp = spacy.load('en_core_web_md')

count = Counter(line.split())
negated_word_found = False
for key, val in count.items():
    key = key.rstrip('.,?!\n') # removing punctuations
    if key in negatedwords :
        negated_word_found= True

if negated_word_found== True:
    file_write.write("False")
else:
    file_write.write("True")

file_write.write(" | ")
document = nlp(line)

for word in document:
    look_for_word = word.text
    word_pos = word.pos_
    if ((word_pos =="NOUN" or word_pos =="ADJ" or word_pos == "PROPN" ) and look_for_word!="use" ): #The pos_ tag for 'use' is showed as NOUN  
        file_write.write(look_for_word)
        file_write.write(' ')

false.txt
never
Never
no
No
NO
not
NOT
Not
NEVER
don't
Don't
DON'T
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50902129

复制
相关文章

相似问题

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