0

I have been trying to cythonize the following loop, however I can't seem to cdef the np.array([],str). When compiling, array is not recognised as being part of numpy.

import numpy as np
from textblob import TextBlob

def arr_blob_corr(str_arr1):
    
    ph1=np.array([],str)
    
    for n1 in range(0,str_arr1.shape[0]):
        ph2=TextBlob(str_arr1[n1].lower()).correct().raw
        ph1=np.append(ph1,ph2)
        
    return ph1

I have tried

cdef np.ndarray arr2=np.ndarray(arr1,str)

and

cdef np.nrray arr2=np.array(arr1,str)

To no avail.

Cam K
  • 127
  • 2
  • 2
  • 13
  • Relevant: https://stackoverflow.com/questions/20268228/cython-cimport-and-import-numpy-as-both-np - it's because you have to `cimport` numpy to use it in static types. I'm trying to find a better duplicate though – DavidW Jul 03 '20 at 16:46
  • Don't spent too much time trying to statically type string arrays though - you'll find it difficult and won't gain much – DavidW Jul 03 '20 at 16:47
  • I'm not sure what you mean by statically typing string arrays. However, lists of lists of strings may be the way to go in this situation. – Cam K Jul 07 '20 at 13:00
  • There's very little advantage to setting the type of a variable to be `np.ndarray` - the advantages start to come when you tell Cython the type of the elements. Unfortunately it's quite hard to do that for an array of strings (which I think you're using?) – DavidW Jul 07 '20 at 13:47
  • That was what I was hoping to use, yes. – Cam K Jul 07 '20 at 13:48

0 Answers0