2016-04-26 14 views
0

私は共通の英語のデータベースを持っています。私はこれらの単語のそれぞれの接頭辞を見つけて、その接頭辞に基づいて単語を評価したいと思います。私は3つの評価制度、ポスト、ニュートラル、ネガティブを持っています。私はそれぞれのcatorgoryを配列に変えました。今、私は各単語を分けていて、単語がどこにあるのかを見るために各リストにそれを補完したいと思います。Pythonで単語プレフィックスを見つける

これはこれまでのコードです。インポート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 

答えて

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): 
    ... 
+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)) 
     ... 
関連する問題