すぐ忘れるのでメモ

c++初心者です

google-chrome で日本語入力できない

firefoxに続いてgoogle-chromeでも日本語入力の挙動がおかしくなった.最初は問題なく動いていたのに... 以下のサイトを参考にしたら,とりあえず動いた.

tabeta-log.blogspot.com

やったことは,以下のことのみ.あとは,chromeのプロセスをキルして,立ち上げ直したら動いた.

sudo apt reinstall xkb-data

firefoxのインストール

問題

環境はubuntu23.10. firefoxを立ち上げるとこんなエラーが出てきて日本語入力(fcitx5)が出来ない.

[Parent 19490, Main Thread] WARNING: Create input context failed: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: An AppArmor policy prevents this sender from sending this message to this recipient; type="method_call", sender=":1.277" (uid=1000 pid=19490 comm="/snap/firefox/3728/usr/lib/firefox/firefox" label="snap.firefox.firefox (enforce)") interface="org.freedesktop.IBus" member="CreateInputContext" error name="(unset)" requested_reply="0" destination="org.freedesktop.IBus" (uid=1000 pid=2289 comm="fcitx5" label="unconfined").: 'glib warning', file /build/firefox/parts/firefox/build/toolkit/xre/nsSigHandlers.cpp:187

(firefox:19490): IBUS-WARNING **: 14:31:23.240: Create input context failed: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: An AppArmor policy prevents this sender from sending this message to this recipient; type="method_call", sender=":1.277" (uid=1000 pid=19490 comm="/snap/firefox/3728/usr/lib/firefox/firefox" label="snap.firefox.firefox (enforce)") interface="org.freedesktop.IBus" member="CreateInputContext" error name="(unset)" requested_reply="0" destination="org.freedesktop.IBus" (uid=1000 pid=2289 comm="fcitx5" label="unconfined").
[Parent 19490, Main Thread] WARNING: Create input context failed: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: An AppArmor policy prevents this sender from sending this message to this recipient; type="method_call", sender=":1.277" (uid=1000 pid=19490 comm="/snap/firefox/3728/usr/lib/firefox/firefox" label="snap.firefox.firefox (enforce)") interface="org.freedesktop.IBus" member="CreateInputContext" error name="(unset)" requested_reply="0" destination="org.freedesktop.IBus" (uid=1000 pid=2289 comm="fcitx5" label="unconfined").: 'glib warning', file /build/firefox/parts/firefox/build/toolkit/xre/nsSigHandlers.cpp:187

(firefox:19490): IBUS-WARNING **: 14:31:23.241: Create input context failed: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: An AppArmor policy prevents this sender from sending this message to this recipient; type="method_call", sender=":1.277" (uid=1000 pid=19490 comm="/snap/firefox/3728/usr/lib/firefox/firefox" label="snap.firefox.firefox (enforce)") interface="org.freedesktop.IBus" member="CreateInputContext" error name="(unset)" requested_reply="0" destination="org.freedesktop.IBus" (uid=1000 pid=2289 comm="fcitx5" label="unconfined").

解決策

ubuntufirefoxはsnap版を入れるらしい.解決できそうな感じのページを見つけたが時間をかけたくないので,手っ取り早く解決するために,snap版を捨てて,バイナリをダウンロードした. 以下のページからlinux用のバイナリをダウンロードする. www.mozilla.org ダウンロードして,/opt に入れて,リンクしておけば良い.

tar xjf firefox-xx.x.tar.bz2
sudo mv firefox /opt/
sudo ln -s /opt/firefox/firefox /usr/local/bin/firefox

gurobi10.03でc++

g++-9 -m64 -g -o mip1_c++ ../c++/mip1_c++.cpp -I/opt/gurobi1003/linux64/include -L/opt/gurobi1003/linux64/lib -lgurobi_c++ -lgurobi100 -lm

とすると

