How to extract quotations from text using NLTK [duplicate]

怎甘沉沦 提交于 2019-12-14 03:29:31

问题


I have a project wherein I need to extract quotations from a huge set of articles . Here , by quotations I mean things said by people , for eg: Alen said " text to be extracted ." I'm using NLTK for my other NLP related tasks so any solution using NLTK or any kind of Python library would be quite useful.

Thanks


回答1:


As Mayur mentioned, you can do a regex to pick up everything between quotes

list = re.findall("\".*?\"", string)

The problem you'll run into is that there can be a surprisingly large amount of things between quotation marks that are actually not quotations.

If you're doing academic articles, you can look for a number after the closing quotation to pick up the footnote number. Else with non academic articles, perhaps you could run something like:

"(said|writes|argues|concludes)(,)? \".?\""

can be more precise, but risks losing quotes such as blockquotes (blockquotes will cause you problems anyways because they can include a newline before the closing quotation mark)

As for using NLTK, I can't think of anything there that will be of much help other than perhaps wordnet for finding synonyms for "said".




回答2:


This qualifies as a pattern, ie, data you are looking for is always between quotation marks "". Simply put, you can use regex for pattern matching. Let's take this example she said " DAS A SDASD sdasdasd SADSD", " SA23 DSD " ASDAS "ASDAS1 3123$ %$%"

The regex that works for your basic example is -

list = re.findall("\".*?\"", string)

List gives us ['" DAS A SDASD SADASD SADSD"', '" SA23 DSD "', '"ASDAS1 3123$ %$%"']

Here, .*? matches any character (except newline) and the pattern matches the quotation marks (beginning \" and ending \") literally.

Please beware of the fact that quotation marks within quotation marks breaks this code. You will not get the expected output.



来源:https://stackoverflow.com/questions/37936461/how-to-extract-quotations-from-text-using-nltk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!