2016-11-08 77 views
0

我的代码需要句子,并在该句子中找到给定的单词。需要句子分析帮助

如果单词在句子中,它需要说,它已发现的话,说什么样的立场字是。

如果字是不是在它应该显示错误消息的句子。

我有这样的:

print("Please insert your sentence without punctuation") 
sentence=(input()) 
variable1='sentence' 
print("Which word would you like to find in your sentence?") 
word=input() 
variable2='word' 
if 'word'=='COUNTRY': 
    'variable3'==5 
    'variable4'==17 
if word in sentence: 
    print([word], "is in positions", [variable3], "and", [variable4]); 
else: 
    print("Your word is not in the sentence!") 
+0

你预计' '字' =='COUNTRY''是真的吗?你的意思是说,如果word =='COUNTRY''? – doctorlove

+2

这听起来像是一个很短的时间的重大任务.. –

+0

这段代码是完全没用的。你有没有运行过这段代码?你能告诉我们这段代码的哪部分不起作用吗? – pt12lol

回答

0

的Python序列提供index方法。它给你一个元素的索引,或者如果元素不在序列中,则会产生一个错误。在字符串上,它允许您查找子字符串。

>>> 'hello world'.index('world') 
6 
>>> 'hello world'.index('word') 
ValueError: substring not found 

基本上,你必须添加输入的句子和单词来搜索。而已。

print("Insert sentence without punctuation...") 
sentence=input() # get input, store it to name `sentence` 
print("Insert word to find...") 
word=input() 
try: 
    idx = sentence.index(word) 
except ValueError: # so it wasn't in the sentence after all... 
    print('Word', word, 'not in sentence', repr(sentence)) 
else: # if we get here, IndexError was NOT thrown 
    print('Word', word, 'first occurs at position', idx) 

这里有一些注意事项,例如'fooworldbar'也会匹配。这些事情的正确处理取决于人们想要的东西。我猜你实际上想要单词职位。


如果您需要在“的n个字”的含义位置,你必须改变句子单词的列表。 str.split这样做。然后您可以再次使用index。另外,如果您想全部职位,您必须反复调用索引。

print("Insert sentence without punctuation...") 
sentence = input() # get input, store it to name `sentence` 
words = sentence.split() # split at whitespace, creating a list of words 
print("Insert word to find...") 
word=input() 
positions, idx = [], -1 
while idx < len(words): 
    try: 
     idx = words.index(word, idx+1) 
    except ValueError: # so it wasn't in the rest of the sentence after all... 
     break 
    else: # if we get here, IndexError was NOT thrown 
     positions.append(idx) # store the index to list of positions 
# if we are here, we have searched through the whole string 
if positions: # positions is not an empty list, so we have found some 
    print('Word', word, 'appears at positions', ', '.join(str(pos) for pos in positions)) 
else: 
    print('Word', word, 'is not in the sentence') 
0

我想处理所提供的代码中的一些误解。

首先,

print("Please insert your sentence without punctuation") 
sentence=(input()) 

是简单,因为

sentence = input("Please insert your sentence without punctuation") 

现在我有一个名为sentence变量wihich不应与字符串'sentence'

同样可以糊涂,我们可以说

word = input("Which word would you like to find in your sentence?") 

给出了另一个变量word又不与字符串'word'

假设因为我们有争论的缘故被糊涂,

sentence = "Has this got an elephant in?" 

,我们搜索单词'elephant'

张贴的代码尝试使用in,但会发生这种情况:

>>> "elephant" in sentence 
True 
>>> "ele" in sentence 
True 
>>> "giraffe" in sentence 
False 
>>> 

关闭。但不够近。它不是在寻找整个词汇,因为我们在'elephant'中发现了'ele'

如果将split这个句子转化为单词,如其他答案所示,则可以再搜索整个单词找到该位置。 (查找拆分;您可以选择除默认' '之外的其他字符)。

words = sentence.split() 
word = 'ele' 
words.index(word) 

如果单词不存在,你会得到一个错误:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ValueError: 'ele' is not in list 

我会离开的错误处理给你。

0

您可以使用re模块:

import re 

sentence = input('Sentence: ') 
word = input('Word: ') 

## convert word in regular expression for search method. 
regex_word = r'(' + word + ')(?=\s|$)' 

## initialize search var. 
search = re.search(regex_word, sentence) 

if search: 
    while search: 
     match_pos = search.span() 
     sentence = sentence[:match_pos[0]] + sentence[match_pos[1]:] 

     print(word + ' is in position ' + str(match_pos[0])) 
     search = re.search(regex_word, sentence) 
else: 
    print(word + ' is not present in this sentence')