/usr/bin/ld: /tmp/ccF9f4Jr.o: in function `main':
/home/tom/Dropbox/research/gurobi/examples/c++/../c++/mip1_c++.cpp:22:(.text+0xac): undefined reference to `GRBEnv::set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/saito/Dropbox/research/gurobi/examples/c++/../c++/mip1_c++.cpp:29:(.text+0x169): undefined reference to `GRBModel::addVar(double, double, double, char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

というエラーが出た. g++のバージョンが新しい場合,libgurobi_g++5.2.a を使うらしい.上記で指定している libgurobi_c++.a はlibgurobi_g++4.8.a のリンク.

ls -l /opt/gurobi1003/linux64/lib/libgurobi_c++.a 
lrwxrwxrwx 1 5075 501 20 Sep  6 11:15 /opt/gurobi1003/linux64/lib/libgurobi_c++.a -> ./libgurobi_g++4.8.a

以下,設定をメモ .barhrcに書いたこと.

export GUROBI_HOME=/opt/gurobi1003/linux64
export PATH=$GUROBI_HOME/bin:$PATH
export LD_LIBRARY_PATH=$GUROBI_HOME/lib:$LD_LIBRARY_PATH
export GRB_LICENSE_FILE=/home/tom/gurobi.lic

Distinction2000のmp3ファイルを結合

Distinction2000でダウンロードしてきた単語が単語,例文ごとに細かく分けられていたので,結合してみた.ffmpegが使える環境が必要.以下は,ubuntu22.04で実行.

プログラムの中身

  • 最初の4桁の数字が同じファイルを1つにまとめる.4桁の数字の次の数字の順にならべる.
  • ファイル名は4桁の数字_単語名.mp3
  • TAG付けは,曲名=ファイル名,アーティスト=atsu,アルバム=Distinction2000
  • 出力先はoutputと言う名のディレクト

ChatGPTにお願いして作成したpythonコード

import os
import re
import subprocess
import eyed3

def embed_tags(file_path, title, artist="atsu", album="Distinction2000"):
    audiofile = eyed3.load(file_path)

    if not audiofile.tag:
        audiofile.initTag()

    audiofile.tag.title = title
    audiofile.tag.artist = artist
    audiofile.tag.album = album

    audiofile.tag.save()

def merge_and_tag(prefix):
    # カレントディレクトリ内の該当するMP3ファイルを取得
    mp3_files = [f for f in os.listdir() if f.endswith('.mp3') and f.startswith(f'{prefix}.')]

    # prefix の後に続く英単語を抽出
    word_match = next((re.search(f'{prefix}\.0 (\w+)', f) for f in mp3_files if f.startswith(f'{prefix}.0 ')), None)
    if not word_match:
        print(f"No matching file found for prefix {prefix}!")
        return

    word = word_match.group(1)

    # prefix の後に続く数字でソートするための関数
    def sorting_key(filename):
        match = re.search(f'{prefix}\.(\d+)', filename)
        if match:
            return int(match.group(1))
        return float('inf')  # 数字がない場合、最後になるようにする

    # ソート
    files_to_merge = sorted(mp3_files, key=sorting_key)

    # 出力ディレクトリとファイル名
    output_dir = "output"
    output_file = os.path.join(output_dir, f"{prefix}_{word}.mp3")

    # 出力ディレクトリが存在しない場合、作成
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # ffmpeg コマンドを構築
    input_files_str = " ".join([f"-i \"{f}\"" for f in files_to_merge])
    cmd = f"ffmpeg {input_files_str} -filter_complex concat=n={len(files_to_merge)}:v=0:a=1 -acodec libmp3lame {output_file}"

    # コマンドの実行
    subprocess.call(cmd, shell=True)

    # タグ埋め込み
    embed_tags(output_file, f"{prefix}_{word}")


# 0004 から 0007 までの処理
for i in range(1, 2001):
    merge_and_tag(str(i).zfill(4))

インクルードガード

質問:

インクルードガードとしてprogram onceが使われていますが,こちらを使うメリットはなんですか?ifでガードするのは時代遅れですか?


ChatGPTの答え:

#pragma once#ifndef/#define ガードの両方には、それぞれのメリットとデメリットがあります。#pragma once は新しい形式であり、多くの現代のコンパイラでサポートされていますが、#ifndef/#define ガードもまだ広く使われています。以下に、それぞれのメリットとデメリットを示します。

#pragma once のメリット:

  1. シンプル:ヘッダファイルの先頭と末尾に1行ずつ追加するだけで、インクルードガードが完成します。
  2. ユニークネス#define ガードを使う場合、マクロ名がユニークであることを確認する必要がありますが、#pragma once にはその心配はありません。
  3. 速度:一部のコンパイラにおいて、#pragma once の方が微妙に高速に動作すると言われています。

#ifndef/#define ガードのメリット:

  1. ポータビリティ:ほとんどすべてのC++コンパイラでサポートされています。一方、#pragma once は非標準であり、すべてのコンパイラやプラットフォームでサポートされているわけではありません(ただし、現代の主要なコンパイラはサポートしています)。
  2. 明示的:どのマクロでガードされているかが明確にわかります。
  3. 拡張性:他のプリプロセッサの命令と組み合わせることができます。

時代遅れか?

#ifndef/#define ガードが時代遅れであるとは言えません。実際、多くのプロジェクトやライブラリではまだこれを使用しています。しかし、新しいプロジェクトやコードベースで#pragma once を使用することが増えてきており、これには上記のようなメリットが影響しています。

最終的に、どちらの方法を使用するかはプロジェクトの要件、対象となるコンパイラやプラットフォーム、そしてチームの好みによるでしょう。

発音(母音)

発音の勉強に役に立ちそうなyoutubeのリンクのまとめ

Rachel's English はElsa Speak からリンクされてる動画とだいたい一緒.

American English - AA [æ] Vowel - How to make the AA Vowel - YouTube American English - AH [ɑ] Vowel - How to make the AH Vowel - YouTube American English - UH [ʌ] Vowel - How to make the UH Vowel - YouTube American English Sounds - UH [ə] Vowel - How to make the SCHWA Vowel - YouTube American English - EH [ɛ] Vowel - How to make the EH Vowel - YouTube American English - IH [ɪ] Vowel - How to make the IH Vowel - YouTube American English - EE [i] Vowel - How to make the EE Vowel - YouTube

English Pronunciation: Sit /ɪ/ vs Seat /i:/ - YouTube English Pronunciation: Bat /æ/ vs But /Ʌ/ - YouTube English Pronunciation: Bad /æ/ vs Bed /ɛ/ - YouTube

ubuntuでのdebのインストール

obsidian, slack, ticktick いずれもdebファイルが用意されている.

sudo apt install ./filename.deb

でインストールできる.

以下はリンク先