在python中找出一个单词前缀

时间:2016-04-26 23:29:01

标签: python python-2.7 csv

我有一个普通英语单词的数据库。我想找到每个单词的前缀,并根据前缀对单词进行评分。我有3个评级系统,正面,中性和负面的catorgoties。我将每个catorgory变成了一个数组。现在我将每个单词分开,并希望将其与每个列表进行比较,以查看该单词的位置。

这是我到目前为止的代码。 import csv

negativePrefix = ['un', 'in', 'im','il','ir','non', 'mis','mal','dis','anti','de','under', 'semi', 'mini', 'ex', 'sub', 'infra']

postivePrefix = ['re', 'over', 'equi', 'micro','macro','mega','extra','prime', 'post','retro', 'bi','multi','pro','auto','co','con']

neutralPrefix = ['inter', 'super', 'super','peri', 'ante', 'pre','semi', 'mono', 'tri','quad','penta','hex','sept','oct','dec']

postive = 2
neutral = 1
negative = 0

path = r'/Users/Valerie/Desktop/ClassCoding/gdi/javascript_2/common_words_Update_2.csv'
fileName = 'common_words_Update_2'
  with open(path, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in reader:
        word = ' '.join(''.join(row)) 
        print word
        # check to see if the word matches one of the arrays

2 个答案:

答案 0 :(得分:1)

for row in reader:
    for word in row:
        if any([word.startswith(w) for w in negativePrefix]):
            print("{} is negative!".format(word))
        elif any([word.startswith(w) for w in postivePrefix]):
            print("{} is positive!".format(word))
        ...

答案 1 :(得分:0)

if any(word.startswith(prefix) for prefix in negativePrefix):
   ...
elif any(word.startswith(prefix) for prefix in posativePrefix):
   ...
elif any(word.startswith(prefix) for prefix in neutralPrefix):
  ...