Your dog might be scratching the carpet to get out pent-up energy. If your dog has too much energy and no way to get rid of it, then it might turn to more strange and destructive ways to tire itself out. Although scratching the carpet might be how your dog has chosen to preoccupy itself, you can curb this behavior by tiring them out in other ways like playing with them, going for more or longer walks, providing them with more toys, etc.
There’s another possible reason why your dog scratches the carpet, it’s because of an obsessive compulsive condition derived from its lifestyle. If your dog has been confined too much, poorly socialized, or exposed to stressful environments they can adopt inappropriate behaviors like scratching, chewing, pacing, and licking. Professional trainers can help curb and stop this behavior.