2017-03-21 6 views
0

현재 가상 머신에시 생성기를 설치하려고합니다. 즉, ubunutu를 실행하고 있습니다.Python, Django, pickle resource Errno2 해당 파일이나 디렉토리가 없습니다.

Exception Type:  IOError 
Exception Value:  

[Errno 2] No such file or directory: 'poetry_generator/resources/bigram.pickle' 

Exception Location:  /home/lee/Downloads/PoEmo-master/poetry_generator/architecture/experts/generating_experts/collocation_expert.py in train, line 31 

가 poetry_generator 내에/자원 폴더가 없으며 bigram.pickles 중 하나를 그 안에 파일이 없습니다 : 나는 서버를 실행하고 텍스트를 입력하고 생성 쳤을 때, 나는 아래의 오류가 발생합니다. 다음 스크립트의 linke 31을 문제로 식별합니다.

import nltk 
import os 
import pickle 
from pattern import en 

from poetry_generator.structures.word import Word 
from poetry_generator.architecture.experts.generating_experts.word_generating_expert import WordGeneratingExpert 
from poetry_generator.settings import resources_dir 


class CollocationExpert(WordGeneratingExpert): 
    """Generating most common contexts for words for words""" 

    def __init__(self, blackboard): 
     super(
      CollocationExpert, 
      self).__init__(
      blackboard, 
      "Collocation Expert") 
     self.word_tag_pairs = [] 

    def train(self): 
     bigram_pickle_file = os.path.join(resources_dir, 'bigram.pickle') 
     try: 
      with open(bigram_pickle_file,'rb') as f: 
       self.word_tag_pairs = pickle.load(f) 

     except IOError: 
      tagged_words = nltk.corpus.brown.tagged_words(tagset='universal') 
      self.word_tag_pairs = list(nltk.bigrams(tagged_words)) 
      with open(bigram_pickle_file,'w') as f: 
       pickle.dump(self.word_tag_pairs,f) 


    '''Finding verbs for noun ''' 

    def _find_verbs(self, word): 
     word_bigrams = [(a[0], b[0]) for a, b in self.word_tag_pairs 
               if a[0] == word.name and a[1] == 'NOUN' and b[1] == 'VERB' 
               and en.conjugate(b[0], "inf") not in ('be', 'have')] 
     return self.__get_best_collocations(word, word_bigrams) 

    '''Finding adjectives for noun''' 

    def _find_epithets(self, word): 
     word_bigrams = [(b[0], a[0]) for (a, b) in self.word_tag_pairs 
         if b[0] == word.name and b[1] == 'NOUN' and a[1] == 'ADJ'] 
     epithets = self.__get_best_collocations(word, word_bigrams) 
     return epithets 

    '''Finding nouns described by adjective''' 

    def _find_comparisons(self, adjective): 
     word_bigrams = [(a[0], b[0]) for (a, b) in self.word_tag_pairs 
         if a[0] == adjective.name and b[1] == 'NOUN' and a[1] == 'ADJ'] 
     comparisons = self.__get_best_collocations(adjective, word_bigrams) 
     return comparisons 

    '''Adding epithets for noun to pool''' 

    def _add_epithets(self, word): 
     epithets = set([Word(w, "JJ") for w in self._find_epithets(word)]) 
     if word not in self.blackboard.pool.epithets: 
      self.blackboard.pool.epithets[word] = [] 
     self.blackboard.pool.epithets[word] += list(epithets) 
     return epithets 

    def _add_verbs(self, word): 
     verbs = set([Word(w, "V") for w in self._find_verbs(word)]) 
     self.blackboard.pool.verbs[word] = list(verbs) 
     return verbs 

    '''Adding nouns for adjectives to pool''' 

    def _add_comparisons(self, adj): 

     comparisons = set([Word(w, "N") for w in self._find_comparisons(adj)]) 
     self.blackboard.pool.comparisons[adj] = comparisons 
     return comparisons 

    def __get_best_collocations(self, word, word_bigrams, n=20): 
     words = nltk.ConditionalFreqDist(word_bigrams)[word.name] 
     best_bigrams = sorted(words.items(), key=lambda (k, v): v, reverse=False)[:n] 

     return dict(best_bigrams).keys() 

    def generate_words(self): 
     super(CollocationExpert, self).generate_words() 
     counter = 0 
     for w in self.blackboard.pool.nouns: 
      eps = self._add_epithets(w) 
      vs = self._add_verbs(w) 
      le = len(eps) 
      lv = len(vs) 
      counter += le + lv 
     for adj in self.blackboard.pool.adjectives: 
      comps = self._add_comparisons(adj) 
      counter += len(comps) 
     return counter 

아이디어가 있습니까? 나는 파이썬에 익숙하지 않아서 나는 정말로 실마리가 없습니다. 감사!

+0

"(아래의 모든 것이 코드로 등록되지 않은 이유는 확실하지만 그렇습니다)"=> 공백으로 텍스트를 들여 쓰기하여 코드로 표시해야합니다. 가장 간단한 해결책은 코드 부분을 선택하고 편집기의 도구 모음에서 "코드"버튼을 클릭하는 것입니다. –

답변

0

train 메서드의 세 번째 줄에서 오류가 발생하지만 사용자는이를 catch합니다. 그러나 잡기에서는 똑같은 것을 시도합니다. 파일을 여는 방법을 변경해도 오류가 생략되지는 않습니다. 파일/디렉토리가 없으면 먼저 만들어야합니다.

디렉터리를 재귀 적으로 생성하려면 this question을 참조하십시오.

파일을 만들려면 this이 도움이 될 수 있습니다.