Home>

https://github.com/o-tomox/TextGenerator
I made the following code available in Python3, and when I tried to run it in the Django app, I got the following error.

cannot use a string pattern on a bytes-like object

While running from the command line, it works without any errors, why does Django give me such an error?

Environment is
Python 3.6.3
Django 2.2.dev20180610131139
(The necessary modules are installed and are basically the latest version.)

The error codes and the peripheral codes that are operating are as follows.

/Users/Usename/webapp/app/views.py in sample
        triplet_freqs = chain.make_triplet_freqs () ...
▼ Local vars
Variable Value
chain
<app.models.PrepareChain object at 0x10dcfc978>
request
<WSGIRequest: GET '/ sample? your_name =% E6% 96% 87% E7% AB% A0% E3% 82% 92% E5% 85% A5% E5% 8A% 9B% E3% 81% 97% E3% 81% A6% E3% 81% 8F% E3% 81% A0% E3% 81% 95% E3% 81% 84 '>
text
(b '\ xe6 \ x96 \ x87 \ xe7 \ xab \ xa0 \ xe3 \ x82 \ x92 \ xe5 \ x85 \ xa5 \ xe5 \ x8a \ x9b \ xe3'
 b '\ x81 \ x97 \ xe3 \ x81 \ xa6 \ xe3 \ x81 \ x8f \ xe3 \ x81 \ xa0 \ xe3 \ x81 \ x95 \ xe3 \ x81 \ x84')
------------------------------------
/Users/Usename/webapp/app/models.py in make_triplet_freqs
        def make_triplet_freqs (self):
        u "" "
        From morphological analysis to the number of occurrences of triplets
        @return Dictionary of triples and their occurrences key: triples (tuples) val: number of occurrences
        "" "
        # Split long sentences into sentences
        sentences = self._divide (self.text) ...
        # Number of triples
        triplet_freqs = defaultdict (int)
        # Make a triple for each sentence
        for sentence in sentences:
▼ Local vars
Variable Value
self
<app.models.PrepareChain object at 0x10dcfc978>
------------------------------------
/Users/Usename/webapp/app/models.py in _divide
        @param text Text before splitting
        @return An array of sentences
        "" "
        # Split characters other than newline characters (regular expression notation)
        delimiter = u ". |. | \."
        # Replace all split characters with newline characters (in order not to lose information such as "." When split)
        text = re.sub (u "({0})". format (delimiter), r "\ 1 \ n", text) ...
        # Split at newline
        sentences = text.splitlines ()
        # Remove leading and trailing whitespace
        sentences = [sentence.strip () for sentence in sentences]
▼ Local vars
Variable Value
delimiter
'. |. | \\. '
self
<app.models.PrepareChain object at 0x10dcfc978>
text
(b '\ xe6 \ x96 \ x87 \ xe7 \ xab \ xa0 \ xe3 \ x82 \ x92 \ xe5 \ x85 \ xa5 \ xe5 \ x8a \ x9b \ xe3'
 b '\ x81 \ x97 \ xe3 \ x81 \ xa6 \ xe3 \ x81 \ x8f \ xe3 \ x81 \ xa0 \ xe3 \ x81 \ x95 \ xe3 \ x81 \ x84')
--------------------------------------
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py in sub
    def sub (pattern, repl, string, count = 0, flags = 0):
    "" "Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.
    a callable, it's passed the match object and must return
    a replacement string to be used. "" "
    return _compile (pattern, flags) .sub (repl, string, count)
▼ Local vars
Variable Value
count
0
flags
0
pattern
'(. |. | \\.)'
repl
'\\ 1 \\ n'
string
(b '\ xe6 \ x96 \ x87 \ xe7 \ xab \ xa0 \ xe3 \ x82 \ x92 \ xe5 \ x85 \ xa5 \ xe5 \ x8a \ x9b \ xe3'
 b '\ x81 \ x97 \ xe3 \ x81 \ xa6 \ xe3 \ x81 \ x8f \ xe3 \ x81 \ xa0 \ xe3 \ x81 \ x95 \ xe3 \ x81 \ x84')