简体   繁体   English

检查字符串是否显示为自己的单词-Python

[英]Check if string appears as its own word - Python

Let's say that I am looking for the word "or" . 假设我正在寻找单词"or" What I want is to check whether that word appears as a word or as a substring of another word. 我要检查的是该单词是作为单词出现还是作为另一个单词的子串出现。

Eg 例如

Input - "or" Output - "true" 输入-“或”输出-“真”

Input - "for" Output - "false" 输入-“ for”输出-“ false”

I suppose I could check if the chars before and after are letters but is there a more efficient/easier way of doing it? 我想我可以检查一下前后的字符是否为字母,但是是否有更有效/更简便的方法呢? Thanks 谢谢

Edit In addition, the string will be part of a sentence. 编辑此外,字符串将成为句子的一部分。 So I want "I can go shopping or not" to return true but "I can go shopping for shoes" to return false. 因此,我希望“我可以去购物还是不去购物”返回true,但是“我可以去购物”可以返回false。 Therefore using == wouldn't work. 因此,使用==无效。 I'm sorry I should've mentioned this earlier 对不起,我应该早些提及

Use a regex. 使用正则表达式。

>>> import re
>>> re.search(r'\bor\b', 'or')
<_sre.SRE_Match object at 0x7f445333a5e0>
>>> re.search(r'\bor\b', 'for')
>>> 

You can use a regular expression for this: 您可以为此使用正则表达式:

import re

def contains_word(text, word):
    return bool(re.search(r'\b' + re.escape(word) + r'\b', text))

print(contains_word('or', 'or')) # True
print(contains_word('for', 'or')) # False
print(contains_word('to be or not to be', 'or')) # True

Create a checker with just a test if it is in the line 只是一个测试创建一个检查,如果它是该行

def check_word_in_line(word, line):
    return " {} ".format(word) in line

print(check_word_in_line("or", "I can go shopping or not")) //True
print(check_word_in_line("or", "I can go shopping for shoes")) //False

You can use the nltk (Natural Language Toolkit) to split the sentence into words, and then check if some word exists with == . 您可以使用nltk(自然语言工具包)将句子拆分为单词,然后使用==检查是否存在某个单词。

NLTK Installation NLTK安装

NLTK Package Download NLTK软件包下载

import nltk

def checkword(sentence):
    words = nltk.word_tokenize(sentence)
    return any((True for word in words if word == "or"))

print(checkword("Should be false for."))
print(checkword("Should be true or."))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM