wlav / cppyy

Other
384 stars 38 forks source link

How about this code? #219

Closed Soebb closed 4 months ago

Soebb commented 4 months ago

For these which haven't a class, how i should write it's python code? And how pass a user input to it since it have one?

#include "hazm.h"

#include <iostream>
#include <string>
using namespace Hazm;

int main()
{
    Normalizer normalizer;
    POSTagger tagger("pos_tagger.model");
    std::cout << "Enter text:";
    std::string input;
    std::getline(std::cin, input);
    std::string normlized_text = normalizer.normalize(input);
    std::string  processed_sentences = "";
    for (auto &sentence: sent_tokenize(normlized_text)) {
        std::vector<std::string> words = word_tokenize(sentence);
        std::string fixed_words = "";
        for (auto &word: tagger.tag(words)) {
            if ((word.type).substr(word.type.length()-1,1) == "Z" ) { 
                if ((word.word).substr(word.word.length()-2,2) != "ِ")  {
                    if ( (word.word).substr(word.word.length()-2,2) == "ه" and (word.word).substr(word.word.length()-4,2) != "ا") { 
                        word.word += "‌ی";
                    }
                }
                word.word += "ِ";
            }
            fixed_words += word.word + " ";
        }
        processed_sentences += fixed_words;

    }
    std::cout << processed_sentences << std::endl;
    return 0;
}
wlav commented 4 months ago

What are asking exactly? If you just want that above block run as-is from cppyy, drop it into a cppyy.cppdef call, then run cppyy.gbl.main().

If you are asking on how to get user input from a prompt, I wouldn't bother using the C++ facilities, but use Python's, e.g. https://docs.python.org/3/library/functions.html#input .