【发布时间】:2013-12-08 19:15:53
【问题描述】:
我有一个像这样的词库。有3000多个字。但是有2个文件:
File #1:
#fabulous 7.526 2301 2
#excellent 7.247 2612 3
#superb 7.199 1660 2
#perfection 7.099 3004 4
#terrific 6.922 629 1
#magnificent 6.672 490 1
File #2:
) #perfect 6.021 511 2
? #great 5.995 249 1
! #magnificent 5.979 245 1
) #ideal 5.925 232 1
day #great 5.867 219 1
bed #perfect 5.858 217 1
) #heavenly 5.73 191 1
night #perfect 5.671 180 1
night #great 5.654 177 1
. #partytime 5.427 141 1
我有很多这样的句子,3000多行如下:
superb, All I know is the road for that Lomardi start at TONIGHT!!!! We will set a record for a pre-season MNF I can guarantee it, perfection.
All Blue and White fam, we r meeting at Golden Corral for dinner to night at 6pm....great
我必须遍历每一行并完成以下任务:
1) 查找这些词库是否与句子中的任何位置匹配
2) 查找这些词的语料库是否匹配句子的开头和结尾
我能够完成第 2 部分)而不是第 1 部分)。我可以做到,但要找到一种有效的方法。 我有以下代码:
for line in sys.stdin:
(id,num,senti,words) = re.split("\t+",line.strip())
sentence = re.split("\s+", words.strip().lower())
for line1 in f1: #f1 is the file containing all corpus of words like File #1
(term2,sentimentScore,numPos,numNeg) = re.split("\t", line1.strip())
wordanalysis["trail"] = found if re.match(sentence[(len(sentence)-1)],term2.lower()) else not(found)
wordanalysis["lead"] = found if re.match(sentence[0],term2.lower()) else not(found)
for line in sys.stdin:
(id,num,senti,words) = re.split("\t+",line.strip())
sentence = re.split("\s+", words.strip().lower())
for line1 in f1: #f1 is the file containing all corpus of words like File #1
(term2,sentimentScore,numPos,numNeg) = re.split("\t", line1.strip())
wordanalysis["trail"] = found if re.match(sentence[(len(sentence)-1)],term2.lower()) else not(found)
wordanalysis["lead"] = found if re.match(sentence[0],term2.lower()) else not(found)
for line1 in f2: #f2 is the file containing all corpus of words like File #2
(term2,sentimentScore,numPos,numNeg) = re.split("\t", line1.strip())
wordanalysis["trail_2"] = found if re.match(sentence[(len(sentence)-1)],term.lower()) else not(found)
wordanalysis["lead_2"] = found if re.match(sentence[0],term.lower()) else not(found)
我这样做对吗?有没有更好的方法。
【问题讨论】:
-
在Redis中使用数据strcuctrue Hashes怎么样?首先,将这两个文件中的数据读取到存储在Hashes中的Redis中。然后在从句子中读取一个单词时,在 Redis 中进行哈希搜索,这可能会非常快。这可能会有所帮助hash command in redis
-
@flyer 是不是像java中的Hashtable?
-
抱歉,我对 Java 知之甚少。这是一个简单的解释:the little redis book
标签: python regex file file-io python-3.x