Python - 检查一行中的所有单词是否存在于数组中
Python - Checking whether all the words in a line exist in an array
注意:对于这个问题,除了 sys 和 io 之外,我不能使用任何导入
对于一个作业,我必须将两个文件作为系统参数,并且两个文件都包含字符串行。
为了完成我的作业,我想一次读取一个文件中的一行,并检查该行中的所有单词是否都存在于另一个文件中。
文件如下:
g1.ecfg
S -> NP VP
NP -> Det N
NP -> PN
Det -> "the"
N -> "dog"
N -> "rat"
N -> "elephant"
PN -> "Alice"
PN -> "Bob"
VP -> V NP
V -> "admired"
V -> "bit"
V -> "chased"
u1a.utt
the aardvark bit the dog
the dog bit the man
Bob killed Alice
所以,我想阅读 u1a.utt 中的每一行,并检查该行中的每个单词是否在 [=50= 中找到].
我认为 g1 中的引号可能有问题,所以我将所有引号中的单词放在一个数组中,不保留引号。
我当前的代码总是 returns false,即使字符串应该打印 "Parsing!!!"
也会产生 "No valid parse"
谁能帮我理解如何将每行中的单词与 g1 文件进行比较?
这是我的代码:
import sys
import io
# usage = python CKYdet.py g#.ecfg u#L.utt
# Command Line Arguments - argv[0], argv[1], argv[2]
script = sys.argv[0]
grammarFile = open(sys.argv[1])
utteranceFile = open(sys.argv[2])
# Initialize rules from grammarFile
ruleArray = []
wordsInQuotes = []
uttWords = []
for line in grammarFile:
rule = line.rstrip('\n')
start = line.find('"') + 1
end = line.find('"', start)
ruleArray.append(rule)
wordsInQuotes.append(line[start:end]) #create a set of words from grammar file
for line in utteranceFile:
x = line.split()
print x
if (all(x in grammarFile for x in line)): #if all words found in grammarFile
print "Parsing!!!"
else:
print "No valid parse"
我认为这可能与我的列表是否可哈希有关,或者可能是范围问题,但我正在努力寻找适合我的替代方案。
让我们使用集合来存储项目,稍后我们将检查成员资格,并使用 str.split
查找引号中的单词。
with open('grammarfile') as f:
words = set()
for line in f:
line = [a for a in line.split() if '"' in a]
for a in line:
words.add(a.replace('"', ''))
with open('utterancefile') as f:
for line in f:
if all(a in words for a in line.split())
print("Good Parse")
else:
print("Word not found")
注意:对于这个问题,除了 sys 和 io 之外,我不能使用任何导入
对于一个作业,我必须将两个文件作为系统参数,并且两个文件都包含字符串行。
为了完成我的作业,我想一次读取一个文件中的一行,并检查该行中的所有单词是否都存在于另一个文件中。
文件如下:
g1.ecfg
S -> NP VP
NP -> Det N
NP -> PN
Det -> "the"
N -> "dog"
N -> "rat"
N -> "elephant"
PN -> "Alice"
PN -> "Bob"
VP -> V NP
V -> "admired"
V -> "bit"
V -> "chased"
u1a.utt
the aardvark bit the dog
the dog bit the man
Bob killed Alice
所以,我想阅读 u1a.utt 中的每一行,并检查该行中的每个单词是否在 [=50= 中找到].
我认为 g1 中的引号可能有问题,所以我将所有引号中的单词放在一个数组中,不保留引号。
我当前的代码总是 returns false,即使字符串应该打印 "Parsing!!!"
也会产生 "No valid parse"谁能帮我理解如何将每行中的单词与 g1 文件进行比较?
这是我的代码:
import sys
import io
# usage = python CKYdet.py g#.ecfg u#L.utt
# Command Line Arguments - argv[0], argv[1], argv[2]
script = sys.argv[0]
grammarFile = open(sys.argv[1])
utteranceFile = open(sys.argv[2])
# Initialize rules from grammarFile
ruleArray = []
wordsInQuotes = []
uttWords = []
for line in grammarFile:
rule = line.rstrip('\n')
start = line.find('"') + 1
end = line.find('"', start)
ruleArray.append(rule)
wordsInQuotes.append(line[start:end]) #create a set of words from grammar file
for line in utteranceFile:
x = line.split()
print x
if (all(x in grammarFile for x in line)): #if all words found in grammarFile
print "Parsing!!!"
else:
print "No valid parse"
我认为这可能与我的列表是否可哈希有关,或者可能是范围问题,但我正在努力寻找适合我的替代方案。
让我们使用集合来存储项目,稍后我们将检查成员资格,并使用 str.split
查找引号中的单词。
with open('grammarfile') as f:
words = set()
for line in f:
line = [a for a in line.split() if '"' in a]
for a in line:
words.add(a.replace('"', ''))
with open('utterancefile') as f:
for line in f:
if all(a in words for a in line.split())
print("Good Parse")
else:
print("Word not found")