python - 将文本中的股票代码与股票代码列表匹配,而不匹配停用词
问题描述
我有一个包含大约 28,000 个股票代码的 python 列表。
我正在解析我抓取的文本以与股票代码匹配,并在我得到匹配时增加计数。
我遇到的问题是所有停用词都与一些我不想要的代码匹配;例如,V 是一个合法的股票代码,并且与各个标记化的词匹配,因为它是自由流动的社交媒体文本。例如,V 想要 TSLA。
你能建议我一些逻辑,我可以使用这些停用词应用一些逻辑智能匹配吗?
counts = dict()
Symbol_list =['TSLA','V','T','AAPL',...]
example sentence = { 'V want TSLA but not. T + 5 times' }
这是我迄今为止尝试过的:
sen = example_sentence.translate(str.maketrans('','',string.punctuation))
sentence_words = sen.split()
for words in sentence_words:
if(word in symbol_list):
counts[word] = counts.get(word,0) + 1
我想拥有{'TSLA':1}
但没有{'TSLA':1, 'V':1, 'T': 1}
。在某些情况下,我可能需要将 T 和 V 添加到字典中,但要根据上下文。
解决方案
import string
counts = dict()
example_sentence = { 'V want TSLA but not. T + 5 times' }
counts = dict()
symbol_list =['TSLA','V','T','AAPL',...]
example_sentence = 'V want TSLA but not. T + 5 times'
sen = example_sentence.translate(str.maketrans('','',string.punctuation))
sentence_words = sen.split()
counts_list = []
for word in sentence_words:
if(word in symbol_list):
counts[word] = counts.get(word,0) + 1
counts_list.append({word:counts[word]})
现在你有一个字典列表作为输出:counts_list[1] {'TSLA': 1}
推荐阅读
- ef-core-3.1 - ef core 接收奇怪的主机不能为空错误
- c# - 如何向 XAML 页面 BindingContext 提供具有带有参数的构造函数的 ViewModel
- python - 如何从另一个脚本中获取输出
- java - 为行矩阵乘法运行多个线程
- mysql - Mysql 触发器 ON INSERT + INNER JOIN 语法错误
- python - 有没有办法在您的代码中使 dict 可调用?
- kubernetes - Prometheus - 查询过去 24 小时内存在的 pod 数量
- list - 从 Racket 的列表中复制 n 次元素
- python - ROC 曲线 sklearn 概率与分数
- woocommerce - 如何在 Woocommerce 订单管理页面中显示用户元字段?