Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何从文本文件中获取特定单词前后的单词_String_Python 3.x_Tuples - Fatal编程技术网

String 如何从文本文件中获取特定单词前后的单词

String 如何从文本文件中获取特定单词前后的单词,string,python-3.x,tuples,String,Python 3.x,Tuples,我定义了这个函数来查找文本文件中的关键字,但现在我想得到一个元组,其中包含文件中关键字前后的单词,我不知道如何做到这一点 def findProperWords(paragraphs, excludedWords): key = [] # an empty list count = 0 for paragraph in paragraphs: #calling of every paaragraph in the textfile count += 1 #counting each p

我定义了这个函数来查找文本文件中的关键字,但现在我想得到一个元组,其中包含文件中关键字前后的单词,我不知道如何做到这一点

def findProperWords(paragraphs, excludedWords):
key = [] # an empty list
count = 0
for paragraph in paragraphs:  #calling of every paaragraph in the textfile
    count += 1 #counting each paragraph
    words = list(paragraph.split(' ')) 
           # spliting each paragraph into a list of words
    for keys in words: 
        if len(keys) > 0:
            if keys[0] == keys[0].upper(): 
                         #checking for words that start with capital letters
                if keys.lower() not in excludedWords: 
                     key.append(keys) # creating a list of the key words
                       index = paragraph.find(keys) 
                        # finding the position of each key word in the textile

试试这个,但请注意,它只能在段落中找到前面和后面的单词。如果你希望它在前一段/下一段中找到结果,考虑创建一个大的单词列表(如果内存限制允许的话),或者在迭代一个新段落时,更新前一个段落,并在最后一个段落的迭代中保存最后一个单词以供以后使用。
def findProperWords(paragraphs, excludedWords):
key = [] # an empty list
count = 0
for paragraph in paragraphs:  #calling of every paaragraph in the textfile
    count += 1 #counting each paragraph
    words = list(paragraph.split(' ')) 
           # spliting each paragraph into a list of words
    for idx,keys in enumerate(words):
        if len(keys) > 0:
            if keys[0] == keys[0].upper(): 
                         #checking for words that start with capital letters
                if keys.lower() not in excludedWords: 
                     key.append(keys) # creating a list of the key words
                     index = paragraph.find(keys) 
                     # finding the position of each key word in the textile
                     if idx > 0:
                         word_before = words[idx-1]
                     if idx < len(words) -2:
                        word_after = words[idx+1]
def findProperWords(段落,不包括单词):
key=[]#一个空列表
计数=0
对于段落中的段落:#调用文本文件中的每个paaragraph
计数+=1#计算每个段落
单词=列表(段落分割(“”))
#将每个段落拆分为单词列表
对于idx,枚举中的键(字):
如果len(键)>0:
如果键[0]==键[0]。上限()
#检查以大写字母开头的单词
如果keys.lower()不在excludedWords中:
关键字。附加(关键字)#创建关键字列表
索引=段落。查找(键)
#查找每个关键字在文本中的位置
如果idx>0:
单词前=单词[idx-1]
如果idx
只有一个小提示:有一个内置的方法可以检查大写:
如果键[0]。isupper():
。而
if len(key)>0:
可以简单地写成
if key: