Home>

When running a py file at the command prompt, the following error occurred and it was not possible to proceed.

Occurrences/error messages
File "main.py", line 12, in<module>
    proto = Unmo ('proto')
  File "C: \ Users \ 聡 一郎 \ repos \ unmo \ unmo.py", line 24, in __init__
    self._dictionary = Dictionary ()
  File "C: \ Users \ Kazuichiro \ repos \ unmo \ dictionary.py", line 39, in __init__
    count, template = line.strip (). split ('t')
ValueError: not enough values ​​to unpack (expected 2, got 1)
Applicable source code

dicitionary.py

import os.path
from collections import defaultdict
import morph
class Dictionary:
    "" "Dictionary class for thought engine.

    Static method:
    touch_dics ()-touch the dictionary file
    make_pattern (str)-helper for reading a pattern dictionary
    pattern_to_line (pattern)-convert pattern hash to pattern dictionary format
    Property:
    random-random dictionary
    pattern-Pattern dictionary
    template-Template dictionary
    "" "
    DICT = {'random': 'dics_random.txt',
            'pattern': 'dics_pattern.txt',
            'template': 'dics_template.txt',
            }

    def __init __ (self):
        "" "Read dictionary from file." ""
        Dictionary.touch_dics ()
        with open (Dictionary.DICT ['random'], encoding = 'utf-8') as f:
            self._random = [l for l in f.read (). splitlines () if l]
        with open (Dictionary.DICT ['pattern'], encoding = 'utf-8') as f:
            self._pattern = [Dictionary.make_pattern (l) for l in f.read (). splitlines () if l]
        with open (Dictionary.DICT ['template'], encoding = 'utf-8') as f:
            self._template = defaultdict (lambda: [], {})
            for line in f:
                count, template = line.strip (). split ('t')
                if count and template:
                    count = int (count)
                    self._template [count] .append (template)
    def study (self, text, parts):
        "" "Save random, pattern, and template dictionaries in memory." ""
        self.study_random (text)
        self.study_pattern (text, parts)
        self.study_template (parts)
    def study_template (self, parts):
        "" "Receives the morpheme list parts,
        Only the noun is changed to '% noun%' and the template string is added to self._template.
        If the noun does not exist or if the same template exists, do nothing.
        "" "
        template = ''
        countd = 0
        for word, part in parts:
            if morph.is_keyword (part):
                word = '% noun%'
                count + = 1
            template + = word
        if count>0 and template not in self._template [couont]:
            self._template [count] .append (template)
    def study_random (self, text):"" "Save the user's utterance text in a random dictionary.
        If there is already the same statement, do nothing. "" "
        if not text in self._random:
            self._random.append (text)
    def study_pattern (self, text, parts):
        "" "Saves user utterance text in pattern dictionary based on morpheme parts." ""
        for word, part in parts:
            if self.is_keyword (part): # learn if part of speech is a noun
                # Check for duplicate words
                # Add a pattern if registered with the same word
                # If not, create a new pattern
                duplicated = next ((p for p in self._pattern if p ['pattern'] == word), None)
                if duplicated:
                    if not text in duplicated ['phrases']:
                        duplicated ['phrases']. append (text)
                else:
                    self._pattern.append ({'pattern': word, 'phrases': [text]})
    def save (self):
        "" "Save the dictionary in memory to a file." ""
        with open (Dictionary.DICT ['random'], mode = 'w', encoding = 'utf-8') as f:
            f.write ('\ n'.join (self.random))
        with open (Dictionary.DICT ['pattern'], mode = 'w', encoding = 'itf-8') as f:
            f.write ('\ n'.join (self.random))
        with open (Dictionary.DICT ['template'], mode = 'w', encoding = 'utf-8') as f:
            for count, templates in self._template.items ():
                for template in templates:
                    f.write ('{} \ t {} \ n'.format (count, template))

    @staticmethod
    def make_pattern (line):
        "" "Split the string line with single-byte spaces and return it in the form {'pattern': [0], 'phrases': [1]}." ""
        pattern, phrases = line.split ("")
        if pattern and phrases:
            return {'pattern': pattern, 'phrases': phrases.split ('|')}

    @staticmethod
    def pattern_to_line (pattern):
        "" "Converts the pattern hash into a string." ""
        return '{} {}'. format (pattern ['pattern'], '|' .join (pattern ['phrases']))
    @staticmethod
    def touch_dics ():
        "" "If there is no dictionary file, create an empty file, do nothing." ""
        for dic in Dictionary.DICT.values ​​():
            if not os.path.exists (dic):
                open (dic, 'w'). close ()

    @property
    def random (self):
        "" "Random dictionary" ""
        return self._random
    @property
    def pattern (self):
        "" "Pattern dictionary" ""
        return self._pattern
    @property
    def template (self):
        "" "Template dictionary" ""
        return self._template


dics_template.txt

I am% noun% of% noun%
% noun% is% noun%
I went to% noun% during this time and met% noun %% noun%
Tried

The same error occurred before, but at that time it was solved by changing to `` split ('t') → split ('') '', but this time it is not a split by key and value So you can't divide the contents of one line into two. There may be errors other than this, but since it is a python beginner who makes reference to the site (http://sandmark.hateblo.jp/entry/2017/10/11/004126), do not proceed Hmm. I would appreciate it if you could understand the meaning of this error.

  • Answer # 1

    The contents of dics_template.txt, but if you look closely at the reference HP, it was as follows.

    2 I am% noun% of% noun%
    2% noun% is% noun%
    3 When I went to% noun% during this time, I met% noun% of% noun%

    The key (count) is the left number and the value (template) is the right string. So I added the key numbers and split
    It was solved just by ('').