私信  •  关注

Joffan

Joffan 最近创建的主题
Joffan 最近回复了
4 年前
回复了 Joffan 创建的主题 » 如何索引Python中每行有多个单词的输入单词

如果您想输出包含每次搜索中所有单词的每个句子,可以构建一个查找相关字符串的词典,然后缩小范围,只输出包含所有所需单词的项目。

news_sentences = '''Joe Biden is the us president
John McCain was a congressman
JPMorgan Chase is the way to go
This is an irrelevant sentence
Kanye West IS an artist'''.split('\n')


from collections import defaultdict

newsDict = defaultdict(set)
for sentence in news_sentences:
    for word in sentence.split():
        newsDict[word.lower()].add(sentence)
#
oov_ner_data = input('test, hit me: ').split()

report = newsDict[oov_ner_data[0].lower()].copy()
for word in oov_ner_data[1:]:
    report &= newsDict[word.lower()]

if report:
    print(*report,sep='\n')
else:
    print("Good news")

如果搜索词恰好出现在一个不相关的句子中,就有可能出现“假新闻”。你可以限制每个新闻句子的扫描量,但在我看来,关键词原则上可以出现在任何地方。您还可以在新闻解析和输入时潜在地丢弃常用词。

3 年前
回复了 Joffan 创建的主题 » Python-从日期输入中添加或减去N*个工作日*

似乎对你的日常生活稍加调整就能奏效:

from datetime import datetime, timedelta

def bizday_calc_func(self, start_date, num_days):
    my_start_date = start_date
    my_num_days = abs(num_days)
    inc = 1 if num_days > 0 else -1
    while my_num_days > 0:
      my_start_date += timedelta(days=inc)
      weekday = my_start_date.weekday()
      if weekday >= 5:
        continue
      my_num_days -= 1
    return my_start_date

免责声明:未经测试。