thu-coai / NAST

Codes for "NAST: A Non-Autoregressive Generator with Word Alignment for Unsupervised Text Style Transfer" (ACL 2021 findings)
MIT License
15 stars 1 forks source link

TypeError #5

Open FayeXXX opened 1 year ago

FayeXXX commented 1 year ago

There is a bug in styletransformer/seq2seq.py line 350, which is : "{TypeError}can only concatenate str (not "list") to str" I think the result["gen"][i] should be the sentence instead of token list

thangld201 commented 8 months ago

Hi @FayeXXX, @hzhwcmhf, I also encounter this error when running

cd styletransformer
python run.py --name STyelp0 --dataid ../data/yelp --clsrestore model/CLSyelp0_best.model

Does anyone have a fix ? (solved, I changed list to " ".join(list))

hzhwcmhf commented 8 months ago

@thangld201 I am not sure where is the problem... Is the output correct after modifying this line?

thangld201 commented 8 months ago

@hzhwcmhf There are two parts that I had to change: +) Line 350-351 to:

                show_str.append(f"sent{domain}:" + " ".join(result["sgen"][i]))
                show_str.append(f"gen{domain}:" + " ".join(result["gen"][i]))

+) Line 397-403 to:

            with open(f"{output_dir}/{self.now_batch}.neg2pos.txt", 'w') as f:
                for sent in result0["gen"]:
                    f.write(' '.join(sent)+ "\n")

            with open(f"{output_dir}/{self.now_batch}.pos2neg.txt", 'w') as f:
                for sent in result1["gen"]:
                    f.write(' '.join(sent)+ "\n")

The reasons being result["gen"][i],result["sgen"][i] and sent are all list of words (e.g. ["I","have","seen","this","cake"] instead of "I have seen this cake"), so they need to be formatted as string before writing to files like this.