我试图确定日期是否出现在任意字符串中。这是我的代码:
import nltk
txts = ['Submitted on 1st January',
'Today is 1/3/15']
def chunk(t):
w_tokens = nltk.word_tokenize(t)
pt = nltk.pos_tag(w_tokens)
ne = nltk.ne_chunk(pt)
print ne
for t in txts:
print t
chunk(t)
我得到的输出是
Submitted on 1st January
(S (GPE Submitted/NNP) on/IN 1st/CD January/NNP)
Today is 1/3/15
(S Today/NN is/VBZ 1/3/15/CD)
显然,日期没有被标记。有谁知道如何标记日期?
由于
答案 0 :(得分:2)
NLTK ne_chunk()
默认无法识别日期。您需要首先从nltk_contrib获取timex.py
来使用width
。
答案 1 :(得分:2)
我从您的评论 1/1/70 中获取了日期示例,但如果格式不同,例如1970/01/20或2-21-79,则此正则表达式代码也会找到它们
import re
x = 'asdfasdf sdf5sdf asd78fsadf 1/1/70 dfsdg fghdfgh 1970/01/20 gfh5fghh sdfgsdg 2-21-79 sdfgsdgf'
print re.findall(r'\d+\S\d+\S\d+', x)
输出:
['1/1/70', '1970/01/20', '2-21-79']
OR,
y = 'Asdfasdf Ddf5sdf asd78fsadf Jan 3 dfsdg fghdfgh February 10 sdfgsdgf'
print re.findall(r'[A-Z]\w+\s\d+', y)
输出:
['Jan 3', 'February 10']
答案 2 :(得分:1)
NLTK本身不会检测日期,而是将其与Stanford's Named Entity Tagger结合使用,它会。找到有效工作的正确指令可能很困难,所以这里有几个链接:
Stanford tagger site - look for downloads: https://nlp.stanford.edu/software/CRF-NER.shtml
Stanford tagger API - http://www.nltk.org/api/nltk.tag.html#nltk.tag.stanford.StanfordTagger
很抱歉,链接不适用于最后两个。
以下是我使用的代码:
from nltk.tag import StanfordNERTagger
stanfordClassifier = '/path/to/classifier/classifiers/english.muc.7class.distsim.crf.ser.gz'
stanfordNerPath = '/path/to/jar/stanford-ner-2017-06-09/stanford-ner.jar'
st = StanfordNERTagger(stanfordClassifier, stanfordNerPath, encoding='utf8')
result = st.tag(word_tokenize("The date is October 13, 2017"))
print (result)